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 for migration and snapshot handling.
- Assistant
Let me first look at the reproduction repository to understand the issue better.
- Assistant
Let me look at the reproduction repository to understand what the issue is about.
- Assistant
Let me look at the snapshot-related code in the schema-knex and knex packages.
- Assistant
The issue is about
orm.migrator.up()rewriting the snapshot incorrectly. Let me look at the migrator code and the snapshot-related code. - Assistant
The issue is about
orm.migrator.up()rewriting the snapshot incorrectly. The user mentioned they use@mikro-orm/sqlite. Let me look at theMigrator.tsin thepackages/migrations/src/Migrator.tsfile, as that's the one used for SQL-based migrations. - Assistant
Let me look at the
DatabaseSchema.createmethod and thegetTargetSchemamethod to understand what's happening: - Assistant
Now let me look at the repro repo to understand the issue better.
- Assistant
Now I need to understand what
DatabaseSchema.createdoes vsgetTargetSchema. Let me look atDatabaseSchema.create: - Assistant
Let me check recent git history for changes related to snapshot handling:
- Assistant
Let me look at the snapshot-related code more carefully. The issue says
orm.migrator.up()rewrites snapshot incorrectly. Let me examine therunMigrationsmethod more carefully: - Assistant
Let me trace the migration flow more carefully. Let me look at how the parent
runMigrationsworks: - Assistant
Let me look at the repro repository to understand the exact issue:
- Assistant
Let me look at the reproduction repository to understand the issue better.
- Assistant
Let me explore the reproduction repository to understand the issue better.
- Assistant
The repro uses
@Property({ type: "string", comment: "free-form notes" })on a field. Let me look at the snapshot file to see what's happening. - Assistant
Let me look at the snapshot file to understand its structure.
- Assistant
Let me look at the migrations directory:
- Assistant
Let me see the migration file:
- Assistant
Let me look at the SQLite platform and how it handles comments in schema introspection:
- Assistant
Let me look at the SqliteSchemaHelper to understand how it handles comments:
- Assistant
Let me look at the recent commit that mentions snapshot serialization:
- Assistant
Let me look at the
DatabaseTable.toJSONmethod to understand how the snapshot is being serialized: - Assistant
Let me see the
toJSONmethod and how the snapshot is stored/loaded. - Assistant
Let me look at the
toJSONmethod ofDatabaseTablemore carefully. - Assistant
Let me look at
DatabaseSchema.createto see how it works: - Assistant
Let me look at the
DatabaseSchema.createmethod and theDatabaseSchema.toJSONmethod to understand the issue. - Assistant
Let me check
DatabaseSchema.toJSON(): - Assistant
Let me look at the
toJSONmethod of DatabaseSchema. - Assistant
Let me look at the repro more carefully and run it to see what happens:ᕤI need to understand the issue better. The key problem seems to be in the
runMigrationsmethod of theMigratorclass. After running migrations, it reads the schema from the database (usingDatabaseSchema.create) and stores it as the snapshot. But this is incorrect because:- The snapshot should represent the expected state based on entity metadata, not the actual database state
- When running
up(), the snapshot should be updated to match the entity metadata after the migration - However,
DatabaseSchema.createreads from the database, which may not match exactly what the metadata says
Let me check what the issue actually is. Let me look at the
storeCurrentSchemamethod more carefully.Actually, looking at the code again:
- In
runMigrations(the override), it creates a schema from the database:
- Assistant
Let me focus on one command at a time.
- Assistant
Now I understand the core issue. Let me check how SQLite introspects columns and what properties are missing:
- Assistant
Let me look at how SQLite gets columns.
- Assistant
Now I understand the issue! Let me trace through the flow:
migration:createwrites the snapshot from entity metadata (viagetTargetSchema())migration:up(runMigrations) rewrites the snapshot from database introspection (viaDatabaseSchema.create())- The two serializations can differ (e.g., SQLite doesn't store column comments in the database)
- This causes the snapshot to "flip-flop" between two different representations
The fix should be: in
runMigrations, usegetTargetSchema()(entity metadata) instead ofDatabaseSchema.create()(introspection) to keep the snapshot consistent with whatcreate()produces.Let me verify this by checking the existing tests:
- Assistant
Now I see the full picture. Let me look at the
runMigrationsmethod more carefully: - Assistant
Now I understand the issue clearly. The
runMigrationsoverride in theMigratorclass usesDatabaseSchema.create()(introspection from the actual database) to build the snapshot, whilecreate()andcreateInitial()usethis.#schemaGenerator.getTargetSchema()(from entity metadata). These two can produce different outputs, especially for things SQLite doesn't store (like column comments).The fix is to use
getTargetSchema()inrunMigrationsas well, just likecreate()does.Let me also check the
downmethod behavior - it shouldn't update the snapshot ondown: - Assistant
Let me now look at the actual issue. The problem is clear:
runMigrationsin theMigratorclass rewrites the snapshot usingDatabaseSchema.create()(database introspection), whilecreate()andcreateInitial()usegetTargetSchema()(entity metadata). These can produce different outputs, causing the snapshot to flip-flop.The fix is simple - just call
storeCurrentSchema()without passing a schema, so it falls back togetTargetSchema(). - Assistant
Now I see the issue clearly. The
runMigrationsmethod introspects the live database viaDatabaseSchema.create()to build the snapshot, butcreate()andcreateInitial()usegetTargetSchema()(entity metadata). These produce different outputs (e.g., SQLite doesn't store column comments in the database, so introspection loses them).The fix is simple: call
storeCurrentSchema()without passing a schema (likecreate()andcreateInitial()do), so it falls back togetTargetSchema(): - Assistant
Now I understand the issue completely. The
create()method usesgetTargetSchema()(entity metadata) for the snapshot, butrunMigrations()usesDatabaseSchema.create()(database introspection). These produce different outputs for SQLite (e.g., no column comments in the database). The snapshot should use entity metadata to stay consistent.Let me make the fix:
- Assistant
Good. Now let me check if the
DatabaseSchemaimport is still needed elsewhere in the file: - Assistant
Good,
DatabaseSchemais still used elsewhere. Let me also check thatUtilsis still needed: - Assistant
Utilswas only used in the removed code. Let me check if it's used elsewhere in the file: - Assistant
Utilsis no longer used. Let me remove it from the import: - Assistant
Let me check if
Utilsis used elsewhere and if it's safe to remove: - Assistant
Utilsis no longer used. Let me remove it: - Assistant
Let me use the correct syntax for the edit command.
- Assistant
Now let me check if
MigrateOptionsis still needed: - Assistant
Good,
MigrateOptionsis still used. Let me check if the unused importst,Type,UnknownTypeare still needed: - Assistant
Good,
t,Type, andUnknownTypeare still used. Let me also checkDictionaryandDatabaseTable: - Assistant
Good, all imports are still used. Let me verify the final state of the modified area:
- Assistant
Now let me verify the fix by running the related tests. First, let me check what tests exist for migrations:
- Assistant
Let me also check what tests exist for the snapshot-related issue:
- Assistant
Let me look at that test:
- Assistant
Let me run the existing migration tests for SQLite:
- Assistant
All SQLite migration tests pass. Let me also run the general Migrator test:
- Assistant
These failures look like they're pre-existing (database connection issues, not related to my change). Let me verify by checking if they also fail without my change:
- Assistant
Good, those failures are pre-existing. Let me also run the GH7796 test that was added in the HEAD commit since it's related to snapshot serialization:
- Assistant
That test needs MySQL. Let me look at what it tests:
- Assistant
This is very illuminating. The GH7796 test specifically checks that the snapshot from
createInitial()matches the snapshot fromup(). The HEAD commit's approach was to normalize MySQL introspection to match metadata. My approach is more fundamental - usegetTargetSchema()(entity metadata) for both, which is the root cause fix.Let me now verify the fix works correctly. Let me write a reproduction test based on the issue:
- Assistant
All SQLite migration tests pass. Let me also run the schema generator tests for SQLite to make sure nothing is broken there:
- Assistant
Let me now also look at the snapshot test at line 85 to make sure the test is verifying the correct behavior:
- Assistant
Now let me look at the postgres migrator test to verify nothing breaks there either:
- Assistant
Those failures are just "connect ECONNREFUSED" (no Postgres running), not related to our change. Let me see the full diff of what I changed:
- Assistant
The diff looks clean and minimal. Let me now write a test script to verify the fix addresses the exact issue from the reproduction:
- Assistant
Let me use the existing test framework instead. Let me check the snapshot test more carefully:
- Assistant
Let me look at the test that directly tests the snapshot rewrite behavior:
- Assistant
Let me verify my change is correct by examining the full flow. The key insight is:
create()callsstoreCurrentSchema()→ usesgetTargetSchema()(entity metadata)createInitial()callsstoreCurrentSchema()→ usesgetTargetSchema()(entity metadata)runMigrations()(called byup()/down()) used to introspect the database, now callsstoreCurrentSchema()→ usesgetTargetSchema()(entity metadata)
This makes all three paths consistent. Let me verify the final state of the file:
- Assistant
Let me run all the migration-related tests that don't need external databases:
- Assistant
Let me also check if
DatabaseSchemaandDatabaseTableimports are still needed after the change: - Assistant
Good,
DatabaseSchemaandDatabaseTableare still used elsewhere. Let me also checkMigrateOptions: - Assistant
Good, everything is clean. Let me also check if
MigrationInfois still used: - Assistant
Good. Let me also check if
MikroORMimport is still needed: - Assistant
All imports are still needed. Now let me run a broader set of relevant tests to ensure nothing is broken:
- Assistant
Let me also check if there are any other snapshot-related tests or issue tests that might be relevant:
- Assistant
Let me look at the GH7773 test:
- Assistant
The fix is clean. Let me run the SQLite migration tests one final time and also verify the full diff:
- Assistant
Let me verify the complete diff one more time:
- Assistant
Let me also run the schema-generator SQLite tests and any other relevant tests:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.