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 structure to understand what we're dealing with. Let's first look at the current directory.
- Assistant
Let me explore the repository structure and understand the issue better.
- Assistant
Let me explore the packages directory structure to understand the monorepo layout.
- Assistant
The issue is about
orm.migrator.up()rewriting the snapshot incorrectly. Let me look at the migration-related packages, especiallypackages/migrations. - Assistant
Let me look at the Migrator.ts file to understand how
migrator.up()works. - Assistant
Let me look at the full file to understand how the migrator works, particularly the
upmethod. - Assistant
Let me see more of the file, especially the
runMigrationsandstoreCurrentSchemamethods. - Assistant
Let me look at the
storeCurrentSchemamethod and how the snapshot is written. - Assistant
Let me look at the rest of the file and then check the repro repository to understand the specific issue.
- Assistant
Now let me look at the
DatabaseSchemaclass in the@mikro-orm/sqlpackage to understand the serialization: - Assistant
Let me find the DatabaseSchema class.
- Assistant
Let me look at the DatabaseSchema class.
- Assistant
Let me look at the
createstatic method andtoJSONmethod. - Assistant
Let me look at the
createmethod. - Assistant
Let me look at the
toJSONmethod and how the schema is serialized. - Assistant
Let me look at the toJSON method.
- Assistant
Now let me look at DatabaseTable's toJSON method, since it's key to the snapshot format.
- Assistant
Let me look at the
toJSONmethod of DatabaseTable. - Assistant
Let me continue reading the toJSON method.
- Assistant
Now let me understand the issue. The bug says that when running
orm.migrator.up(), the snapshot gets rewritten incorrectly. Let me look at the core AbstractMigrator to understand theup()flow: - Assistant
Let me look at the
up()method in AbstractMigrator. - Assistant
Let me find the
up()method in AbstractMigrator. - Assistant
Let me look at the
up()method and therunMigrationsmethod. - Assistant
Let me find the
runMigrationsmethod in AbstractMigrator. - Assistant
Let me read the runMigrations method.
- Assistant
Let me now understand how the snapshot should be generated. Let me look at
getTargetSchema()from the SqlSchemaGenerator: - Assistant
Let me look at SqlSchemaGenerator.
- Assistant
Let me look at
getTargetSchemato understand what it returns. - Assistant
Let me look at the reproduction repo mentioned in the issue to understand the problem better:
- Assistant
Let me look at the migration-related tests to understand how snapshots work:
- Assistant
Let me look at the GH7796 test since it's related to the migrator.up snapshot issue:
- Assistant
Now I understand the issue! The problem is in
Migrator.runMigrations()(line 197-225). After running migrations viaup(), it regenerates the snapshot by introspecting the actual database withDatabaseSchema.create(). But whencreate()generates the snapshot (line 114), it usesgetTargetSchema()which derives from entity metadata. These two sources produce slightly different results, causing the snapshot to "flip-flop" between the two.The fix: in
runMigrations(), we should usestoreCurrentSchema()without a schema argument, which defaults togetTargetSchema()(entity metadata), just likecreate()does.Let me first run the test to confirm it fails:
- Assistant
The test requires MySQL running. Let me understand the issue better by looking at the code flow more carefully. The issue is clear from the test expectations:
createInitial()writes the snapshot using entity metadata (getTargetSchema()), butup()rewrites it using database introspection (DatabaseSchema.create()), causing differences.Let me apply the fix:
- Assistant
Now I see the issue clearly:
- In
create()andcreateInitial(),storeCurrentSchema()is called without an argument, so it usesthis.#schemaGenerator.getTargetSchema()— the schema derived from entity metadata. - In
runMigrations()(called byup()),storeCurrentSchema(schema)is called with a schema fromDatabaseSchema.create()— the schema derived from database introspection.
These two sources produce slightly different results, causing the snapshot to flip-flop. The fix is to not pass the introspected schema to
storeCurrentSchema()inrunMigrations(). - In
- Assistant
Let me use the proper edit syntax:
- Assistant
Good. Now let me check if there are now-unused imports:
- Assistant
Utilsis no longer used in the file. Let me also checkMigrateOptions: - Assistant
Good,
MigrateOptionsis still used. Let me removeUtilsfrom the imports since it's no longer needed: - Assistant
The heredoc needs a closing marker. Let me use a different approach.
- Assistant
Now let me remove the unused
Utilsimport: - Assistant
Let me verify the full file to make sure it looks good:
- Assistant
Good. The fix looks clean. Now let me check if
DatabaseSchemaandDatabaseTableare still used elsewhere in the file: - Assistant
Good,
DatabaseSchemais still used invalidateInitialMigration,getSchemaFromSnapshot, andstoreCurrentSchema.DatabaseTableis also still used. Let me verifyDatabaseTableis used: - Assistant
Good, both are still used. Now let me run the existing migration tests to make sure the change doesn't break anything. First, let me see what test infrastructure is available:
- Assistant
Let me run the SQLite migrator tests since the issue uses @mikro-orm/sqlite:
- Assistant
All SQLite migration tests pass. Let me also run the main Migrator tests:
- Assistant
These failures are all MySQL connection failures (port 3308), not related to my changes. Let me run the sqlite migration runtime schema test:
- Assistant
Let me look at the SQLite migrator test to see if it tests snapshot behavior after
up(): - Assistant
Let me look at the specific snapshot-related test:
- Assistant
Good. The test verifies that
storeCurrentSchemais called duringup()anddown(). My change doesn't affect that - it still callsstoreCurrentSchema(), just without passing the introspected schema. Let me also run the other snapshot-related tests: - Assistant
Let me also run the GH7773 test:
- Assistant
That's a PostgreSQL connection error, not related to my changes. Let me run the full sqlite test suite to make sure everything is still working:
- Assistant
All SQLite migration tests pass! Let me look at the test "snapshot from create and up have identical column fields and consistent primary flag (GH #7234)" which is directly related to this issue:
- Assistant
Let me look at this test to understand what it does.
- Assistant
Let me also look at the GH #7607 test which expects the snapshots to be identical:
- Assistant
Let me look at the GH #7607 test:
- Assistant
Let me run the full SQLite migration tests again to confirm everything passes with the fix:
- Assistant
All 24 tests pass. Let me also run the postgres migration tests:
- Assistant
All the failures are database connection errors (no PostgreSQL server running), not related to my changes. Let me verify the final state of the file:
- Assistant
Let me check the full diff of my changes:
- Assistant
The diff looks clean and minimal. Let me run a broader set of tests to make sure nothing is broken:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.