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 start by exploring the repository structure to understand the codebase and locate relevant files. Let's first check the current directory and see what's there.
- Assistant
Let me understand the issue. The bug is about
orm.migrator.up()rewriting snapshot incorrectly. Let me look at the migrator code and understand the snapshot handling. - Assistant
Let me explore the migrations package and the migrator to understand how snapshots work.
- Assistant
Let me look at the Migrator.ts file to understand how
up()works. - Assistant
Now let me look at the AbstractMigrator to understand the
up()method and therunMigrationsflow: - Assistant
Let me check the reproduction repository to understand what the issue is:
- Assistant
Let me look at
DatabaseSchema.create()to understand how it reads the current schema from the database: - Assistant
Let me look at the DatabaseSchema.create method to understand how it reads the schema:
- Assistant
Now let me look at DatabaseTable's toJSON() to understand the serialization:
- Assistant
Let me check the toJSON method of DatabaseTable
- Assistant
Let me look at the
SqlSchemaGenerator.getTargetSchema()method: - Assistant
Let me look at
getTargetSchemato understand how the target schema is created from metadata: - Assistant
Let me look at how columns are populated during introspection:
- Assistant
Let me examine the SQLite schema helper to understand the issue:
- Assistant
Let me look at the test files for migrations to understand the expected behavior:
- Assistant
Let me look at the SQLite migration test to understand the snapshot behavior:
- Assistant
Let me look at the test that was already added for this:
- Assistant
Let me see more of this test:
- Assistant
Let me look at the test snapshot to understand what the expected snapshot format looks like:
- Assistant
Let me check git history for recent changes to the Migrator and snapshot-related code:
- Assistant
Let me look at the
runMigrationsoverride more closely - this is where the snapshot is written afterup(): - Assistant
Let me look at the full
runMigrationsoverride method: - Assistant
Let me look at the SQLite column introspection to see what data it returns:
- Assistant
Let me look at the getColumns method to understand what it returns:
- Assistant
Let me look more closely at the existing snapshot tests to understand what the actual difference is:
- Assistant
Now let me understand the problem better. Let me check if there's a reproduction in the repo:
- Assistant
Let me first check what tests exist and run the SQLite migration tests:
- Assistant
Let me check the test output for the relevant test:
- Assistant
Let me understand the issue better. Let me look at the repro repository linked in the issue:
- Assistant
Let me look at what happens during the
up()flow more carefully - the issue seems to be that whenmigrator.up()runs, it rewrites the snapshot usingDatabaseSchema.create()(introspection from DB) rather thangetTargetSchema()(from metadata). Let me verify this: - Assistant
Now I understand the issue. The problem is in the
runMigrationsoverride in Migrator.ts. Whenmigrator.up()runs, it:- Runs the migrations
- Creates a
DatabaseSchemaby introspecting the actual database usingDatabaseSchema.create() - Stores that as the snapshot
But when
migrator.create()is called, it stores the snapshot usinggetTargetSchema()(from entity metadata). These two approaches can produce different results, causing the snapshot to be "wrong" afterup().The fix: in
runMigrations, usegetTargetSchema()(metadata-based) instead ofDatabaseSchema.create()(introspection-based) when storing the snapshot, just likecreate()does. This ensures the snapshot is always consistent. - Assistant
Let me look at the full test to understand the exact issue better, and look at what the issue is specifically:
- Assistant
Let me now write a test to understand the actual issue. Let me first create a reproduction script:
- Assistant
Now I understand the issue. Let me trace the flow:
migrator.create()stores snapshot usingthis.#schemaGenerator.getTargetSchema()(metadata-based)migrator.up()callsrunMigrations()which overrides and stores snapshot usingDatabaseSchema.create()(database introspection)- The database introspection schema can differ from the metadata-based schema, causing incorrect snapshot rewriting
The fix should be to use
getTargetSchema()(metadata-based) when storing the snapshot afterup(), consistent with whatcreate()does. Let me make the change: - Assistant
Now I understand the problem clearly. When
migrator.up()runs, it:- Executes the migrations
- Reads the actual database via
DatabaseSchema.create()to get the snapshot - Stores that as the snapshot
But when
migrator.create()stores the snapshot, it usesgetTargetSchema()(from entity metadata).The issue is that
DatabaseSchema.create()reads from the actual DB via introspection, which can produce a different schema thangetTargetSchema()produces from metadata. This causes the snapshot to be rewritten incorrectly.The fix: after running
up(), usegetTargetSchema()to store the snapshot (same ascreate()does), rather thanDatabaseSchema.create(). This ensures consistency. - Assistant
Now I understand the core issue. The
runMigrationsoverride inMigrator.tsreads the database schema viaDatabaseSchema.create()(introspection) and stores it as the snapshot. Butcreate()stores the snapshot usinggetTargetSchema()(metadata-based). These produce different results, causing the snapshot to be incorrectly rewritten.The fix: in
runMigrations, usestoreCurrentSchema()without passing the introspected schema, so it usesgetTargetSchema()(same ascreate()). - Assistant
Let me look more carefully at the problem. The
runMigrationsoverride introspects the database viaDatabaseSchema.create()and stores that as the snapshot. Butcreate()usesgetTargetSchema(). These can differ because:DatabaseSchema.create()reads column types from the database, which may be normalized differently- Some metadata-only properties (like
nativeEnumName,ignoreSchemaChanges,defaultConstraint) may not be available from introspection
The fix: in
runMigrations, don't pass the introspected schema - just callstoreCurrentSchema()without arguments (same ascreate()does), which usesgetTargetSchema()from metadata.Wait, but what about
down()- when we revert migrations, we need the snapshot to reflect the current DB state. Let me think...Actually, for both
up()anddown(), after running migrations, the expected state of the DB is the metadata-derived schema. The snapshot should always matchgetTargetSchema()to ensure consistency. - Assistant
Now let me check if the removed
DatabaseSchema.create()import is still used elsewhere in the file: - Assistant
Good,
DatabaseSchemais still used in other places. Let me also check ifUtilsis still needed for the removedctxlogic: - Assistant
Utils is no longer used. Let me check if
MigrateOptionsis still needed: - Assistant
MigrateOptionsis still needed. Let me clean up unused imports (Utils): - Assistant
Let me remove unused
Utilsfrom imports: - Assistant
Now let me run the existing tests to make sure nothing is broken:
- Assistant
Good, all tests pass. Let me also run the other migration tests:
- Assistant
Postgres tests fail due to no connection (expected in this environment). Let me run the general Migrator test:
- Assistant
The postgres tests fail because there's no postgres connection available, and the Migrator.test.ts failures are unrelated to my change (they're about MongoDB setup issues). Let me check the Migrator.sqlite test is the one most relevant. It passes.
Let me now check if the unused imports need cleanup. I removed
Utilsimport - let me verify that it's no longer needed: - Assistant
Good,
Utilsis no longer used anywhere. Let me also check ifDatabaseTableis still used: - Assistant
Good,
DatabaseTableis still used. Let me also check ifMigrateOptionsis still needed: - Assistant
Good,
MigrateOptionsis still needed. Let me checkDatabaseSchemais still used: - Assistant
Good,
DatabaseSchemais still used elsewhere. Let me also run the SQLite runtime schema migration tests: - Assistant
Let me verify the final state of the file looks correct:
- Assistant
Good. Now let me verify the
runMigrationsmethod looks correct: - Assistant
Now let me run the full test suite for migration tests one more time to confirm:
- Assistant
All 25 tests pass. Let me also run the Migrator.test.ts (main test file) to check:
- Assistant
The Migrator.test.ts failures are pre-existing (MongoDB connection issues), not related to my change. Let me verify by checking if they fail on the original code too:
- Assistant
Confirmed - the failures are pre-existing and not related to my change. Let me restore my changes:
- Assistant
Good. Let me also run the GH7185 test and check any other migration-related snapshot tests:
- Assistant
That's a postgres connection error, not related to my change. Let me verify the diff is what we expect:
- Assistant
The diff looks clean and correct. Let me now think about edge cases and make sure the fix is complete. Let me look at what
storeCurrentSchemadoes when called without arguments: - Assistant
Let me see the full storeCurrentSchema method:
- Assistant
Good. When called without arguments,
storeCurrentSchema()usesthis.#schemaGenerator.getTargetSchema()which is the metadata-based schema - exactly whatcreate()uses. This is the correct fix.Let me run a broader set of migration-related tests to make sure nothing else breaks:
- Assistant
The MSSQL test failure is just a connection issue (no MSSQL server available). SQLite tests all pass.
Now let me verify my understanding of the issue and the fix is correct by looking at the core issue more carefully. Let me look at the
storeCurrentSchemamethod flow: - Assistant
Let me verify the complete file is correct:
- Assistant
The file looks good. Let me now verify that the
MigrateOptionstype is still needed (it is, since it's used in the method signature forrunMigrations).Let me now also check if there are some snapshot tests that depend on this behavior in other test files:
- Assistant
Let me check that comment in the sqlite test:
- Assistant
Let me check that comment - it mentions the old behavior:
- Assistant
That comment is about the old behavior. The test still passes because with blank migrations the result is the same either way. Let me also check what the postgres test references:
- Assistant
This is very interesting! The postgres test specifically tests that
DatabaseSchema.create()was causing a deadlock when running inside a transaction because it tried to acquire a separate pool connection. Our fix also resolves this issue because we no longer callDatabaseSchema.create()inrunMigrations().Let me verify the change makes sense end-to-end. Let me review the full diff one more time:
- Assistant
The change is clean and minimal:
- Removed
Utilsimport - no longer needed - In
runMigrations(): Instead of creating aDatabaseSchemavia introspection (DatabaseSchema.create()) and passing it tostoreCurrentSchema(schema), we now callstoreCurrentSchema()without arguments. This usesthis.#schemaGenerator.getTargetSchema()(metadata-based schema), which is the same as whatcreate()uses.
This ensures the snapshot after
up()/down()is always consistent with whatcreate()produces. Let me run the full SQLite tests one more time: - Removed
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.