ORM-26: Add Model::schema() and a schema drift check #26

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

Depends on: #17, #25

Context

Models and migrations are written separately, so they can disagree. Rather than generating migrations from models (risky with SQLite's limited ALTER), a hand-written Model::schema() describes what the model expects, and a verifier compares it with the real database. It is a safety net for tests and CI.

Tasks

  • Optional trait method fn schema() -> Option<TableSchema> (default None) describing columns with type and nullability, the PK and foreign keys.
  • Schema::verify::<M>() -> Result<Vec<Drift>> compares against PRAGMA table_info and PRAGMA foreign_key_list. A Drift names the table, the column and the difference (missing column, type mismatch, nullability, missing FK).
  • Relation descriptors report the FK or pivot they expect, so verification covers them.
  • Wire into the tests: after Migrator::run(), verify every fixture model.

Example

impl Model for Post {
    /* ... */
    fn schema() -> Option<TableSchema> {
        Some(
            TableSchema::new(Self::TABLE)
                .integer("id").primary_key()
                .integer("user_id").references("users")
                .text("title")
                .boolean("published"),
        )
    }
}
#[test]
fn migrated_schema_matches_the_models() {
    setup_with_relations();
    assert!(Schema::verify::<User>().unwrap().is_empty());
    assert!(Schema::verify::<Post>().unwrap().is_empty());
}

What a Drift looks like (Display):

posts.title: model says TEXT NOT NULL, database has TEXT (nullable)
posts.user_id: model expects FOREIGN KEY -> users(id), database has none
tags: table does not exist

Acceptance criteria

  • A correct schema reports no drift.
  • Each drift kind is detected against a deliberately wrong table.
  • Every fixture model is verified in the test setup.
**Depends on:** #17, #25 ## Context Models and migrations are written separately, so they can disagree. Rather than generating migrations from models (risky with SQLite's limited `ALTER`), a hand-written `Model::schema()` describes what the model expects, and a verifier compares it with the real database. It is a safety net for tests and CI. ## Tasks - [ ] Optional trait method `fn schema() -> Option<TableSchema>` (default `None`) describing columns with type and nullability, the PK and foreign keys. - [ ] `Schema::verify::<M>() -> Result<Vec<Drift>>` compares against `PRAGMA table_info` and `PRAGMA foreign_key_list`. A `Drift` names the table, the column and the difference (missing column, type mismatch, nullability, missing FK). - [ ] Relation descriptors report the FK or pivot they expect, so verification covers them. - [ ] Wire into the tests: after `Migrator::run()`, verify every fixture model. ## Example ```rust impl Model for Post { /* ... */ fn schema() -> Option<TableSchema> { Some( TableSchema::new(Self::TABLE) .integer("id").primary_key() .integer("user_id").references("users") .text("title") .boolean("published"), ) } } ``` ```rust #[test] fn migrated_schema_matches_the_models() { setup_with_relations(); assert!(Schema::verify::<User>().unwrap().is_empty()); assert!(Schema::verify::<Post>().unwrap().is_empty()); } ``` What a `Drift` looks like (`Display`): ``` posts.title: model says TEXT NOT NULL, database has TEXT (nullable) posts.user_id: model expects FOREIGN KEY -> users(id), database has none tags: table does not exist ``` ## Acceptance criteria - [ ] A correct schema reports no drift. - [ ] Each drift kind is detected against a deliberately wrong table. - [ ] Every fixture model is verified in the test setup.
Sign in to join this conversation.
No description provided.