Repository navigation
fix(store): refuse a database that applied another text of a migration - #9
Merged
Merged
Conversation
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]>
6 tasks done
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.
Why
The nsl dev database (
/var/tmp/frostnotes/notesd) was created by M0's notesd. M1 edited0001_init.sqlin place (allowed pre-release), butschema_migrationsalready recorded migration 1, so M1's tables were never created. notesd still started: it loggedno such table: versions/no such table: files, the vault looked registered, nothing was indexed, and search returned an Internal error.What
schema_migrationsgains achecksumcolumn: the SHA-256 of each migration's SQL, recorded when the migration is applied.store.Opencompares the recorded checksums with the embedded migrations. On a mismatch it fails, and the error names the database, the migration and the fix:checksumcolumn): the migrations are applied to an in-memory database and the twosqlite_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.DB.Txinstead of their ownBeginTx.0001_init.sqlheader anddocs/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.sqlalso 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.TestOpenMigratesAndReopensnow also checks the recorded checksums.make checkpasses, andgo test -racepasses oninternal/store.Not run:
make ui-devagainst a real stale database in nsl.🤖 Generated with Claude Code