tango-core 0.3.0

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

/// The initial schema.
///
/// Raw SQL rather than the builder DSL on purpose: the tables carry
/// `GENERATED ALWAYS AS ... STORED` tsvector columns and partial GIN indexes,
/// both of which read better written out and both of which the builder makes
/// obscure. The statements are embedded at compile time so there is exactly
/// one copy and the docs cannot drift from what the database gets.
#[derive(DeriveMigrationName)]
pub struct Migration;

const SCHEMA: &str = include_str!("sql/0001_create_schema.sql");

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // 2.0 returns ExecResult from execute_unprepared where 1.1 returned
        // (). A row count is meaningless for DDL, so it is discarded.
        manager.get_connection().execute_unprepared(SCHEMA).await?;
        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // Reverse creation order, children before parents. The transcript
        // tables are dropped even though nothing writes to them: the schema is
        // the schema, and a half-reverted one is worse than none.
        manager
            .get_connection()
            .execute_unprepared(
                r#"
                DROP TABLE IF EXISTS public.video_transcripts;
                DROP TABLE IF EXISTS public.video_transcript_documents;
                DROP TABLE IF EXISTS public.playlist_videos;
                DROP TABLE IF EXISTS public.playlists;
                DROP TABLE IF EXISTS public.videos;
                DROP TABLE IF EXISTS public.channels;
                "#,
            )
            .await?;
        Ok(())
    }
}