ISSUE DESCRIPTION orm.migrator.up() rewrites snapshot incorrectly Describe the bug When…
- User
ISSUE DESCRIPTION
orm.migrator.up()rewrites snapshot incorrectlyDescribe the bug
When we run
orm.migrator.up(), the snapshot changes and a bunch of stuff in the snapshot is wrong and we have to revert the changes.Reproduction
https://github.com/zachkirsch/mikro-orm-migrate-snapshot-repro
What driver are you using?
@mikro-orm/sqlite
MikroORM version
7.1.2-dev.10
Node.js version
v24.0.1
Operating system
macOS
Validations
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord.
- The provided reproduction is a minimal reproducible example of the bug.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. - Assistant
I’ll first inspect the repository structure and locate migrator snapshot read/write paths and related tests. Then I’ll reproduce the snapshot rewrite behavior with the narrowest existing test fixture or a temporary script, patch the underlying serialization/metadata issue, and run targeted tests plus relevant broader checks.
- Assistant
rgis unavailable, so I’ll usefind/grep. The repository is already dependency-installed and clean, which should make targeted test execution straightforward. - Assistant
The changelog indicates recent work on stable snapshot serialization, so this checkout may contain adjacent fixes or a partial implementation. I’ll inspect
Migrator.up(), snapshot hydration/serialization, schema JSON methods, and recent history to identify the regression precisely rather than applying a broad “don’t write” workaround. - Assistant
The current
runMigrationsexplicitly introspects the live database after every successful up/down and rewrites the snapshot. The reported corruption likely comes from round-trip differences between metadata-built snapshots (created during migration generation) and SQLite introspection. I’ll clone the linked minimal reproduction into/tmp, inspect its entities/migration/snapshot, and run it against this checkout if practical. - Assistant
The repro exposes the key semantic problem: the metadata snapshot records
notes.comment, but SQLite DDL does not preserve column comments, so live introspection cannot reconstruct it. Replacing a generation snapshot with an introspection snapshot necessarily loses information. It may also add the migration tracking table. I’ll run the original repro as-is to capture the exact diff, then inspect historical commits around the up/down snapshot rewrite to determine the intended lifecycle behavior. - Assistant
The exact bug is reproduced:
up()changes"comment": "free-form notes"tonull. This is not safely fixable by SQLite normalization because comments are intentionally metadata-only and needed for future diffs. More generally,up({ to })can represent only a partial migration state while the generation snapshot represents the latest migration, making any live rewrite on up conceptually wrong. The earlier behavior already encoded the correct invariant: generation writes the target snapshot; only rollback must refresh it. I’ll inspect current migration tests for assumptions and adapt source only (per instruction, no test edits), then run the relevant suite. - Assistant
I’ll restore the lifecycle guard while retaining the newer transaction-context handling and read-only-filesystem protection for
down(). This is a minimal source-only change:up()will no longer introspect or write the snapshot;down()still must do so because rollback changes the expected schema and there is no historical snapshot per migration. - Assistant
Python is not installed in this environment, so no source change occurred. I’ll apply the same replacement with the repository editor command and immediately inspect the diff.
- Assistant
The diff is clean and preserves all rollback behavior. I’ll now run the targeted SQLite migrator suite. I expect legacy assertions specifically requiring an up-write to fail because they encode the behavior being corrected; all unrelated cases should pass. I’ll also run migrations package type/build checks and then verify the linked reproduction against local package builds/symlinks.
- Assistant
The test command timed out because Corepack attempted an interactive Yarn download, not because of the code. I’ll bypass Yarn and invoke the already-installed Vitest binary directly. I’ll first check for any orphaned process and then run only the target file with an increased command timeout unavailable tool? Tool fixed likely 10 sec. Tests may under 10 perhaps command timed out due prompt. run direct.
- Assistant
The targeted suite confirms 23/24 pass; the sole failure is the obsolete test asserting that
up()must callstoreCurrentSchema, exactly the behavior this issue requires removing. Importantly, the existing serialization equality tests and all migration execution/down tests pass.To verify the actual repro rather than relying on call counts, I’ll build the affected workspace package(s), point the cloned repro’s MikroORM dependencies at this checkout’s workspace packages (or use their built output), restore its original snapshot, run
up(), and compare bytes before/after. First I’ll inspect workspace package export/build configuration and current dist state. - Assistant
Workspace exports point directly to TypeScript source in development, so no build is needed for the reproduction. I’ll replace only the cloned repro’s installed
@mikro-orm/*package directories with symlinks to this checkout, restore the snapshot, run migration, and perform a byte-for-byte comparison. - Assistant
The linked reproduction now passes exactly: migration applies successfully and the snapshot remains byte-for-byte unchanged, preserving the SQLite column comment.
Next I’ll run TypeScript compilation for the migrations package directly (avoiding Corepack), then run focused tests that cover rollback snapshot refresh and no-op behavior. Since the legacy combined test aborts before its down assertion, I’ll use existing test-name filters for the read-only/down path and relevant serialization tests; no tests will be modified.
- Assistant
A deeper lifecycle edge case needs checking before finalizing: after
down(), the snapshot is refreshed to the rolled-back database state; a subsequentup()must restore the generated target snapshot. Simply skipping all up writes would leave it stale. The safe distinction is likely not “never write on up,” but “never replace it with live introspection on up”: rebuild from current entity metadata instead, preserving comments and the intended latest target even for partial database migration states. I’ll inspectstoreCurrentSchema()and schema-generation semantics, then adjust if confirmed. - Assistant
Confirmed:
storeCurrentSchema()without an argument serializesgetTargetSchema()from entity metadata. The robust lifecycle is therefore:up: write the metadata target (preserves comments and represents the latest generated target, including after a prior down).down: introspect the rolled-back live database (target no longer represents current rollback state).
This also retains compatibility with the existing expectation that a successful up refreshes a stale snapshot, while eliminating lossy SQLite introspection and unnecessary database schema reads on up. I’ll implement this branch explicitly and rerun the full targeted suite and linked repro.
- Assistant
The editor replacement left one closing brace from the prior implementation at line 230, and the compiler correctly caught it. I’ll remove that duplicate brace, rerun compilation, then execute the full SQLite migrator test file. With metadata writes on up, the previously failing legacy call-count test should now pass too.
- Assistant
Compilation now succeeds and all 24 SQLite migrator tests pass, including up/down write behavior, read-only handling, and serialization stability. I’ll rerun the external reproduction with the finalized metadata-write approach, run formatting/lint checks on the changed file, and inspect the final diff/status for unintended files.
- Assistant
One subtle semantic regression in the draft is that moving rollback introspection inside the existing
trywould silently swallow database introspection errors, whereas the original catch covered only snapshot file writes. I’ll preserve the original error behavior by computing the down schema before the writetry, while selecting metadata (undefined) for up. Then I’ll perform the final compile and targeted suite once more. - Assistant
The final implementation compiles, is correctly formatted, and all 24 targeted SQLite migration tests pass. The external reproduction had already confirmed metadata serialization leaves the snapshot unchanged; this last refactor only restores error boundaries without changing up behavior. I’ll do one final lint/diff review, then submit with the requested checklist accounting in the final summary.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.