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 callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /mikro-orm, current file: none) bash-$
Assistant
Let me start by exploring the repository structure to understand the
Assistant
me look at the relevant files for understanding the snapshot behavior:
Assistant
me look at the reproduction repo to understand the issue:
Assistant
Let me clone the reproduction repo:
Assistant
Now let me reproduce the bug locally. Let me set up the reproduction.
Assistant
I've reproduced the bug. Now let me investigate why this happens:
Assistant
Let me check how other dialects handle column comments:
Assistant
So MySQL and PostgreSQL have native comment support, while SQLite doesn't. Let me check how the SQL is generated for SQLite and how comments are stripped when generating create table SQL
Assistant
Let me look at the parent class:
Assistant
Now let me check the SQLite schema helper to see if it preserves comments in any way.
Assistant
Now let me understand the issue better by looking at how the migrator works:
Assistant
me look at existing snapshot tests to understand what's expected:
Assistant
me look at the recent commits related to snapshot:
Assistant
Now let me look at the current code in DatabaseTable toJSON to understand what's there.
Assistant
me look at PR #7608 and other related changes:
Assistant
Let me check the test file for the snapshot in SQLite:
Assistant
Let me check the related issue test (GH7778) for reference:
Assistant
me check the SqlitePlatform and see if it has any concept of supports column comments:
Assistant
Let me look at how the schema helper creates columns to see if comments are added:
Assistant
me look at the GitHub issue this is referring to:
Assistant
me look at the SchemaHelper class to see if there's a
supportsColumnComments()method:Assistant
Let me look at the existing test that involves running migrations and snapshots:
Assistant
let me look at the Migrator.ts code more carefully and understand the fix needed:
Assistant
Let me look at the existing migration tests:
Assistant
Let me look at the full picture of this test (the rest of the snapshot tests
Assistant
me look at the entities-schema to understand the test entities:
Assistant
Now let me see the existing snapshot test for SQLite to understand the pattern:
Assistant
Let me run the existing test cases first to see the baseline:
Assistant
let me create a script to reproduce the bug locally:
Assistant
I need to specify the driver:
Assistant
Let me look at the schema generator:
Assistant
Let me look at the DatabaseSchema.create function more carefully:
Assistant
Let me check the existing test to see what the expected behavior is for cases where the migration adds things that aren't in the entity
Assistant
Let me look at the migration tests that involve raw SQL:
Assistant
Let me check what tests might be affected by using the target schema:
Assistant
Now let me understand the database schema structure better:
Assistant
Let me check the Column type definition
Assistant
Now I have a clear understanding of the issue. Let me implement the fix. The fix should be in the
Migrator.tsto merge the target schema with the database schema, so we preserve fields like column comments that the database doesn't track (specifically for SQLite).Let me first verify my understanding by exploring the schema structure:
Assistant
let me design the fix. The approach is to merge the database schema with the target schema after
migration:up, filling in fields the database doesn't track (like SQLite column comments):Assistant
Now let me design and implement the fix. The fix is in the
Migrator.tsto merge the target schema with the database schema, so we preserve fields like column comments that the database doesn't track:Assistant
Now let me implement the fix. I'll modify the
runMigrationsmethod to merge the target schema with the database schema:Assistant
Now let me implement the fix. I'll add the merging logic in
Migrator.ts:Assistant
Let me implement the fix in the
Migrator.ts:Assistant
Let me use the edit command to apply the fix:
Assistant
Now let me apply the fix using the
replacecommand:Assistant
Let me check the editor's current view:
Assistant
Now I can see the file. Let me try editing it with the edit command using a smaller search text:
Assistant
Now let me create a test to verify the fix works:
Assistant
Let me also write a simpler test that doesn't rely on require
Assistant
fix works. Now let me run the existing migration tests to make sure nothing is broken:
Assistant
Let me also run the migration tests for other databases:
Assistant
Postgres tests need a postgres server. The test failures are due to no postgres connection. Let me check if the postgres tests would be affected by my changes (the issue is the connection, not the test). Let me look at the specific failing test:
The failing tests are:
ensureTable when the schema does not exist- can't connectrespects the skipTables option when diffing schemas- can't connect
These failures are because postgres isn't available, not because of my changes. Let me see if there are other tests that don't require a connection.
Actually, the postgres tests are running, but many are skipped because of the connection issue. Let me check the migration tests:
Assistant
The tests are mostly skipped because there's no Postgres connection. Let me check the docker setup to see if I can get a postgres running:
Assistant
No docker available. Let me focus on the SQLite tests since the bug is SQLite-specific. Let me also test other SQLite-related migration tests:
Assistant
Postgres isn't available. Let me run all the SQLite tests to make sure nothing is broken:
Assistant
All SQLite migration tests pass. Let me also verify the fix works with the actual reproducer:
Assistant
, the reproducer is using the npm published version of mikro-orm, not the local version. Let me check that:
Assistant
The reproducer is using the npm published version, not my fix. Let me link the local code to the reproducer:
Assistant
me verify my fix with a more comprehensive test:
Assistant
fix is working for the SQLite case. Let me also check the postgres tests where the snapshot is supposed to be different after up/down because executeMigrations is mocked.
Let me also look at what other tests might be affected. Let me search for tests that specifically check the snapshot behavior:
Assistant
me run the existing test "snapshot serialization is stable across metadata and introspection paths (GH #7607)" to make sure my fix doesn't break it:
Assistant
The test passes. Let me also run all migration tests to make sure nothing is broken:
Assistant
the failures are due to lack of database connections (Postgres, MySQL, MSSQL, Oracle). The 3 "Failed Tests" are connection errors, not caused by my fix.
Let me verify my fix works for the issue I created:
Assistant
fix works. Let me also think about edge cases. Let me remove the test file I added (since the test should not be in the repo according to the instructions) and run a final check:
Assistant
Let me verify my fix handles all the edge cases correctly. Let me re-check the fix:
Assistant
Let me also verify the imports are correct:
Assistant
Let me think about whether to extend the fix to handle other fields besides
comment. Let me check what other fields might be affected:Assistant
Let me also check if table-level comments are affected:
Assistant
me extend the fix to also handle table-level comments:
Assistant
I made a mistake - I missed adding back the
continuestatement. Let me fix the indentation issue:Assistant
an extra closing brace on line 274. Let me fix it:
Assistant
fix looks good now. Let me verify the full code by viewing it:
Assistant
The fix looks good. Let me run all the migration tests to make sure nothing is broken:
Assistant
24 SQLite migration tests pass. Let me also create my test for the issue again to make sure the fix still works:
Assistant
Let me fix the test (just need to specify the right snapshot name):
Assistant
The test passes. The fix works correctly. Now let me clean up the test file since the instructions say not to add new tests:
Assistant
Let me also verify the fix works with the reproducer using a simple script:
Assistant
Decorators are not being processed. Let me set up the tsconfig properly:
Assistant
works. The comment is preserved after
migrator.up(). Let me clean up the temporary files:Assistant
now run a broader set of SQLite tests to make sure nothing else is broken:
Assistant
failures are mostly for non-SQLite databases which can't run without a database server. Let me check specifically the SQLite tests:
Assistant
These all look like tests that require external database connections (MongoDB, MySQL, Postgres, etc.). Let me check the migration tests specifically:These all look like tests that require external database connections (MongoDB, MySQL, Postgres, etc.). Let me check the migration tests specifically:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /mikro-orm && timeout 600 npx vitest run tests/features/migrations/ 2>&1 | grep -E "^\s*×\s" | head -20]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me try running the test commands.
Assistant
All the failing tests are connection-related (Postgres, MSSQL, MySQL), not caused by my fix. Let me confirm the SQLite tests all pass:
Assistant
25 SQLite migration tests pass. Let me also examine the existing GH7796 test to see if my fix would have any impact:
Assistant
Let me also verify my fix doesn't break the existing SQLite tests:
Assistant
All 24 tests pass. Now let me look at the overall state of my changes:Let me also check the file's final state:
Assistant
The fix is clean and minimal. Let me verify with the actual reproducer:
Assistant
The reproducer is using npm packages, not the local fix. Let me restore the original snapshot and create a new test to verify the fix:
Assistant
Good, the snapshot is back to the original state. Let me verify the fix by linking the local packages to the reproducer:
Assistant
Let me modify the package.json to use local files:
Assistant
The snapshot is now in the original state. Now let me run the migrate command to see if my fix works:
Assistant
The fix works! No diff in
git difffor the snapshot. Let me also verify the fix is stable by running it multiple times:Assistant
The fix is stable. Let me also test that creating a new migration still works:
Assistant
The CLI requires specific versions, but the migrator.up() works correctly. Let me verify the snapshot is still in good state:
Assistant
The snapshot is unchanged. Only
package.jsonandpackage-lock.jsonwere modified (to use local packages). Let me clean up the reproducer:Assistant
Now let me run a final check to make sure my fix doesn't break anything:
Assistant
failures are connection-related (DBs not available). Let me check the SQLite-specific tests:
Assistant
No SQLite tests fail. Let me now check the diff of my fix:
Assistant
The diff looks clean. Let me also verify the final state of the file:Let me also verify the final state of the file:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /mikro-orm && sed -n '195,280p' packages/migrations/src/Migrator.ts]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me try the command again.
Assistant
The fix looks good. Let me also check that my fix doesn't affect MongoDB. Let me look at what
migrator.up()does for MongoDB:Let me check if my fix affects MongoDB too:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /mikro-orm && grep -n "class.*Migrator|extends.Migrator" packages/migrations-mongodb/src/.ts 2>/dev/null | head -5]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
Let me check that my fix doesn't affect MongoDB.
Assistant
Let me check if there are other fields I should handle:
Assistant
Let me check what table-level fields SQLite might be missing:
Assistant
run all the SQLite migration tests once more to make sure nothing is broken:
Assistant
All 25 SQLite migration tests pass.
Let me also check if my fix could be more thorough. Looking at the snapshot fix - we could also handle this in the
DatabaseSchema.createmethod or in the schema helper. But putting it in the Migrator is cleaner because:- It's specific to the migration use case.
- It's a small, focused change.
- It doesn't affect other parts of the system.
Let me also do one final verification - let me look at the overall git diff to make sure I didn't introduce any other changes:Let me also do one final verification - let me look at the overall git diff:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /mikro-orm && git status && echo "---" && git diff --stat]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me try again
Assistant
Only the
Migrator.tsfile is modified with 48 lines added. The fix is minimal and focused. Let me think about one more edge case -migration:down:Assistant
Let me create a simpler test that uses the existing migrations:
Assistant
All 3 tests pass:
- Comment is preserved after
up(). - Snapshot after
up()is identical to snapshot aftercreate(). - Comment is preserved after
down().
Let me clean up the test file since the instructions say not to add tests:
- Comment is preserved after
Assistant
Only the
Migrator.tsfile is modified. Let me look at the final diff:Only theMigrator.tsfile is modified. Let me look at the final diff:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /mikro-orm && git diff]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
me check the final diff.
Assistant
The fix is complete and minimal. Let me run one final test to ensure everything works:Let me run one final test to ensure everything works:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /mikro-orm && timeout 600 npx vitest run tests/features/migrations/Migrator.sqlite.test.ts tests/features/migrations/MigrationRuntimeSchema.sqlite.test.ts 2>&1 | tail -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
run the test again.
Assistant
All 25 tests pass.
Let me now go through the checklist one more time before submitting:
Checklist Review:
-
Reproduced the error: I successfully reproduced the bug with the actual reproducer from the issue (
zachkirsch/mikro-orm-migrate-snapshot-repro) and confirmed that runningnpm run migrate(which callsmigrator.up()) corrupts the snapshot by changing column comments from"free-form notes"tonull. -
Confirmed the fix resolves the error: I verified the fix works by:
- Linking the local packages to the reproducer and running
npm run migratemultiple times - no diff ingit difffor the snapshot - Creating a temporary test that asserts the comment is preserved after
up()anddown()- all assertions pass - Verified the snapshot after
up()is identical to the snapshot aftercreate()
- Linking the local packages to the reproducer and running
-
Edge cases handled:
- Empty string vs null comments (handled with
dbColumn.comment == null || dbColumn.comment === '') - Tables that only exist in the database (raw SQL migrations) - left untouched
- Tables that only exist in the target schema (entity but not in DB yet) - skipped gracefully
- Columns that don't match between schemas - skipped gracefully
migration:downcase - tested and works correctly- Table-level comments - also handled (SQLite also doesn't preserve these)
- Empty string vs null comments (handled with
-
Existing tests pass: All 25 SQLite migration tests pass (
Migrator.sqlite.test.tsandMigrationRuntimeSchema.sqlite.test.ts). The failing tests in other test files are all due to lack of database connections (Postgres, MySQL, MSSQL, Oracle), not related to my fix.
Summary of the fix:
The fix is in
packages/migrations/src/Migrator.ts. Aftermigration:up/migration:down, the snapshot was being read from the database viaDatabaseSchema.create(), which uses dialect-specific introspection. For SQLite, column and table comments are not stored in the database, so the introspection always returnednull/undefinedfor those fields, causing the snapshot to lose this information.The fix adds a
mergeSchemaFromMetadatastatic method that merges the target schema (computed from entity metadata) into the database schema, filling in fields that the database introspection didn't populate. This ensures:- For SQLite (and other dialects that don't track certain fields): entity metadata values are preserved in the snapshot.
- For MySQL/Postgres (which do track comments): no change in behavior since the database already has the correct values.
- Non-entity tables/columns (created via raw SQL migrations) are left untouched in the snapshot.
-
Assistant
The fix is complete. Let me submit it:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →