migrations.rs (1672B)
1 #![forbid(unsafe_code)] 2 3 use radroots_sql_core::SqlExecutor; 4 use radroots_sql_core::error::SqlError; 5 use radroots_sql_core::migrations::{Migration, migrations_run_all_down, migrations_run_all_up}; 6 7 pub static MIGRATIONS: &[Migration] = &[ 8 Migration { 9 name: "0000_local_events", 10 up_sql: include_str!("../migrations/0000_local_events.up.sql"), 11 down_sql: include_str!("../migrations/0000_local_events.down.sql"), 12 }, 13 Migration { 14 name: "0001_change_tracking", 15 up_sql: include_str!("../migrations/0001_change_tracking.up.sql"), 16 down_sql: include_str!("../migrations/0001_change_tracking.down.sql"), 17 }, 18 Migration { 19 name: "0002_network_source_runtime", 20 up_sql: include_str!("../migrations/0002_network_source_runtime.up.sql"), 21 down_sql: include_str!("../migrations/0002_network_source_runtime.down.sql"), 22 }, 23 ]; 24 25 pub fn run_all_up<E>(executor: &E) -> Result<(), SqlError> 26 where 27 E: SqlExecutor, 28 { 29 migrations_run_all_up(executor, MIGRATIONS) 30 } 31 32 pub fn run_all_down<E>(executor: &E) -> Result<(), SqlError> 33 where 34 E: SqlExecutor, 35 { 36 migrations_run_all_down(executor, MIGRATIONS) 37 } 38 39 #[cfg(test)] 40 mod tests { 41 use radroots_sql_core::SqliteExecutor; 42 43 use super::*; 44 45 #[test] 46 fn migration_entrypoints_apply_and_reverse_schema() { 47 let executor = SqliteExecutor::open_memory().expect("open memory sqlite"); 48 49 run_all_up(&executor).expect("migrate up"); 50 executor 51 .query_raw("select name from __migrations order by name", "[]") 52 .expect("query migrations"); 53 run_all_down(&executor).expect("migrate down"); 54 } 55 }