use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
const UP: &str = include_str!("sql/0007_transcript_schema_reconciliation.sql");
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> {
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(())
}
}