1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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(())
}
}