Skip to main content

miden_node_db/migration/
mod.rs

1//! Provides a framework for SQLite migrations.
2//!
3//! Migrations are built as an ordered [`Migrator`] with two phases. Retired migrations are retained
4//! as pure SQL and are only used to initialize fresh databases. Active migrations run after the
5//! retired SQL set, remain supported for existing databases, and can be pure SQL or Rust functions.
6//! This lets old active migrations eventually be converted into retired SQL once their upgrade path
7//! no longer needs to be supported.
8//!
9//! The database version is stored in SQLite's `PRAGMA user_version`. Each migration also has an
10//! expected [`SchemaHash`] computed by applying migrations to an in-memory reference database
11//! during builder construction. Use [`Migrator::create_database`] or [`Migrator::open_database`]
12//! to obtain a handle, then call [`MigrationDatabase::migrate_to`] with the desired version. Each
13//! migration commits only after the resulting schema hash matches the expected hash. Normal load
14//! paths should use [`Migrator::verify_latest_schema`] before opening their connection pools.
15//!
16//! Build migrators manually with [`Migrator::builder`], or generate one from a migration directory
17//! with [`Migrator::generate`] in a `build.rs`. Callers should snapshot [`Migrator::schema_hashes`]
18//! in tests to catch accidental schema drift and to prove that retired SQL still produces the same
19//! schema as the active migrations it replaced.
20
21mod build_script;
22mod builder;
23mod entry;
24mod migrator;
25mod schema;
26
27pub use builder::MigratorBuilder;
28pub use entry::CodeMigrationFn;
29pub use migrator::{MigrationDatabase, Migrator};
30pub use schema::{SchemaHash, SchemaHashes};