add db migrations and restore config on unlock
This commit is contained in:
1 parent
a10510ed6e
commit
0798124811
12 files changed
+365
-41
No files matched your search
@@ -0,0 +1,174 @@
|
||||
use duckdb::Connection;
|
||||
use include_dir::{Dir, include_dir};
|
||||
|
||||
use crate::error::CreddyError;
|
||||
|
||||
|
||||
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
|
||||
|
||||
|
||||
struct Migration {
|
||||
id: &'static str,
|
||||
sql: &'static str,
|
||||
}
|
||||
|
||||
|
||||
/// Run all unapplied database migrations.
|
||||
pub fn migrate(conn: &mut Connection) -> Result<(), CreddyError> {
|
||||
apply_migrations(conn, &embedded_migrations())
|
||||
}
|
||||
|
||||
/// Sorted list of migration files, checked for utf-8-ness at compile time.
|
||||
fn embedded_migrations() -> Vec<Migration> {
|
||||
let mut migrations: Vec<Migration> = MIGRATIONS_DIR
|
||||
.files()
|
||||
.filter(|f| f.path().extension().is_some_and(|ext| ext == "sql"))
|
||||
.map(|f| Migration {
|
||||
// these are baked in at compile time, so a bad file is a build problem, not a runtime one
|
||||
id: f.path().file_name().and_then(|n| n.to_str()).expect("migration filename is not UTF-8"),
|
||||
sql: f.contents_utf8().expect("migration file is not UTF-8"),
|
||||
})
|
||||
.collect();
|
||||
migrations.sort_by_key(|m| m.id);
|
||||
migrations
|
||||
}
|
||||
|
||||
/// Run a specific list of migrations. (Makes testing easier than always hard-coding)
|
||||
fn apply_migrations(conn: &mut Connection, known: &[Migration]) -> Result<(), CreddyError> {
|
||||
create_migrations_table(conn)?;
|
||||
let applied = list_applied_migrations(conn)?;
|
||||
|
||||
// the applied migrations must be exactly the first N known ones
|
||||
for (i, id) in applied.iter().enumerate() {
|
||||
if known.get(i).is_some_and(|m| m.id == id) {
|
||||
continue;
|
||||
}
|
||||
// at this point, either known[i] doesn't exist or doesn't match `id`
|
||||
if known.iter().any(|m| m.id == id) {
|
||||
return Err(CreddyError::MigrationOutOfOrder(id.clone()));
|
||||
}
|
||||
//
|
||||
return Err(CreddyError::DbTooNew(id.clone()));
|
||||
}
|
||||
|
||||
for migration in &known[applied.len()..] {
|
||||
let tx = conn.transaction()?;
|
||||
tx.execute_batch(migration.sql)?;
|
||||
tx.execute("INSERT INTO _migrations (id) VALUES (?);", [migration.id])?;
|
||||
tx.commit()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create the migrations table if it doesn't exist already.
|
||||
fn create_migrations_table(conn: &Connection) -> Result<(), CreddyError> {
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE IF NOT EXISTS _migrations (
|
||||
id VARCHAR PRIMARY KEY,
|
||||
applied_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp
|
||||
);",
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// List migrations that have been applied to this database, in the order they were applied.
|
||||
fn list_applied_migrations(conn: &Connection) -> Result<Vec<String>, CreddyError> {
|
||||
let mut stmt = conn.prepare("SELECT id FROM _migrations ORDER BY id;")?;
|
||||
let ids = stmt
|
||||
.query_map([], |row| row.get(0))?
|
||||
.collect::<Result<Vec<String>, _>>()?;
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use testresult::TestResult;
|
||||
use super::*;
|
||||
|
||||
const FIRST: Migration = Migration {
|
||||
id: "0001_first.sql",
|
||||
sql: "CREATE TABLE a (x INTEGER);",
|
||||
};
|
||||
const SECOND: Migration = Migration {
|
||||
id: "0002_second.sql",
|
||||
sql: "CREATE TABLE b (x INTEGER); INSERT INTO b VALUES (1);",
|
||||
};
|
||||
|
||||
fn table_exists(conn: &Connection, name: &str) -> duckdb::Result<bool> {
|
||||
conn.query_row(
|
||||
"SELECT count(*) > 0 FROM duckdb_tables() WHERE table_name = ?;",
|
||||
[name],
|
||||
|row| row.get(0),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_embedded_migrations_apply() -> TestResult {
|
||||
let mut conn = Connection::open_in_memory()?;
|
||||
migrate(&mut conn)?;
|
||||
|
||||
let applied = list_applied_migrations(&conn)?;
|
||||
let expected: Vec<_> = embedded_migrations().iter().map(|m| m.id.to_string()).collect();
|
||||
assert_eq!(applied, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_incrementally() -> TestResult {
|
||||
let mut conn = Connection::open_in_memory()?;
|
||||
apply_migrations(&mut conn, &[FIRST])?;
|
||||
assert_eq!(list_applied_migrations(&conn)?, ["0001_first.sql"]);
|
||||
|
||||
// already-applied migrations are skipped, so re-running FIRST would fail if it weren't
|
||||
apply_migrations(&mut conn, &[FIRST, SECOND])?;
|
||||
assert_eq!(list_applied_migrations(&conn)?, ["0001_first.sql", "0002_second.sql"]);
|
||||
assert!(table_exists(&conn, "b")?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_failed_migration_rolls_back() -> TestResult {
|
||||
let mut conn = Connection::open_in_memory()?;
|
||||
let broken = Migration {
|
||||
id: "0002_broken.sql",
|
||||
sql: "CREATE TABLE b (x INTEGER); this is not sql;",
|
||||
};
|
||||
|
||||
let res = apply_migrations(&mut conn, &[FIRST, broken]);
|
||||
assert!(matches!(res, Err(CreddyError::Db(_))));
|
||||
|
||||
// FIRST stays applied, but nothing from the broken migration survives
|
||||
assert_eq!(list_applied_migrations(&conn)?, ["0001_first.sql"]);
|
||||
assert!(table_exists(&conn, "a")?);
|
||||
assert!(!table_exists(&conn, "b")?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_db_too_new() -> TestResult {
|
||||
let mut conn = Connection::open_in_memory()?;
|
||||
apply_migrations(&mut conn, &[FIRST, SECOND])?;
|
||||
|
||||
// simulates running an older version of the app, which doesn't know about SECOND
|
||||
match apply_migrations(&mut conn, &[FIRST]) {
|
||||
Err(CreddyError::DbTooNew(id)) => assert_eq!(id, "0002_second.sql"),
|
||||
res => panic!("Expected DbTooNew, got {res:?}"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_migration_out_of_order() -> TestResult {
|
||||
let mut conn = Connection::open_in_memory()?;
|
||||
apply_migrations(&mut conn, &[SECOND])?;
|
||||
|
||||
// FIRST sorts before an already-applied migration, so it was added after the fact
|
||||
match apply_migrations(&mut conn, &[FIRST, SECOND]) {
|
||||
Err(CreddyError::MigrationOutOfOrder(id)) => assert_eq!(id, "0002_second.sql"),
|
||||
res => panic!("Expected MigrationOutOfOrder, got {res:?}"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user