tango-core 0.3.0

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

/// Reconciles the reserved transcript tables with what transcript indexing needs.
///
/// `0001` created both tables as placeholders, keyed by `video_id` alone, with
/// `start_seconds` and no full-text column. Nothing has ever written to them, so
/// this is the cheap moment to fix three things that are expensive afterwards:
///
/// 1. Add `lang` and re-key on `(video_id, lang)`. With one transcript per video
///    forever, a second language is impossible.
/// 2. Replace `start_seconds` with `start_ms` / `end_ms`. A seek position needs
///    millisecond resolution, and a cue with no end has no duration.
/// 3. Add generated `tsv` columns and GIN indexes, because adding a generated
///    column to a populated table is a full rewrite.
///
/// `videos.transcript_status` is deliberately untouched - its vocabulary has not
/// been decided, and a CHECK constraint guessing at it would be as hard to
/// remove later as a primary key guessed from incomplete requirements.
#[derive(DeriveMigrationName)]
pub struct Migration;

const UP: &str = include_str!("sql/0007_transcript_schema_reconciliation.sql");

/// The `0001` shapes, verbatim. Kept as SQL rather than derived, so a revert
/// reproduces what the schema actually was rather than what it is assumed to be.
const DOWN: &str = r#"
    DROP TABLE IF EXISTS public.video_transcripts;
    DROP TABLE IF EXISTS public.video_transcript_documents;

    CREATE TABLE public.video_transcript_documents (
      video_id text NOT NULL,
      body text NOT NULL,
      updated_at timestamp with time zone NOT NULL DEFAULT now(),
      CONSTRAINT video_transcript_documents_pkey PRIMARY KEY (video_id),
      CONSTRAINT video_transcript_documents_video_id_fkey
        FOREIGN KEY (video_id) REFERENCES public.videos(id)
    );

    CREATE TABLE public.video_transcripts (
      video_id text NOT NULL,
      cue_index integer NOT NULL,
      start_seconds integer NOT NULL,
      content text NOT NULL,
      CONSTRAINT video_transcripts_pkey PRIMARY KEY (video_id, cue_index),
      CONSTRAINT video_transcripts_video_id_fkey
        FOREIGN KEY (video_id) REFERENCES public.videos(id)
    );
"#;

#[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> {
        // A real reverse, unlike 0006, because the `0001` shape is known - but
        // only losslessly while the tables are empty. `lang`, `start_ms`,
        // `end_ms` and `tsv` have nowhere to go in the old shape, so a revert
        // that found rows would quietly discard them. Refuse rather than do
        // that; it is the same guard `up` needs, for the same reason.
        manager
            .get_connection()
            .execute_unprepared(
                r#"
                DO $$
                BEGIN
                    IF EXISTS (SELECT 1 FROM public.video_transcripts)
                       OR EXISTS (SELECT 1 FROM public.video_transcript_documents) THEN
                        RAISE EXCEPTION
                            'refusing to revert 0007: the transcript tables have rows, and the pre-0007 shape cannot store lang, start_ms or end_ms';
                    END IF;
                END
                $$;
                "#,
            )
            .await?;
        manager.get_connection().execute_unprepared(DOWN).await?;
        Ok(())
    }
}