Skip to main content

assert_migration_set

Function assert_migration_set 

Source
pub const fn assert_migration_set(set: &[SqlMigration])
Expand description

Rejects a malformed migration set at compile time.

The array a module hands to Migrations is the apply order, and nothing checked that it agreed with the ids in it. auth-core defines its six migration constants in the file in the order 1, 2, 5, 6, 4, 3 and lists them in the arrays correctly; that is one careless edit away from applying 0005 before 0003, against a database that would record both as applied and never notice (issue #27).

Call it in a const item beside the set, where a failure is a build error rather than something a boot discovers:

const MIGRATIONS: [SqlMigration; 2] = [
    SqlMigration::new("0001", "init", ""),
    SqlMigration::new("0002", "next", ""),
];
const _: () = assert_migration_set(&MIGRATIONS);

A gap does not compile:

ⓘ
const MIGRATIONS: [SqlMigration; 2] = [
    SqlMigration::new("0001", "init", ""),
    SqlMigration::new("0003", "skipped", ""),
];
const _: () = assert_migration_set(&MIGRATIONS);

Nor a duplicate, nor an id out of order, nor one that is not four digits:

ⓘ
const MIGRATIONS: [SqlMigration; 2] = [
    SqlMigration::new("0002", "second", ""),
    SqlMigration::new("0001", "first", ""),
];
const _: () = assert_migration_set(&MIGRATIONS);
ⓘ
const MIGRATIONS: [SqlMigration; 1] =
    [SqlMigration::new("1", "unpadded", "")];
const _: () = assert_migration_set(&MIGRATIONS);

§Panics

At compile time when called in a const item, at run time otherwise. The message is a fixed string: panic! in a const fn cannot format, so the offending id is not in it. The compile error points at the const item, which names the crate and the line — enough to find the set, which is the whole array anyway.