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 relevant tests, then reproduce the unintended rewrite with the smallest available fixture before changing implementation.
- Assistant
rgis unavailable, so I’ll usefind/grepto map the migrations package and identify every snapshot mutation call, especially anything invoked byup(). - Assistant
The changelog shows recent, potentially conflicting behavior changes (“skip snapshot write on up” followed by “normalize snapshots and write on both up/down”). I’ll inspect
Migrator.ts, its history, and migrations tests to determine intended semantics and isolate whyup()serializes a different schema. - Assistant
runMigrations()always introspects the live database after either direction and overwrites the metadata-derived snapshot. That explains broad drift on SQLite (introspection cannot preserve every metadata detail). I’ll inspect the commits that introduced this and current assertions around up/down so the fix preserves the rollback normalization requirement without restoring obsolete behavior accidentally. - Assistant
The regression is direct:
up()changed from intentionally not touching the already-current snapshot to replacing it with lossy database introspection. The newly added tests codify the regression rather than the invariant (one PostgreSQL assertion even explicitly expects drift). I’ll try fetching the provided minimal reproduction to confirm its exact diff, then implement the narrow direction guard: preserve snapshots onup(), refresh only after successfuldown(). - Assistant
The reproduction snapshot contains a column comment, a strong example of metadata information SQLite introspection cannot recover. I’ll run its script with the reported dependency line and inspect the resulting Git diff to verify the live failure before patching.
- Assistant
The reported failure is reproduced exactly:
up()changes the valid metadata comment tonull. The general fix is to avoid introspection-based snapshot writes afterup()because a snapshot records the schema target at migration-generation time and is already current; onlydown()makes the database state diverge from that target and needs a refreshed snapshot. I’ll add that guard while retaining transaction-context introspection and read-only handling for rollback. - Assistant
Python is not installed in this environment, so I’ll apply the same focused source replacement using the repository’s Node runtime, then inspect formatting and diff.
- Assistant
I’ll now run the focused SQLite migrator suite. I expect legacy assertions introduced with the regression (“up should call storeCurrentSchema”) to fail, but all functional generation/down behavior should remain sound; this run will also reveal any unintended effects.
- Assistant
The test command exceeded the terminal’s short per-command limit before Vitest emitted buffered output. I’ll launch it in the background, poll its output, and avoid the unsupported Jest-only
--runInBandoption. - Assistant
The shell job was stopped when the transient terminal closed. I’ll detach it with
nohupand then poll the result file. - Assistant
I’ll check whether the detached focused suite completed and inspect its complete failure summary.
- Assistant
The focused suite confirms 23/24 behaviors pass; the sole failure is the obsolete assertion that explicitly requires the buggy write. To verify the actual reproduction against this source (not merely conceptually), I’ll inspect workspace export conditions/build setup and run a temporary non-test script through the monorepo’s normal TypeScript resolution, removing it afterward.
- Assistant
The workspace exports source directly, so I can execute the provided reproduction from a temporary directory inside the monorepo. I’ll copy only its source, run with
tsxagainst the modified workspace packages, compare the snapshot byte-for-byte, and delete the temporary files. - Assistant
That temporary run did not reach the migrator because it omitted the reproduction’s legacy-decorator
tsconfig; the unchanged hash is therefore not evidence. I’ll rerun with the providedtsconfig.jsonexplicitly, ensuring migration execution succeeds before checking the hash. - Assistant
The reproduction completed successfully (
STATUS=0) and the snapshot hashes are identical, confirming the comment is preserved with the fix. The shell’s final nonzero status was only due to an earlier stopped background job, not the script. Next I’ll run the migrations package TypeScript build and a focused lint check. - Assistant
The package compiles and the changed file passes type-aware lint. I’ll do one final diff/status review to ensure only the intended guard and explanatory comment will be submitted and no temporary reproduction artifacts remain.
- Assistant
Checklist review:
- If an error was reported in the issue description, I have successfully reproduced it. Yes. Running the provided repository with the published dev version changed the SQLite
notes.commentsnapshot field from"free-form notes"tonull. - If an error was reported in the issue description, I have confirmed that my fix resolves the error. Yes. I reran the provided source against the modified workspace; migration execution succeeded and the snapshot SHA-256 remained byte-for-byte identical.
- I have thought about edge cases and made sure my fix handles them as well. Yes. The guard still requires a successful migration result and enabled snapshots, preserves
down()refreshes (including external transaction context), and leaves its read-only-filesystem error handling intact.up()no longer performs lossy or redundant introspection.
- If an error was reported in the issue description, I have successfully reproduced it. Yes. Running the provided repository with the published dev version changed the SQLite
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.