use std::path::Path; use sqlx::{ SqlitePool, sqlite::{SqliteConnectOptions, SqlitePoolOptions}, }; const LEDGER: &str = include_str!("fixtures/migration_ledger.txt"); const PRE_MERGE_DB: &str = "tests/fixtures/pre_merge.db"; async fn migrate_with_todays_entry_points(pool: &SqlitePool) { sqlite::migrate(pool).await.unwrap(); } async fn ledger(pool: &SqlitePool) -> String { let rows: Vec<(i64, String)> = sqlx::query_as("SELECT version, hex(checksum) FROM _sqlx_migrations ORDER BY version") .fetch_all(pool) .await .unwrap(); rows.iter().map(|(v, c)| format!("{v} {c}\n")).collect() } async fn file_pool(path: &Path, create: bool) -> SqlitePool { let opts = SqliteConnectOptions::new() .filename(path) .create_if_missing(create); SqlitePoolOptions::new() .max_connections(1) .connect_with(opts) .await .unwrap() } #[tokio::test] async fn migrating_a_fresh_database_produces_the_recorded_ledger() { let pool = SqlitePool::connect("sqlite::memory:").await.unwrap(); migrate_with_todays_entry_points(&pool).await; assert_eq!(ledger(&pool).await, LEDGER); } #[tokio::test] async fn a_database_migrated_by_the_old_layout_migrates_again_cleanly() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("pre_merge.db"); std::fs::copy( Path::new(env!("CARGO_MANIFEST_DIR")).join(PRE_MERGE_DB), &path, ) .unwrap(); let pool = file_pool(&path, false).await; sqlite::migrate(&pool).await.unwrap(); assert_eq!(ledger(&pool).await, LEDGER); }