tango-core 0.3.0

The tango schema, entities, migrations, and search contract
Documentation
use sea_orm_migration::prelude::*;

/// Corrects what `captions_available` means, and the rows written under the
/// wrong reading of it.
///
/// It was read as "this video has captions". It reports whether the **creator
/// published** caption tracks, and says nothing about YouTube's auto-generated
/// ones — which most videos with any speech have. The transcript command was
/// skipping on it, so videos that had transcripts available were never fetched.
///
/// On the dev database, 151 videos were marked false and every one checked with
/// `yt-dlp --list-subs` had `en` and `en-orig` auto-captions. 73 of them had
/// already been transcribed before the flag existed, which is what made the
/// contradiction visible: a transcript cannot be fetched from a video with no
/// captions.
///
/// Those 73 go back to NULL — unknown — rather than to true. The transcript
/// proves captions existed, but not by way of this column's source, and writing
/// `true` would attribute the Data API's answer to something that did not come
/// from it.
///
/// `transcript_targets` now filters on `subtitle_availability` instead: having
/// looked and found nothing is the only thing that settles it.
#[derive(DeriveMigrationName)]
pub struct Migration;

const UP: &str = include_str!("sql/0015_captions_available_meaning.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> {
        // Reversible without inventing a value: the rows 0013 backfilled to
        // `true` from stored transcripts, and this migration only undid the
        // `false` ones. Restoring means putting those back to false, which is
        // what the Data API said about them.
        manager
            .get_connection()
            .execute_unprepared(
                r#"
                UPDATE public.videos v
                SET captions_available = false
                WHERE v.captions_available IS NULL
                  AND EXISTS (
                    SELECT 1 FROM public.video_transcript_documents d
                    WHERE d.video_id = v.id
                  );
                "#,
            )
            .await?;
        Ok(())
    }
}