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 update code and related tests. Then I’ll reproduce the behavior with the most focused existing test setup, trace why
up()mutates the snapshot incorrectly, implement a general fix, and run the relevant test suites. - Assistant
rgis unavailable, so I’ll usefindandgrepto identify the migrator implementation and snapshot tests without installing unnecessary tooling. - Assistant
I found snapshot reads/writes concentrated in
packages/migrations/src/Migrator.ts. I’ll inspect the full lifecycle around migration creation and execution, then search test fixtures for snapshot assertions to understand intended semantics. - Assistant
There are already focused tests for snapshot rewriting, including a MySQL regression whose comments explicitly compare metadata-generated snapshots to
up()’s database-introspected rewrite. I’ll inspect those and the SQLite snapshot tests/diffs; this should reveal which fields are unstable and whether the correct policy is to avoid rewriting on execution or to normalize introspection. - Assistant
The current HEAD already contains a recent MySQL stabilization fix, but the reported reproduction is SQLite and likely exposes a broader design flaw that normalization cannot reliably solve. I’ll fetch the linked minimal reproduction (if network access permits) and inspect its committed snapshot before/after script, then run it against this checkout or recreate its entities locally.
- Assistant
The reproduction is available. I’ll inspect its exact entity, migration, and expected snapshot, then run the migrate command and capture a structured diff. This directly satisfies reproduction and should identify all corrupted fields rather than guessing.
- Assistant
The bug is reproduced exactly: SQLite cannot introspect the metadata-only column comment because SQLite DDL does not persist it, so rebuilding the snapshot from the live database necessarily destroys information. This cannot be fixed by introspection normalization. I’ll inspect the history that introduced execution-time snapshot writes and the migration API’s option semantics, to choose a fix that preserves generated snapshots while still handling
down()and migrations created elsewhere correctly. - Assistant
Execution-time rewriting was added very recently to support deleting/updating stale snapshots after rollback. I’ll examine those commits in detail, because the robust solution may be asymmetric:
up()should preserve the snapshot produced during migration creation, whiledown()must invalidate it rather than trying to reconstruct lossy metadata from the database. - Assistant
The history confirms an earlier implementation already used the sound asymmetric rule: do not write on
up()becausecreate()has the authoritative metadata snapshot; refresh afterdown()because state diverges. The current “normalize everything” approach cannot preserve non-database metadata such as SQLite comments. I’ll restore that rule while retaining transaction-aware introspection and read-only handling fordown(). I’ll also check whether imports become unused and run formatting/type checks plus focused tests. Since modifying tests is prohibited, I will validate the exact external reproduction via a temporary linked-package setup or an equivalent temporary script. - Assistant
I’ll now change the execution hook so only successful rollbacks refresh snapshots. The comment will document the correctness reason (database introspection is lossy), not just read-only filesystem concerns. This leaves
down()behavior intact, including transaction context and best-effort writes. - Assistant
python3is not installed and no file changed. I’ll use the repository’s providedreplaceeditor command for the same targeted edit, then inspect the diff. - Assistant
The editor located the text but requires navigating the open file first. I’ll go to that location and apply the replacement in the current editor context.
- Assistant
The implementation is now changed. To verify the exact issue, I’ll temporarily copy this source file into the reproduction’s installed
@mikro-orm/migrationspackage, restore its committed snapshot, runmigrator.up(), and confirmgit diffremains clean. I’ll then compile the migrations package and run focused repository tests that assert snapshot stability and rollback behavior. - Assistant
Two environment issues interrupted validation: the published package does not include a
src/directory at that path, and Corepack attempted an interactive Yarn download. I’ll inspect the installed package layout and use the repository’s already-installed tooling directly (or disable the Corepack prompt) to avoid interactive commands. - Assistant
The reproduction uses compiled
Migrator.js, so I’ll patch only that temporary installed file with the samemethod === 'down'guard and rerun. For repository checks, I’ll invoketscandvitestbinaries directly, bypassing Corepack entirely. - Assistant
The exact reproduction now passes: after applying the migration, the external repository has a clean
git statusand byte-identical snapshot. The migrations package also type-checks successfully. I’ll run the full SQLite migrator test file to detect regressions; one legacy expectation thatup()invokes the writer is expected to conflict with the corrected behavior, but all functional tests should otherwise pass. - Assistant
I accidentally passed a nonexistent Vitest option; no tests ran. I’ll rerun with only supported arguments.
- Assistant
The full suite exposed an important lifecycle edge case in the existing contract: after
down()refreshes the snapshot to the reverted database state, a subsequentup()must restore the current target snapshot. Simply skipping allup()writes would leave that rollback snapshot stale. The correct fix is therefore source-sensitive:up(): write the authoritative target schema from entity metadata, preserving comments and other non-introspectable information.down(): introspect the reverted database state, since it no longer matches current metadata.
This also preserves the existing expectation that both operations update snapshots. I’ll revise the implementation accordingly and rerun the reproduction and full SQLite migrator tests.
- Assistant
The revised implementation compiles cleanly. I’ll run the full SQLite migrator tests now; they exercise both writer call counts, stability between create/up, read-only behavior, transaction-adjacent flow, and down refresh.
- Assistant
All 24 SQLite migrator tests pass. I’ll perform one final exact-reproduction verification with the final strategy (metadata target on
up, introspection only ondown) by patching the temporary compiled package accordingly. Then I’ll inspect the final diff and repository status for unintended generated files. - Assistant
The exact external reproduction passes with the final implementation:
migrator.up()completes and the committed snapshot remains byte-identical with a clean Git status. The repository diff contains only the intended source change. I’ll run the focused PostgreSQL external-transaction snapshot test because this code also handles transaction context, and lint the changed file. - Assistant
Validation summary and requested checklist:
- If an error was reported, I successfully reproduced it. I cloned the linked repository, ran
npm run migrate, and observed the snapshot’snotes.commentchange from"free-form notes"tonull. - If an error was reported, I confirmed the fix resolves it. I ran the same external reproduction with the final logic; the migration succeeded,
git statuswas clean, and the snapshot remained byte-identical. - I thought about edge cases.
up()now serializes the authoritative metadata target, preserving details databases cannot introspect;down()still introspects the reverted database. This also handlesdown()followed byup(), read-only filesystems, no-op migration runs, disabled snapshots, and transaction-aware rollback introspection.
- If an error was reported, I successfully reproduced it. I cloned the linked repository, ran
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.