Skip to main content

tango_core/migration/
mod.rs

1//! Schema migrations.
2//!
3//! Each migration embeds its SQL from `sql/` at compile time, so there is one
4//! copy of every statement and the migrations are the only source of truth for
5//! the schema. `docs/initial_schema.sql` was that file; it is gone now that
6//! `tango-rs migrate` applies a real, versioned sequence.
7//!
8//! On PostgreSQL each migration runs inside its own transaction, so a failure
9//! part-way through leaves the database as it was rather than half migrated.
10//!
11//! Migrations are historical. `m20260101_000001_create_schema` deliberately does
12//! not contain the columns added by 0002 and 0003, so that replaying the
13//! sequence against an existing database reproduces the real order of change.
14//!
15//! Released migrations are frozen. `migrations.lock` records a SHA-256 of every
16//! SQL file at release time, and `tests/migration_lock.rs` fails when one
17//! changes: the ledger records a migration's *name*, never its content, so an
18//! edit to a released migration is invisible at runtime - every deployed
19//! database keeps the old behaviour while a fresh replay produces something
20//! else. A migration that is not in the lock yet is unreleased and may still
21//! change.
22
23use sea_orm_migration::prelude::*;
24
25pub struct Migrator;
26
27#[async_trait::async_trait]
28impl MigratorTrait for Migrator {
29    fn migrations() -> Vec<Box<dyn MigrationTrait>> {
30        vec![
31            Box::new(m20260101_000001_create_schema::Migration),
32            Box::new(m20260101_000002_channel_metadata_and_source_tab::Migration),
33            Box::new(m20260101_000003_duration_seconds::Migration),
34            Box::new(m20260101_000004_resumable_harvests::Migration),
35            Box::new(m20260101_000005_search::Migration),
36            Box::new(m20260101_000006_drop_stale_search_overloads::Migration),
37            Box::new(m20260101_000007_transcript_schema_reconciliation::Migration),
38            Box::new(m20260101_000008_subtitle_availability_and_transcript_source::Migration),
39            Box::new(m20260101_000009_captions_available::Migration),
40            Box::new(m20260101_0010_availability_track_urls::Migration),
41            Box::new(m20260101_0011_search_transcripts::Migration),
42            Box::new(m20260101_0012_transcript_seek_position::Migration),
43            Box::new(m20260101_0013_backfill_captions_available::Migration),
44            Box::new(m20260101_0014_search_count::Migration),
45            Box::new(m20260101_0015_captions_available_meaning::Migration),
46            Box::new(m20260101_0016_harvest_filter_identity::Migration),
47            Box::new(m20260101_0017_strict_fuzzy::Migration),
48            Box::new(m20260101_0018_search_fields::Migration),
49            Box::new(m20260101_0019_search_sort::Migration),
50            Box::new(m20260101_0020_search_metadata_only::Migration),
51            Box::new(m20260101_0021_transcript_search_perf::Migration),
52            Box::new(m20260101_0022_transcript_search_relevance::Migration),
53            Box::new(m20260101_0023_discussion_density::Migration),
54            Box::new(m20260101_0024_contract_version::Migration),
55            Box::new(m20260101_0025_drop_stray_search_transcripts_old::Migration),
56            Box::new(m20260101_0026_channels_product_columns::Migration),
57            Box::new(m20260101_0027_video_chapters::Migration),
58        ]
59    }
60}
61
62/// Applies every pending migration.
63pub async fn up(db: &sea_orm::DatabaseConnection) -> Result<(), DbErr> {
64    <Migrator as MigratorTrait>::up(db, None).await
65}
66
67/// Reverts the most recent `steps` migrations.
68///
69/// Exists so `down` can be tested rather than assumed. Every migration here
70/// has a real reverse, which is the property that makes a bad migration
71/// recoverable instead of requiring a dropped database.
72pub async fn down(db: &sea_orm::DatabaseConnection, steps: u32) -> Result<(), DbErr> {
73    <Migrator as MigratorTrait>::down(db, Some(steps)).await
74}
75
76/// Everything this binary knows how to apply, oldest first.
77pub fn all() -> Vec<String> {
78    Migrator::migrations()
79        .iter()
80        .map(|m| m.name().to_string())
81        .collect()
82}
83
84/// Migrations defined here that have not been applied.
85pub async fn pending(db: &sea_orm::DatabaseConnection) -> Result<Vec<String>, DbErr> {
86    Ok(Migrator::get_pending_migrations(db)
87        .await?
88        .into_iter()
89        .map(|m| m.name().to_string())
90        .collect())
91}
92
93pub mod m20260101_000001_create_schema;
94pub mod m20260101_000002_channel_metadata_and_source_tab;
95pub mod m20260101_000003_duration_seconds;
96pub mod m20260101_000004_resumable_harvests;
97pub mod m20260101_000005_search;
98pub mod m20260101_000006_drop_stale_search_overloads;
99pub mod m20260101_000007_transcript_schema_reconciliation;
100pub mod m20260101_000008_subtitle_availability_and_transcript_source;
101pub mod m20260101_000009_captions_available;
102pub mod m20260101_0010_availability_track_urls;
103pub mod m20260101_0011_search_transcripts;
104pub mod m20260101_0012_transcript_seek_position;
105pub mod m20260101_0013_backfill_captions_available;
106pub mod m20260101_0014_search_count;
107pub mod m20260101_0015_captions_available_meaning;
108pub mod m20260101_0016_harvest_filter_identity;
109pub mod m20260101_0017_strict_fuzzy;
110pub mod m20260101_0018_search_fields;
111pub mod m20260101_0019_search_sort;
112pub mod m20260101_0020_search_metadata_only;
113pub mod m20260101_0021_transcript_search_perf;
114pub mod m20260101_0022_transcript_search_relevance;
115pub mod m20260101_0023_discussion_density;
116pub mod m20260101_0024_contract_version;
117pub mod m20260101_0025_drop_stray_search_transcripts_old;
118pub mod m20260101_0026_channels_product_columns;
119pub mod m20260101_0027_video_chapters;