Skip to content

fix(store): refuse a database that applied another text of a migration - #9

Merged
bketelsen merged 1 commit into
mainfrom
claude/objective-villani-550255
Oct 17, 2026
Merged

bketelsen merged 1 commit into
mainfrom
claude/objective-villani-550255

Conversation

@bketelsen

Copy link
Copy Markdown
Contributor

Why

The nsl dev database (/var/tmp/frostnotes/notesd) was created by M0's notesd. M1 edited 0001_init.sql in place (allowed pre-release), but schema_migrations already recorded migration 1, so M1's tables were never created. notesd still started: it logged no such table: versions / no such table: files, the vault looked registered, nothing was indexed, and search returned an Internal error.

What

  • schema_migrations gains a checksum column: the SHA-256 of each migration's SQL, recorded when the migration is applied.
  • store.Open compares the recorded checksums with the embedded migrations. On a mismatch it fails, and the error names the database, the migration and the fix:

    open database …/notesd.db: migration 0001_init.sql differs from the one this database applied: the database predates an in-place edit of the schema; move it aside, with any -wal and -shm files, and restart notesd to create a new one

  • Databases created before this change (no checksum column): the migrations are applied to an in-memory database and the two sqlite_schemas are compared. If they match, the checksums are recorded and the database keeps working. If not, it is refused with the same advice. M0's stale database is refused, and a current M1 one is kept.
  • Migrations now write through DB.Tx instead of their own BeginTx.
  • The 0001_init.sql header and docs/design/vault.md (Operational notes) say that an in-place edit now means moving dev databases aside. The header edit changes the checksum, but that costs nothing: no database records a checksum yet, and comments outside CREATE statements aren't part of the stored schema.

One trade-off: the checksum covers the exact bytes, so a comment-only edit to 0001_init.sql also makes notesd refuse existing dev databases. The file has been edited 3 times so far, so I kept the exact-bytes check rather than normalizing the SQL.

Tests

  • TestOpenRefusesEditedMigration: migration 1's recorded checksum differs from the embedded one, and Open refuses the database.
  • TestOpenRefusesUncheckedStaleSchema: an M0-style database with no checksums is refused.
  • TestOpenAdoptsChecksumsOfCurrentSchema: a current database with no checksums opens, records them, and reopens.
  • TestOpenMigratesAndReopens now also checks the recorded checksums.
  • make check passes, and go test -race passes on internal/store.

Not run: make ui-dev against a real stale database in nsl.

🤖 Generated with Claude Code

notesd started on a dev database whose schema_migrations recorded
0001_init.sql as applied from M0's text, before M1 edited the file in
place, and ran without M1's tables: scans and prunes logged "no such
table" and search failed. schema_migrations now records each migration's
SHA-256, and Open compares them with the embedded migrations, refusing a
mismatch with the database's path, the migration, and what to do (move
it aside). A database from before checksums adopts them once its schema
equals what its migrations create in an empty database; M0's stale one
is refused. Migrations now write through DB.Tx.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bketelsen
bketelsen merged commit b671ad8 into main Oct 17, 2026
4 checks passed
@bketelsen
bketelsen deleted the claude/objective-villani-550255 branch October 17, 2026 18:37
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