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 start by exploring the repository to understand its structure and locate relevant files. Let's first check the current directory and see what's available.
- Assistant
Let me explore the issue description. The bug is about
orm.migrator.up()rewriting snapshot incorrectly. Let me first look at the reproduction repo link mentioned in the issue.Let me explore the packages directory structure to understand the codebase.
- Assistant
The issue is about
orm.migrator.up()rewriting snapshot incorrectly. Let me look at the reproduction repo to understand the issue better, and then examine the relevant migration code.Let me first check the reproduction repo to understand what's happening.
- Assistant
Let me look at the Migrator.ts to understand the up() method and how snapshots are handled.
- Assistant
Now let me look at the
AbstractMigratorto understand therunMigrationsmethod in the parent class: - Assistant
Let me look at the core migrator.
- Assistant
Now let me look at
DatabaseSchema.createto understand how the snapshot is created from the database: - Assistant
Let me look at the
DatabaseSchema.createmethod andstoreCurrentSchemalogic more carefully: - Assistant
Let me look at
getTargetSchemain SqlSchemaGenerator: - Assistant
Let me look at how DatabaseSchema is serialized and check for toJSON methods:
- Assistant
Let me look at the toJSON method.
- Assistant
Let me look at the
DatabaseTable.toJSONmethod: - Assistant
Let me look at the toJSON method of DatabaseTable to understand the snapshot format.
- Assistant
Let me continue reading the toJSON method.
- Assistant
Let me look at the reproduction repo to understand the issue. First, let me check git log:
- Assistant
Now let me look at the
createmethod of the Migrator and see how it stores the snapshot: - Assistant
Let me understand the key issue. The
runMigrationsmethod introspects the database withDatabaseSchema.create(), whilecreate/createInitialusegetTargetSchema(). Let me look at whatgetTargetSchema()returns: - Assistant
Now I understand the issue! The problem is in
Migrator.runMigrations():When
migrator.up()runs, after executing migrations, it introspects the actual database viaDatabaseSchema.create()and writes that to the snapshot. Butmigrator.create()usesgetTargetSchema()(metadata-derived schema). These can produce different results, leading to incorrect snapshot content afterup().The fix should be to use
getTargetSchema()instead ofDatabaseSchema.create()inrunMigrations(), since after running migrations, the database state should match the entity metadata.Let me first create a reproduction script to verify this:
- Assistant
Let me check if there are existing migration-related tests to understand the pattern:
- Assistant
Let me search for migration tests.
- Assistant
Let me look at the SQLite migrator test since the issue mentions SQLite:
- Assistant
Let me look at the test at line 103 more carefully to understand the existing behavior:
- Assistant
Now I understand the issue. In
Migrator.runMigrations(), after running migrations, it introspects the actual database viaDatabaseSchema.create()and stores that as the snapshot. However, whenmigrator.create()writes the snapshot, it usesgetTargetSchema()(which derives the schema from entity metadata). These two approaches produce different results, causing the snapshot to be incorrectly rewritten afterup().The fix is to call
storeCurrentSchema()without passing a schema inrunMigrations(), so it usesgetTargetSchema()consistently (same ascreate()andcreateInitial()).Let me verify by looking at the exact code:
- Assistant
Now I understand the issue clearly. The
runMigrationsmethod creates a snapshot usingDatabaseSchema.create()(which introspects the actual database), whilecreate()andcreateInitial()usegetTargetSchema()(which derives from entity metadata). After running migrations, the introspected database schema can differ from what the metadata describes, causing the snapshot to be incorrectly rewritten.The fix is to make
runMigrationsusegetTargetSchema()consistently, just likecreate()andcreateInitial()do. Let me make the change: - Assistant
Let me now look at the exact code I need to change:
- Assistant
Now I understand the issue. In
runMigrations, the snapshot is being created by introspecting the actual database viaDatabaseSchema.create(). But whencreate()stores the snapshot, it callsstoreCurrentSchema()with no argument, which falls back tothis.#schemaGenerator.getTargetSchema()(the metadata-derived schema). The introspected DB schema can differ from the metadata-derived schema, causing the snapshot to be rewritten incorrectly.The fix is simple: in
runMigrations, callstoreCurrentSchema()without passing a schema argument, so it usesgetTargetSchema()consistently, just likecreate()andcreateInitial()do. - Assistant
I see there's a duplicate closing brace. Let me fix that:
- Assistant
Good, the file looks correct. Now let me also check if we have unused imports after the change. The
DatabaseSchema.createimport is still needed invalidateInitialMigration. But thectxvariable and relatedMigrateOptionsimport check can be simplified. Let me verify the imports: - Assistant
Good,
DatabaseSchemais still used elsewhere. Let me also check ifMigrateOptionsis still used: - Assistant
Good, all imports are still used. Now let me run the existing tests to make sure the change doesn't break anything:
- Assistant
All 24 tests pass. Let me also run the general Migrator tests and postgres tests to be thorough:
- Assistant
The failures are pre-existing test setup issues (not related to my change). Let me also check the postgres test:
- Assistant
Those failures are due to missing Postgres connection, not related to our change. Let me look at the specific test about snapshot stability:
- Assistant
Let me also look at the second snapshot consistency test:
- Assistant
The tests confirm that the snapshots from
create()andup()should be identical. Since both now usegetTargetSchema(), they'll produce the same output. Let me verify the change looks correct: - Assistant
Let me verify the full file compiles properly by checking that all imports are still used:
- Assistant
Let me run the SQLite migration tests one more time and also check the MSSQL tests:
- Assistant
Let me also run the mssql tests:
- Assistant
MSSQL test fails due to no database connection (not related to my change). Let me also verify the
DatabaseTableimport is still used somewhere: - Assistant
Good, all imports are still used. Let me see the full diff:
- Assistant
The diff is clean. Let me also check if there are any similar patterns in the MongoDB migrator:
- Assistant
Let me check if the MongoDB migrator has a similar pattern:
- Assistant
Good, MongoDB migrator doesn't have this pattern (since it doesn't use SQL schemas). Let me verify that
DatabaseSchemais still needed (it's used invalidateInitialMigrationandgetSchemaFromSnapshot): - Assistant
Now let me run the full migration-related test suite to make sure everything passes:
- Assistant
Let me get the full test results:
- Assistant
Let me see which test files passed and which failed:
- Assistant
Let me verify that the
Utilsimport is still needed after removing theUtils.isObjectcall: - Assistant
Utilsis no longer used in the file. Let me check if it's imported but unused now. - Assistant
Utilsis imported but no longer used. Let me also verify ifDatabaseSchemaandDatabaseTableare still needed: - Assistant
DatabaseSchemaandDatabaseTableare still used.Utilsis no longer needed. Let me remove it: - Assistant
I need to remove the unused
Utilsimport. Let me do that. - Assistant
Let me run the SQLite tests once more to confirm everything still works after cleaning up the import:
- Assistant
All 24 tests pass. Let me see the final diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.