use super::*;
fn migration(id: &str, up: &str) -> Migration {
Migration {
id: id.to_owned(),
description: format!("migration {id}"),
up_pre: None,
up: up.to_owned(),
down: None,
}
}
#[test]
fn checksum_changes_when_up_sql_changes() {
let a = migration("20260101000000_init", "CREATE TABLE a (id INT);");
let mut b = a.clone();
b.up = "CREATE TABLE a (id BIGINT);".to_owned();
assert_ne!(a.checksum(), b.checksum());
}
#[test]
fn checksum_is_stable_for_same_inputs() {
let a = migration("20260101000000_init", "CREATE TABLE a (id INT);");
let b = a.clone();
assert_eq!(a.checksum(), b.checksum());
}
#[test]
fn checksum_changes_when_up_pre_sql_changes() {
let mut a = migration(
"20260101000000_init",
"ALTER TABLE a ALTER COLUMN c SET NOT NULL;",
);
a.up_pre = Some("UPDATE a SET c = 0 WHERE c IS NULL;".to_owned());
let mut b = a.clone();
b.up_pre = Some("UPDATE a SET c = 1 WHERE c IS NULL;".to_owned());
assert_ne!(a.checksum(), b.checksum());
}
#[test]
fn checksum_changes_when_up_pre_sql_is_added() {
let a = migration(
"20260101000000_init",
"ALTER TABLE a ALTER COLUMN c SET NOT NULL;",
);
let mut b = a.clone();
b.up_pre = Some("UPDATE a SET c = 0 WHERE c IS NULL;".to_owned());
assert_ne!(a.checksum(), b.checksum());
}
#[test]
fn checksum_without_up_pre_matches_the_pre_up_pre_digest() {
let migration = migration("20260101000000_init", "CREATE TABLE a (id INT);");
let mut legacy = Sha256::new();
legacy.update(migration.id.as_bytes());
legacy.update(b"\0");
legacy.update(migration.description.as_bytes());
legacy.update(b"\0");
legacy.update(migration.up.as_bytes());
let legacy: [u8; 32] = legacy.finalize().into();
assert_eq!(migration.checksum(), legacy);
}