orm.migrator.up() rewrites snapshot incorrectly Describe the bug When we run…
- User
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.
- Assistant
Investigating the snapshot rewrite bug during
orm.migrator.up()— I'll explore the reproduction repo and the migrator/snapshot code in this workspace. - Assistant
Running the reproduction locally to confirm the issue.
- Assistant
Implementing enrichment of introspected schema with metadata-only fields (e.g. SQLite column comments) before storing the snapshot.
- Assistant
Root cause
After
orm.migrator.up()/down(), the snapshot was rewritten from database introspection (DatabaseSchema.create()). On SQLite, column comments are not stored in the database, so introspection always producedcomment: null, even thoughmigration:createwrote the snapshot from entity metadata (which includes comments).That mismatch is exactly what the repro shows: the
notescolumn comment flips from"free-form notes"tonull.Fix
-
Added
DatabaseSchema.enrichFromTarget()to copy metadata-only details (column/table comments,ignoreSchemaChanges) from the target schema onto the introspected schema for matching tables/columns. -
In
Migrator.runMigrations(), callschema.enrichFromTarget(this.#schemaGenerator.getTargetSchema())before storing the snapshot. Introspection still reflects the real DB structure (tables/columns added or removed by migrations), while metadata-only annotations are preserved. -
Added a regression test in
tests/features/migrations/GH7798.test.ts.
Verified against the reproduction repo: without the fix, becomes after ; with the fix, the snapshot stays unchanged.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.