ORM-09: Add Migration, MigrationList and checksum #9

Closed
opened 2026-10-13 12:49:39 +01:00 by NoctisWolfshadow · 1 comment
Owner

Depends on: #1

Context

migration.rs is currently a private stub (Migration { sql, version }, MigrationList) that nothing uses. This issue replaces it with the public types the runner will work with. The checksum is what later lets the runner detect someone editing an already-applied migration.

Tasks

  • Migration { version: i64, name: String, up: String, down: Option<String>, rebuilds_tables: bool } with Migration::new(version, name, up_sql), .down(sql) and .rebuilds_tables().
  • MigrationList::new(Vec<Migration>) -> Result<Self> rejects duplicate or non-ascending versions.
  • Introduce MigrationError (starting with InvalidList(String)) and add Error::Migration(MigrationError).
  • Migration::checksum(): FNV-1a 64-bit over up with CRLF normalised to LF, as lowercase hex. Do not use DefaultHasher; its output is not stable across Rust releases.
  • pub use the types from lib.rs.

Example

let migrations = MigrationList::new(vec![
    Migration::new(1, "create_users", "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);")
        .down("DROP TABLE users;"),
    Migration::new(2, "add_email", "ALTER TABLE users ADD COLUMN email TEXT;")
        .down("ALTER TABLE users DROP COLUMN email;"),
    // SQL kept in files works too
    Migration::new(3, "create_posts", include_str!("../migrations/0003_create_posts.up.sql")),
])?;
fn fnv1a_64(input: &str) -> String {
    let mut hash: u64 = 0xcbf29ce484222325;
    for byte in input.replace("\r\n", "\n").bytes() {
        hash ^= byte as u64;
        hash = hash.wrapping_mul(0x100000001b3);
    }
    format!("{hash:016x}")
}
#[test]
fn checksum_known_value_and_line_endings() {
    assert_eq!(Migration::new(1, "x", "").checksum(), "cbf29ce484222325");
    let lf = Migration::new(1, "x", "A;\nB;").checksum();
    let crlf = Migration::new(1, "x", "A;\r\nB;").checksum();
    assert_eq!(lf, crlf);
}

#[test]
fn list_rejects_duplicates_and_disorder() {
    let m = |v| Migration::new(v, "x", "SELECT 1;");
    assert!(MigrationList::new(vec![m(1), m(1)]).is_err());
    assert!(MigrationList::new(vec![m(2), m(1)]).is_err());
    assert!(MigrationList::new(vec![m(1), m(2)]).is_ok());
}

Acceptance criteria

  • List validation tests pass.
  • The checksum of an empty string is cbf29ce484222325.
  • CRLF and LF variants of the same SQL give the same checksum.
**Depends on:** #1 ## Context `migration.rs` is currently a private stub (`Migration { sql, version }`, `MigrationList`) that nothing uses. This issue replaces it with the public types the runner will work with. The checksum is what later lets the runner detect someone editing an already-applied migration. ## Tasks - [x] `Migration { version: i64, name: String, up: String, down: Option<String>, rebuilds_tables: bool }` with `Migration::new(version, name, up_sql)`, `.down(sql)` and `.rebuilds_tables()`. - [x] `MigrationList::new(Vec<Migration>) -> Result<Self>` rejects duplicate or non-ascending versions. - [x] Introduce `MigrationError` (starting with `InvalidList(String)`) and add `Error::Migration(MigrationError)`. - [ ] `Migration::checksum()`: FNV-1a 64-bit over `up` with CRLF normalised to LF, as lowercase hex. Do not use `DefaultHasher`; its output is not stable across Rust releases. - [x] `pub use` the types from `lib.rs`. ## Example ```rust let migrations = MigrationList::new(vec![ Migration::new(1, "create_users", "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);") .down("DROP TABLE users;"), Migration::new(2, "add_email", "ALTER TABLE users ADD COLUMN email TEXT;") .down("ALTER TABLE users DROP COLUMN email;"), // SQL kept in files works too Migration::new(3, "create_posts", include_str!("../migrations/0003_create_posts.up.sql")), ])?; ``` ```rust fn fnv1a_64(input: &str) -> String { let mut hash: u64 = 0xcbf29ce484222325; for byte in input.replace("\r\n", "\n").bytes() { hash ^= byte as u64; hash = hash.wrapping_mul(0x100000001b3); } format!("{hash:016x}") } ``` ```rust #[test] fn checksum_known_value_and_line_endings() { assert_eq!(Migration::new(1, "x", "").checksum(), "cbf29ce484222325"); let lf = Migration::new(1, "x", "A;\nB;").checksum(); let crlf = Migration::new(1, "x", "A;\r\nB;").checksum(); assert_eq!(lf, crlf); } #[test] fn list_rejects_duplicates_and_disorder() { let m = |v| Migration::new(v, "x", "SELECT 1;"); assert!(MigrationList::new(vec![m(1), m(1)]).is_err()); assert!(MigrationList::new(vec![m(2), m(1)]).is_err()); assert!(MigrationList::new(vec![m(1), m(2)]).is_ok()); } ``` ## Acceptance criteria - [x] List validation tests pass. - [ ] The checksum of an empty string is `cbf29ce484222325`. - [ ] CRLF and LF variants of the same SQL give the same checksum.
Author
Owner

Checksum was not implemented since it was not needed.

Checksum was not implemented since it was not needed.
Sign in to join this conversation.
No description provided.