tango-core 0.3.0

The tango schema, entities, migrations, and search contract
Documentation
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(())
    }
}