ORM-10: Add the migration tracking table and Migrator::status() #10

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

Depends on: #6, #9

Context

The runner needs to know which migrations were applied, and whether they still match what is in the code. A tracking table (rather than PRAGMA user_version) can store the name and checksum, which is what makes drift detectable.

Tasks

  • Migrator::new(MigrationList).
  • Ensure the tracking table exists (IF NOT EXISTS), created on first use.
  • Migrator::status() -> Result<Vec<MigrationStatus>>, ascending by version, covering migrations from the list and rows recorded in the database.
  • States: Applied, Pending, Modified (checksum differs), Missing (recorded in the database but absent from the list).

Example

CREATE TABLE IF NOT EXISTS _tessera_migrations (
    version    INTEGER PRIMARY KEY,
    name       TEXT NOT NULL,
    checksum   TEXT NOT NULL,
    applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MigrationState { Applied, Pending, Modified, Missing }

pub struct MigrationStatus {
    pub version: i64,
    pub name: String,
    pub state: MigrationState,
}

let migrator = Migrator::new(migrations);
for s in migrator.status()? {
    println!("{:>3} {:<16} {:?}", s.version, s.name, s.state);
}
//   1 create_users     Applied
//   2 add_email        Pending
#[test]
fn fresh_database_reports_everything_pending() {
    Database::connect_thread(Config::memory()).unwrap();
    let status = Migrator::new(sample_list()).status().unwrap();
    assert!(status.iter().all(|s| s.state == MigrationState::Pending));
}

Acceptance criteria

  • One test per state (Applied, Pending, Modified, Missing).
  • status() on a fresh database creates the table and reports everything as Pending.
**Depends on:** #6, #9 ## Context The runner needs to know which migrations were applied, and whether they still match what is in the code. A tracking table (rather than `PRAGMA user_version`) can store the name and checksum, which is what makes drift detectable. ## Tasks - [x] `Migrator::new(MigrationList)`. - [x] Ensure the tracking table exists (`IF NOT EXISTS`), created on first use. - [x] `Migrator::status() -> Result<Vec<MigrationStatus>>`, ascending by version, covering migrations from the list and rows recorded in the database. - [x] States: `Applied`, `Pending`, `Modified` (checksum differs), `Missing` (recorded in the database but absent from the list). ## Example ```sql CREATE TABLE IF NOT EXISTS _tessera_migrations ( version INTEGER PRIMARY KEY, name TEXT NOT NULL, checksum TEXT NOT NULL, applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP ); ``` ```rust #[derive(Debug, Clone, PartialEq, Eq)] pub enum MigrationState { Applied, Pending, Modified, Missing } pub struct MigrationStatus { pub version: i64, pub name: String, pub state: MigrationState, } let migrator = Migrator::new(migrations); for s in migrator.status()? { println!("{:>3} {:<16} {:?}", s.version, s.name, s.state); } // 1 create_users Applied // 2 add_email Pending ``` ```rust #[test] fn fresh_database_reports_everything_pending() { Database::connect_thread(Config::memory()).unwrap(); let status = Migrator::new(sample_list()).status().unwrap(); assert!(status.iter().all(|s| s.state == MigrationState::Pending)); } ``` ## Acceptance criteria - [x] One test per state (`Applied`, `Pending`, `Modified`, `Missing`). - [x] `status()` on a fresh database creates the table and reports everything as `Pending`.
Sign in to join this conversation.
No description provided.