Skip to content

feat(backend): SQLite migration framework via PRAGMA user_version (#241) - #249

Merged
EVWorth merged 1 commit into
mainfrom
feat/sqlite-migration-framework
Jul 23, 2026
Merged

EVWorth merged 1 commit into
mainfrom
feat/sqlite-migration-framework

Conversation

@EVWorth

@EVWorth EVWorth commented Jul 22, 2026

Copy link
Copy Markdown
Owner

feat(backend): SQLite migration framework via PRAGMA user_version (#241)

Replace the silent-failure pattern in init_tables with a real
migration framework:

  • PRAGMA user_version is the cursor (sqlite built-in, no
    custom table needed)
  • MIGRATIONS is an ordered list of (version, name, up SQL) in
    connection/migrations.rs
  • run(conn) reads the current cursor, runs only migrations with
    version > cursor, each inside a transaction, then bumps the
    cursor. Idempotent.
  • Each up is split on ; and run statement-by-statement
    (execute() rejects multi-statement strings for DDL).

Migrations captured:

  • v1: create_connection_profiles (initial 15-col schema)
  • v2: add_env_column
  • v3: add_connect_timeout_secs
  • v4: add_query_timeout_secs
  • v5: add_charset_column

SCHEMA_VERSION = 5 is the single source of truth for "what's the
current head". Append a new entry; never renumber.

init_tables is now 8 lines: lock the mutex, call migrations::run(&db).
No more .ok() swallowing "duplicate column" errors silently. The
old code's silent failure pattern was masking every
column-addition in 0.2.x / 0.3.x as a no-op.

Why the redesign:

  • Silent .ok() failures are dangerous: if 0.4.0 renames a column
    or changes a type, the migration would fail silently and the
    user's local DB would have the old schema while a 0.3.4→0.4.0
    upgrade would write data into the wrong shape. Real corruption
    risk.
  • Per-version cursor: deterministic order, deterministic
    re-runnability, debuggable failure messages.

Tests (4 cases):

  • fresh_db_runs_all_migrations — empty DB ends at v5 with all
    columns present
  • existing_v3_db_applies_only_pending_migrations — simulate a
    user at v3 (manually set up the table + env + connect_timeout),
    verify the framework applies v4 + v5 only and lands at the head
  • already_at_head_is_noop — running run() on a current-state
    DB is a no-op
  • runs_each_migration_exactly_once — re-running after success
    doesn't error or duplicate-apply

Out of scope:

  • Connection-store CRUD tests in tests/integration_tests.rs
    require a real MySQL 8 (port 13306) per the project's testing
    doc. They were not run here; the migration framework changes
    don't touch those flows.
  • The query_database_context_regression test (the "5 filtered
    out" in cargo test -p mas-core) is unrelated to this change.
  • A more sophisticated framework (e.g. with rollback tables, dry-run
    mode, checksum) is overkill for this project's needs.

Files:

  • crates/mas-core/src/connection/migrations.rs (new, 219 lines):
    Migration struct, MIGRATIONS const, run() function, 4 unit
    tests
  • crates/mas-core/src/connection/mod.rs: register the new
    migrations module
  • crates/mas-core/src/connection/store.rs:
    init_tables calls migrations::run(&db) instead of inline
    CREATE + 4 silent ALTERs. -45 / +6 net.
  • Cargo.lock: bumped sqlpilot 0.3.4 -> 0.4.0 (from the version
    bump in main; not a code change here).

Replace the silent-failure pattern in `init_tables` with a real
migration framework:

- `PRAGMA user_version` is the cursor (sqlite built-in, no
  custom table needed)
- `MIGRATIONS` is an ordered list of `(version, name, up SQL)` in
  `connection/migrations.rs`
- `run(conn)` reads the current cursor, runs only migrations with
  version > cursor, each inside a transaction, then bumps the
  cursor. Idempotent.
- Each `up` is split on `;` and run statement-by-statement
  (`execute()` rejects multi-statement strings for DDL).

Migrations captured:
- v1: `create_connection_profiles` (initial 15-col schema)
- v2: `add_env_column`
- v3: `add_connect_timeout_secs`
- v4: `add_query_timeout_secs`
- v5: `add_charset_column`

`SCHEMA_VERSION = 5` is the single source of truth for "what's the
current head". Append a new entry; never renumber.

`init_tables` is now 8 lines: lock the mutex, call `migrations::run(&db)`.
No more `.ok()` swallowing "duplicate column" errors silently. The
old code's silent failure pattern was masking every
column-addition in 0.2.x / 0.3.x as a no-op.

Why the redesign:
- Silent `.ok()` failures are dangerous: if 0.4.0 renames a column
  or changes a type, the migration would fail silently and the
  user's local DB would have the old schema while a 0.3.4→0.4.0
  upgrade would write data into the wrong shape. Real corruption
  risk.
- Per-version cursor: deterministic order, deterministic
  re-runnability, debuggable failure messages.

Tests (4 cases):
- `fresh_db_runs_all_migrations` — empty DB ends at v5 with all
  columns present
- `existing_v3_db_applies_only_pending_migrations` — simulate a
  user at v3 (manually set up the table + env + connect_timeout),
  verify the framework applies v4 + v5 only and lands at the head
- `already_at_head_is_noop` — running `run()` on a current-state
  DB is a no-op
- `runs_each_migration_exactly_once` — re-running after success
  doesn't error or duplicate-apply

Out of scope:
- Connection-store CRUD tests in `tests/integration_tests.rs`
  require a real MySQL 8 (port 13306) per the project's testing
  doc. They were not run here; the migration framework changes
  don't touch those flows.
- The `query_database_context_regression` test (the "5 filtered
  out" in `cargo test -p mas-core`) is unrelated to this change.
- A more sophisticated framework (e.g. with rollback tables, dry-run
  mode, checksum) is overkill for this project's needs.

Files:
- `crates/mas-core/src/connection/migrations.rs` (new, 219 lines):
  `Migration` struct, `MIGRATIONS` const, `run()` function, 4 unit
  tests
- `crates/mas-core/src/connection/mod.rs`: register the new
  `migrations` module
- `crates/mas-core/src/connection/store.rs`:
  `init_tables` calls `migrations::run(&db)` instead of inline
  CREATE + 4 silent ALTERs. -45 / +6 net.
- `Cargo.lock`: bumped sqlpilot 0.3.4 -> 0.4.0 (from the version
  bump in main; not a code change here).
@EVWorth
EVWorth merged commit 981e1a2 into main Jul 23, 2026
10 checks passed
@EVWorth
EVWorth deleted the feat/sqlite-migration-framework branch July 23, 2026 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant