ORM-24: Add the schema builder core #24

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

Depends on: #9

Context

Raw SQL migrations work, but common DDL is repetitive. The builder is a pure SQL generator: closures run at construction time and the generated SQL is stored in the Migration, so the checksum covers real SQL and the runner from M2 does not change. Migrations never reference model code, only literal table and column names.

Tasks

  • Schema collects SQL: create_table(name, |t| ..), drop_table, rename_table, add_column, rename_column, drop_column and create_index(name, table, columns, unique).
  • Table builder: id(), integer, text, real, boolean, blob, with modifiers not_null(), unique() and default(impl Into<Value>).
  • Migration::schema(version, name).up(|s| ..).down(|s| ..).
  • String defaults escape single quotes.

Example

Migration::schema(1, "create_users")
    .up(|s| s.create_table("users", |t| {
        t.id();
        t.text("name").not_null();
        t.text("email").unique();
        t.integer("age").not_null();
        t.boolean("active").not_null().default(true);
    }))
    .down(|s| s.drop_table("users"));

Generated SQL:

CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE,
    age INTEGER NOT NULL,
    active INTEGER NOT NULL DEFAULT 1
);
#[test]
fn string_defaults_are_escaped() {
    let sql = Schema::new()
        .create_table("t", |t| { t.text("note").default("it's"); })
        .sql();
    assert!(sql.contains("DEFAULT 'it''s'"));
}

Acceptance criteria

  • Golden-string tests cover each operation.
  • A builder-made migration runs through Migrator and produces the expected table according to PRAGMA table_info.
**Depends on:** #9 ## Context Raw SQL migrations work, but common DDL is repetitive. The builder is a pure SQL generator: closures run at construction time and the generated SQL is stored in the `Migration`, so the checksum covers real SQL and the runner from M2 does not change. Migrations never reference model code, only literal table and column names. ## Tasks - [ ] `Schema` collects SQL: `create_table(name, |t| ..)`, `drop_table`, `rename_table`, `add_column`, `rename_column`, `drop_column` and `create_index(name, table, columns, unique)`. - [ ] Table builder: `id()`, `integer`, `text`, `real`, `boolean`, `blob`, with modifiers `not_null()`, `unique()` and `default(impl Into<Value>)`. - [ ] `Migration::schema(version, name).up(|s| ..).down(|s| ..)`. - [ ] String defaults escape single quotes. ## Example ```rust Migration::schema(1, "create_users") .up(|s| s.create_table("users", |t| { t.id(); t.text("name").not_null(); t.text("email").unique(); t.integer("age").not_null(); t.boolean("active").not_null().default(true); })) .down(|s| s.drop_table("users")); ``` Generated SQL: ```sql CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE, age INTEGER NOT NULL, active INTEGER NOT NULL DEFAULT 1 ); ``` ```rust #[test] fn string_defaults_are_escaped() { let sql = Schema::new() .create_table("t", |t| { t.text("note").default("it's"); }) .sql(); assert!(sql.contains("DEFAULT 'it''s'")); } ``` ## Acceptance criteria - [ ] Golden-string tests cover each operation. - [ ] A builder-made migration runs through `Migrator` and produces the expected table according to `PRAGMA table_info`.
Sign in to join this conversation.
No description provided.