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.
Interface
No new interfaces are introduced.
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 the migrator snapshot update paths and relevant tests. Then I’ll reproduce the behavior using the closest existing test fixture, patch the underlying snapshot handling, and run focused tests plus any relevant broader suite.
- Assistant
rgis unavailable, so I’ll usefind/grepto identify migration package files and references to snapshot writes andup(). - Assistant
The changelog shows recent snapshot normalization changes, suggesting the regression is likely in the post-
upsynchronization logic. I’ll inspectMigratorand the abstract runner, then review git history around those changes for intended semantics. - Assistant
runMigrations()currently replaces the metadata-generated snapshot with a database-introspected schema after every migration. The report’s “bunch of stuff wrong” likely reflects lossy SQLite introspection. I’ll compare the introducing commit and its tests to determine whether snapshots should be normalized without replacing their semantic metadata, especially onup(). - Assistant
The history confirms the prior intended behavior:
up()should not write becausecreate()already stores the authoritative target snapshot; onlydown()must refresh to match a reverted database. The later normalization change re-enabled the redundant introspection write and introduced this regression. I’ll inspect focused assertions and the reproduction repository (network permitting) to verify which SQLite fields are altered and ensure that restoring thedown-only behavior handles the exact case. - Assistant
The current tests were added with the regressing change and explicitly encode the now-invalid “up writes” behavior; per instructions I won’t modify tests. I can still reproduce externally. I’ll clone the linked minimal repository outside this repository, inspect its scripts/configuration and snapshot, then run its migration with this workspace package resolution if practical—or at minimum compare its committed before/after evidence.
- Assistant
The reproduction is available. I’ll inspect its migration flow and committed snapshot, install its pinned lockfile dependencies, run
migrate, and capture the exact diff. This establishes the bug independently before making the minimal behavioral correction. - Assistant
The exact reproduction is confirmed: SQLite cannot recover column comments from the database, so introspection changes
"free-form notes"tonull. The correct general fix is not to special-case comments, but to avoid replacing the authoritative metadata snapshot onup()at all. I’ll restoredown-only refresh while retaining the current transaction-context handling and read-only filesystem guard. - Assistant
The prior shell command changed the working directory to the temporary reproduction, and Python is unavailable. I’ll explicitly return to the repository and use the provided editor command to make the targeted change safely.
- Assistant
I’ll replace the condition and add a concise rationale documenting why
up()must preserve metadata-only details whiledown()still requires synchronization. - Assistant
The fix is in place. The existing “serialization is stable” test exercises create→up and should now pass because the snapshot remains byte-for-byte authoritative; the obsolete spy test expecting an up-write will intentionally conflict with corrected semantics. I’ll run the focused stability test first, then type/lint checks for the changed package.
- Assistant
The focused Vitest invocation exceeded the terminal’s short per-command timeout before producing results. I’ll run it in the background with output redirected, then poll for completion without losing the process.
- Assistant
The shell stopped the background job due to terminal input handling. I’ll restart it detached with stdin closed, then poll its exit marker.
- Assistant
I’ll check whether the detached focused test has completed and inspect its output.
- Assistant
Vitest’s global setup is downloading/starting shared test services even for SQLite, but the test is progressing. I’ll poll once more.
- Assistant
The corrected create→up stability behavior passes. Next I’ll run the full SQLite migrator test file to identify all effects; I expect only the assertion introduced by the regressing commit (“up and down both write”) to fail, while behavior-oriented tests should pass. I’ll also run the migrations package build/type check.
- Assistant
Both checks are detached and running; I’ll poll their exit markers and inspect output.
- Assistant
As expected, 23/24 tests pass; the sole failure is the stale implementation-detail assertion that
up()must invoke the writer, which directly contradicts this issue’s required behavior. The read-only, stability, and down-related behavior tests pass. I’ll wait for the package build and then verify the linked reproduction by applying the equivalent one-line behavior to its installed package (outside the submitted repository), rerunning, and confirming a clean snapshot diff. - Assistant
The migrations workspace build passes. I’ll now reset the external reproduction snapshot/database state, patch only its installed compiled condition to match the source fix, rerun the exact reported command, and check that the committed snapshot remains unchanged.
- Assistant
The exact linked reproduction now succeeds with a clean snapshot diff, confirming the fix resolves the reported comment loss. I’ll perform final repository hygiene checks, run formatting/lint on the changed file, and inspect the final diff for unintended generated changes.
- Assistant
Final verification and checklist:
- Successfully reproduced the reported error: Yes. Running the linked repository’s
npm run migratechanged the SQLite snapshot column comment from"free-form notes"tonull. - Confirmed the fix resolves the error: Yes. Applying the corrected
down-only snapshot refresh behavior to the linked reproduction produced a cleangit diffafter the same migration command. - Considered edge cases: Yes. Snapshot refresh remains enabled after successful
down()operations, including transaction context propagation and the read-only filesystem guard. No snapshot work occurs when no migrations ran or snapshots are disabled. - Ran affected existing tests: Yes. The focused snapshot stability test passed; the full SQLite migrator file had 23 passing tests and one expected stale failure that explicitly asserts the regressed behavior (“up and down both write”). Per instruction, I did not modify existing tests. The migrations workspace build passed, targeted type-aware lint reported zero issues, and passed.
- Successfully reproduced the reported error: Yes. Running the linked repository’s
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.