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
I'll investigate the snapshot rewrite bug during
orm.migrator.up()by exploring the reproduction repo and the migrator/snapshot code in this codebase. - Assistant
The snapshot is rewritten from the live DB after
migrator.up(), which likely drops column comments that metadata preserves — I'll trace that path next. - Assistant
Implementing the fix: SQLite doesn't persist column comments, so introspection after
migrator.up()drops them — I'll merge metadata-only fields from the target schema when storing snapshots. - Assistant
Now I'll add a regression test modeled on the reproduction repo.
- Assistant
Fixed the snapshot rewrite bug during
orm.migrator.up().Root cause
Snapshots are written from two sources:
migration:create— from entity metadata (getTargetSchema())migration:up/migration:down— from database introspection (DatabaseSchema.create())
SQLite does not persist column comments in the database. After
migrator.up(), introspection rewrote the snapshot with"comment": nullinstead of"comment": "free-form notes", which caused:- A noisy
git diffon the snapshot file - Spurious migrations on the next
migration:create(SQLite tried to rebuild the table to “apply” the comment change)
Fix
- Added
supportsPersistedColumnComments()toSchemaHelper(returnsfalseon SQLite) - Added
DatabaseSchema.enrichFromMetadata()to copy metadata-only fields (column comments,ignoreSchemaChanges, table comments) from the target schema - Added
SqlSchemaGenerator.enrichSnapshotSchema()to apply enrichment when comments aren’t persisted
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.