1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use sea_orm_migration::prelude::*;
/// Replaces the ambiguous duration text column with whole seconds.
///
/// The two backends disagreed on the wire - ISO 8601 from the Data API, a
/// display string from yt-dlp - and both were being written to one column. The
/// `up` statement embeds a backfill that handles either stored format and
/// leaves anything unrecognised NULL rather than guessing.
#[derive(DeriveMigrationName)]
pub struct Migration;
const UP: &str = include_str!("sql/0003_duration_seconds.sql");
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.get_connection().execute_unprepared(UP).await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Restores the display-string column. Values are not converted back:
// a display string is lossy, and the seconds that survive are more
// useful than a rounded reconstruction.
manager
.get_connection()
.execute_unprepared(
r#"
ALTER TABLE public.videos ADD COLUMN IF NOT EXISTS duration text;
DROP INDEX IF EXISTS public.videos_duration_idx;
ALTER TABLE public.videos DROP COLUMN IF EXISTS duration_seconds;
"#,
)
.await?;
Ok(())
}
}