Repository navigation
feat(backend): SQLite migration framework via PRAGMA user_version (#241) - #249
Merged
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat(backend): SQLite migration framework via PRAGMA user_version (#241)
Replace the silent-failure pattern in
init_tableswith a realmigration framework:
PRAGMA user_versionis the cursor (sqlite built-in, nocustom table needed)
MIGRATIONSis an ordered list of(version, name, up SQL)inconnection/migrations.rsrun(conn)reads the current cursor, runs only migrations withversion > cursor, each inside a transaction, then bumps the
cursor. Idempotent.
upis split on;and run statement-by-statement(
execute()rejects multi-statement strings for DDL).Migrations captured:
create_connection_profiles(initial 15-col schema)add_env_columnadd_connect_timeout_secsadd_query_timeout_secsadd_charset_columnSCHEMA_VERSION = 5is the single source of truth for "what's thecurrent head". Append a new entry; never renumber.
init_tablesis now 8 lines: lock the mutex, callmigrations::run(&db).No more
.ok()swallowing "duplicate column" errors silently. Theold code's silent failure pattern was masking every
column-addition in 0.2.x / 0.3.x as a no-op.
Why the redesign:
.ok()failures are dangerous: if 0.4.0 renames a columnor 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.
re-runnability, debuggable failure messages.
Tests (4 cases):
fresh_db_runs_all_migrations— empty DB ends at v5 with allcolumns present
existing_v3_db_applies_only_pending_migrations— simulate auser 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— runningrun()on a current-stateDB is a no-op
runs_each_migration_exactly_once— re-running after successdoesn't error or duplicate-apply
Out of scope:
tests/integration_tests.rsrequire 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.
query_database_context_regressiontest (the "5 filteredout" in
cargo test -p mas-core) is unrelated to this change.mode, checksum) is overkill for this project's needs.
Files:
crates/mas-core/src/connection/migrations.rs(new, 219 lines):Migrationstruct,MIGRATIONSconst,run()function, 4 unittests
crates/mas-core/src/connection/mod.rs: register the newmigrationsmodulecrates/mas-core/src/connection/store.rs:init_tablescallsmigrations::run(&db)instead of inlineCREATE + 4 silent ALTERs. -45 / +6 net.
Cargo.lock: bumped sqlpilot 0.3.4 -> 0.4.0 (from the versionbump in main; not a code change here).