formatting pass
This commit is contained in:
1 parent
d976eebbbc
commit
e4cfa53bce
21 files changed
+333
-194
No files matched your search
+14
-8
@@ -3,16 +3,13 @@ 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())
|
||||
@@ -25,7 +22,11 @@ fn embedded_migrations() -> Vec<Migration> {
|
||||
.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"),
|
||||
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();
|
||||
@@ -81,11 +82,10 @@ fn list_applied_migrations(conn: &Connection) -> Result<Vec<String>, CreddyError
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use testresult::TestResult;
|
||||
use super::*;
|
||||
use testresult::TestResult;
|
||||
|
||||
const FIRST: Migration = Migration {
|
||||
id: "0001_first.sql",
|
||||
@@ -110,7 +110,10 @@ mod tests {
|
||||
migrate(&mut conn)?;
|
||||
|
||||
let applied = list_applied_migrations(&conn)?;
|
||||
let expected: Vec<_> = embedded_migrations().iter().map(|m| m.id.to_string()).collect();
|
||||
let expected: Vec<_> = embedded_migrations()
|
||||
.iter()
|
||||
.map(|m| m.id.to_string())
|
||||
.collect();
|
||||
assert_eq!(applied, expected);
|
||||
Ok(())
|
||||
}
|
||||
@@ -123,7 +126,10 @@ mod tests {
|
||||
|
||||
// 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_eq!(
|
||||
list_applied_migrations(&conn)?,
|
||||
["0001_first.sql", "0002_second.sql"]
|
||||
);
|
||||
assert!(table_exists(&conn, "b")?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user