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
Let me start by understanding the repository structure and the issue. The issue is about
orm.migrator.up()rewriting the snapshot incorrectly in MikroORM. Let me explore the repository structure first.Let me also look at the linked reproduction repository to understand the bug better. But first, let me explore the codebase.
Assistant
Let me look at the packages structure, specifically for the migrator package, and understand the codebase.
Assistant
Let me look at the migrations package structure and understand the relevant code.
Assistant
Let me look at the migrator source code.
Assistant
Let me look at the Migrator.ts file which is the main file for this issue.
Assistant
Now let me look at the DatabaseSchema and DatabaseTable classes, which are serialized in
storeCurrentSchema.Assistant
Let me look at both of these files.
Assistant
Let me now look at DatabaseTable.ts:
Assistant
Let me search for
getTargetSchemain the codeAssistant
Let me look at the
getTargetSchemamethod inSqlSchemaGenerator.ts.Assistant
Let me look at the git log for recent changes related to snapshot and migration to understand the issue
Assistant
Let me look at the most recent commit which is related to snapshot stabilization
Assistant
Let me look at the test from the recent commit and also look at the migrator tests
Assistant
Let me search for related tests and issues
Assistant
Let me look at the SQLite migrator test file, especially the snapshot-related
Assistant
Let me look at the SQLite schema helper to understand how introspection works
Assistant
Hmm, no results. Let me look at the sqlite package structure.
Assistant
Let me look at the SQL package for schema helpers
Assistant
Let me look at the SqliteSchemaHelper
Assistant
Let me try to clone the reproduction repository to understand the issue
Assistant
Let me look at the reproduction repository.
Assistant
Let me look at the source files and README.
Assistant
Now I understand the issue! Column comments are lost in the snapshot after
migrator.up(). Let me look at the source files:Assistant
Let me look at the entity and migration files.
Assistant
Let me look at all these files.
Assistant
Let me look at the other files.
Assistant
Now I have a clear understanding of the issue. The problem is:
migrator.create()writes the snapshot from metadata (entity definitions), which includes column commentsmigrator.up()rewrites the snapshot from database introspection, but SQLite doesn't support column comments, so they becomenull
This causes the snapshot to flip-flop between correct and incorrect states.
Let me look at the key code path more
Assistant
Let me look at the PostgreSQL test that checks snapshot behavior after up/down
Assistant
Now I understand the issue clearly. The
runMigrationsmethod uses database introspection to update the snapshot, but SQLite doesn't support column comments, so metadata-only properties likecommentare lost.The fix should be: after
up, usegetTargetSchema()(metadata-based) for the snapshot, since the database should now match the target schema. Afterdown, keep using introspection since the database may be in a different state.Let me implement the fix:
Assistant
Let me open the file directly
Assistant
Now I'll implement the fix. For
up, use the metadata-based target schema. Fordown, keep using introspection.Assistant
Let me use replace instead:
Assistant
Good, the change looks correct. Now let me verify the fix by running the relevant tests.
Assistant
All SQLite tests pass. Let me also run the PostgreSQL migration tests.
Assistant
The PostgreSQL tests can't run because there's no PostgreSQL server. Let me check if there are any other tests that might be affected. Let me also look at the test that might need updating
Assistant
Let me look at the SQLite test for GH #7607 (snapshot serialization stability) more
Assistant
Let me look at the GH #7234 test in
Assistant
Let me look at the bootstrap to understand the test setup
Assistant
Let me look at the initORMSqlite function
Assistant
Let me look at the parent class's
runMigrationsmethod to understand the fullAssistant
Let me look at the parent
runMigrationsmethodAssistant
Let me look at the
executeMigrationsmethod andprefixmethod to understand how specific migrations areAssistant
Let me look at
executeMigrationsandprefixmethodsAssistant
Let me look at the
prefixmethodAssistant
The SQLite tests pass. Let me now think about whether my change correctly handles all cases. Let me also check MySQL tests.
Assistant
Let me check the GH #7796 test and the other issue
Assistant
Let me review the final state of my change
Assistant
Wait, I need to handle the edge case where only specific migrations are run (not all pending). In that case, the database may not match the full target schema, so we should still use introspection. Let me refine my fix.
Assistant
Now I need to add the
#hasSpecificMigrationshelper method. Let me add it.Assistant
I need to add the
#hasSpecificMigrationsmethod. Let me add it after thehasSnapshotmethod.Assistant
I'll add the
#hasSpecificMigrationsmethod after thehasSnapshotmethod.Assistant
Let me navigate to line 150 and then edit
Assistant
Now let me add the
#hasSpecificMigrationsmethod after line 157.Assistant
Good. Let me now run the SQLite tests again to verify everything still passes.
Assistant
All SQLite migration tests pass. Let me also run the GH #7234 test (which is part of the SQLite tests) and any other relevant tests.
Let me also run a broader set of tests to make sure nothing is broken.
Assistant
Let me run just the SQLite issue tests to confirm nothing is broken.
Assistant
Let me check if the existing tests use entities with comments on columns.
Assistant
Let me write a quick test script to verify the fix works for the exact scenario described in the bug.
Assistant
Now let me run this test script
Assistant
Let me try a simpler test using the schema-based approach like the existing tests
Assistant
Let me run this test
Assistant
Let me verify the git diff to see all changes made
Assistant
Let me run the SQLite tests once more to make sure everything is good
Assistant
Let me also check for any other migration-related test files
Assistant
Let me also run the general Migrator test and the SQLite-based migration runtime schema
Assistant
The failing test is unrelated to my change (it's an
afterAllcleanup issue whereormis undefined). The SQLite test passed.Let me do a final review of my changes by reading the modified file.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.