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
I'll start by exploring the repository structure to understand the codebase.
Assistant
I'll start by exploring the repository structure.
Assistant
Let me explore the packages directory and find the migrator-related code.
Assistant
Let me look at the migrations package and find the migrator code.
Assistant
Let me look at the MigrationGenerator and snapshot-related code.
Assistant
Let me look at the Migrator.ts to understand the snapshot handling.
Assistant
Let me look at the git history for the Migrator file to understand recent changes.
Assistant
Let me look at the recent commit that introduced the runtime schema context changes.
Assistant
Let me look at the most recent snapshot fix commit.
Assistant
Let me look at the existing migration tests for snapshot behavior.
Assistant
Let me look at the snapshot-related test added in the caec0616c commit.
Assistant
Let me look at the snapshot stability test in detail.
Assistant
Let me look at the AbstractMigrator base class.
Assistant
Now I understand the architecture. The
runMigrationsinMigrator.tsoverrides the base class'srunMigrations. After running migrations, it writes the snapshot from the DB introspection viaDatabaseSchema.create.Now the issue is:
orm.migrator.up()rewrites the snapshot incorrectly. The reproduction repo shows that the snapshot changes afterup().Let me think about why this might be incorrect. After
up(), the database is fully migrated, so the DB schema should match the metadata target schema. But the snapshot written from introspection might differ from the metadata-based snapshot. The fix caec0616c tried to align them, but maybe there are still divergences.Wait, the issue version is
7.1.2-dev.10. Let me check the current repo version.Assistant
Let me look at the most recent snapshot fix commit.
Assistant
Let me look at the GH7796 test to understand the pattern of these fixes.
Assistant
Let me create a reproduction test for SQLite to see the divergence. First, let me understand the test bootstrap.
Assistant
Let me create a reproduction test for SQLite. I'll write a test that creates an initial migration, runs up, and compares the snapshot before and after.
Assistant
I reproduced the issue. The divergence is on
precisionandscalefor decimal columns. The metadata-side writesprecision: 10, scale: 2, but the SQLite introspection-side writesprecision: null, scale: null. Let me look at thetoJSONand column handling.Assistant
Let me look at the SQLite schema helper to understand how it introspects column types and precision/scale.
Assistant
Let me look at the
getColumnsmethod for SQLite.Assistant
Let me check the
normalizeColumnTypeand related methods for SQLite.Assistant
So for SQLite,
normalizeColumnType('decimal(10,2)')returnsextractSimpleType('decimal(10,2)')which would bedecimal... wait, let me checkextractSimpleType.Actually, let me check what
extractSimpleTypedoes withdecimal(10,2).Assistant
Let me check the test output more closely and understand what column type is generated. Let me add debugging to the test.
Assistant
Let me check how decimal is handled. Let me modify the test to print the snapshots.
Assistant
Let me modify the test to print the column details instead.
Assistant
Let me add a primary key.
Assistant
Let me look at the full diff from the original repro test with all entity types.
Assistant
The previous run left migrations. Let me clean up and run again.
Assistant
The database file still has migration records. Let me drop the database file.
Assistant
The only divergence in my repro is precision/scale on the decimal column. Let me check how other dialects extract precision/scale from column types.
Assistant
Let me check what
col.typereturns for the SQLite introspection.Assistant
Let me confirm what
col.typereturns from SQLite introspection by adding a temporary log.Assistant
Now I understand the root cause. SQLite's introspection doesn't extract
precision/scalefrom the column type for decimal/numeric columns, while the metadata path does. Let me apply the fix inDatabaseTable.init()(the introspection path), mirroring the logic inaddColumnFromProperty.Assistant
DecimalTypeis already imported. Now let me add the precision/scale extraction in theinitmethod:Assistant
Now let me add the precision/scale extraction in the
initmethod:Assistant
Now let me run the repro test again to see if the precision/scale divergence is fixed.
Assistant
The repro test passes. Now let me make a more comprehensive test to catch any other divergences, since the issue mentions "a bunch of stuff".
Assistant
Let me try to clone the reproduction repo to see the exact entities.
Assistant
The reproduction uses a
commenton a property. Let me check the migrate.ts and the snapshot.Assistant
Now let me write a test mirroring the exact repro to confirm the comment divergence.
Assistant
The actual issue is the comment field. SQLite doesn't support column comments, so the comment is lost during introspection. Let me check how SQLite handles comments in DDL generation.
Assistant
Let me check whether SQLite supports column comments.
Assistant
Let me check the base
getChangeColumnCommentSQLand how comments are diffed.Assistant
Let me check how MySQL and PostgreSQL emit column comments, to understand which platforms support them.
Assistant
Let me check MSSQL and look at the SchemaComparator comment handling.
Assistant
Let me check the Platform class for comment-related capabilities.
Assistant
Let me look at the alter table SQL handling for comment-only changes.
Assistant
So for SQLite,
getChangeColumnCommentSQLreturns''. And theappendmethod - let me check if it filters empty strings. Let me checkappend.Assistant
Let me search for any existing capability method related to comments.
Assistant
Let me check Oracle's comment support.
Assistant
Let me check whether MSSQL and Oracle override
getChangeColumnCommentSQL.Assistant
Let me check where
supportsUnsignedis defined.Assistant
Let me look at the platform class hierarchy to find where to add the capability.
Assistant
The fix needs two parts: (1) drop comments from the snapshot for platforms that don't support them (so
up()doesn't change the snapshot), and (2) make the comparator ignore comment diffs for such platforms (socreate()doesn't generate no-op migrations). Let me examine the SchemaComparator.Assistant
Let me look at the table comment diff (line 286) and the SchemaComparator constructor.
Assistant
Line 286 is in a routine comparison (functions), not tables. Let me find the table comment diff. Line 451-455 is the table comment diff:
if (this.diffComment(fromTable.comment, toTable.comment)) { tableDifferences.changedComment = toTable.comment; ... }Let me look at that.
Assistant
Let me check the libsql platform inheritance.
Assistant
Let me check pglite and other platforms that might use a Sqlite-based platform.
Assistant
Let me trace how the diff.up is built from the alter table SQL to check if empty comment-change SQL results in an empty migration.
Assistant
Now I understand the full picture. I'll follow the existing
supportsUnsignedpattern: add asupportsComments()capability to the Platform, gate comment emission intoJSON, and gate comment diffing in the comparator. Let me first add the capability to the core Platform.Assistant
The JSDoc has a
*\/with a backslash that I escaped for the shell. Let me fix the comment to avoid the closing-comment sequence.Assistant
Now let me add the override in the 4 platforms that support comments. Let me look at the existing
supportsUnsignedoverride in BaseMySqlPlatform.Assistant
Now the PostgreSQL base platform. Let me find a good location.
Assistant
The edit command requires the file to be the currently open file. Let me open it first, then edit. Actually, the
--fileoption should work, but the error says "Text not found in displayed lines." This might be because the editor needs the lines to be displayed. Let me open the file at line 59 first.Assistant
Now MSSQL platform. Let me find a good location.
Assistant
Now the Oracle platform. Let me find a good location.
Assistant
Now let me update
DatabaseTable.toJSONto gate comments onsupportsComments(). Let me open the toJSON method.Assistant
Now update the column comment line:
Assistant
Now the table comment line (1476 originally, now shifted). Let me update it.
Assistant
Now let me update the SchemaComparator to skip comment diffing when the platform doesn't support comments. Let me open the comparator at the column comment diff.
Assistant
Now the table comment diff (line 451). Let me update it.
Assistant
Now let me run the repro3 test to see if the comment divergence is fixed.
Assistant
The repro3 test passes. Now let me run the existing migration tests to make sure I didn't break anything.
Assistant
All 24 sqlite migration tests pass. Now let me run the schema-related tests and the entity-generator tests, since I touched
DatabaseTableandSchemaComparator.Assistant
Now let me run the snapshot test file and GH7796 (which is MySQL, might not run without MySQL). Let me check what test files cover schema diff and snapshots broadly.
Assistant
The failures are connection errors (postgres/mssql/mysql need external DBs). Let me confirm these are all connection-related, not caused by my changes.
Assistant
The sqlite and mongo tests pass. The other failures need external databases. Let me verify
Migrator.test.tsuses an external DB (postgres) — these are connection failures, not caused by my changes.Assistant
The
Migrator.test.tsuses MySQL (port 3308). These are connection failures. Let me try to start the databases via docker-compose to run the broader snapshot tests, especially for MySQL/PG.Assistant
No docker. Let me check if local database binaries are available.
Assistant
No database binaries available, so I can only run SQLite/in-memory tests. Let me run the broader set of SQLite-based schema and diff tests.
Assistant
The 3 failures are mariadb/mysql/postgres connection errors. The SQLite tests pass. Let me run the SQLite-only schema tests individually to be thorough.
Assistant
Now let me run the entity-generator sqlite tests (since the snapshot fix touches
DatabaseTablewhich the entity-generator relies on) and the GH issue tests that don't need external DBs.Assistant
Let me write a quick temp test to verify table comments on SQLite are also handled (no flip-flop, no-op migration).
Assistant
Both table and column comments are now stable on SQLite. Let me check for libsql/node-sqlite tests since they use SqlitePlatform too.
Assistant
The sqlite/libsql variants pass. Now let me run the actual reproduction repo end-to-end against the local build to be 100% sure. Let me build the relevant packages and link the repro.
Assistant
Let me run the repro end-to-end inside the mikro-orm repo (where
@mikro-orm/*resolves to local source). I'll use a file-based DB so the snapshot is written and inspectable.Assistant
The repo's tsx uses cjs. Let me use
.mtsextension for ESM.Assistant
The legacy decorator has issues with tsx/esbuild. Let me instead faithfully replicate the repro's exact scenario (pre-existing snapshot with comment + pre-existing migration) as a vitest test, which handles decorators correctly.
Assistant
The faithful repro works: first
up()drops the comment (one-time fixup), secondup()doesn't change the snapshot (no flip-flop), andcreate()produces no diff.Now let me check whether any committed snapshot files include SQLite column/table comments that might now be outdated.
Assistant
Let me check the migration snapshot dumps for SQLite that might contain comments.
Assistant
The sqlite migration snapshots don't contain comments, so no committed snapshots are affected. Let me run a final combined run of the key affected SQLite test suites.
Assistant
All pass. Let me check whether the entity-generator emits precision/scale for decimals (my
initchange now provides them on SQLite introspection) by checking if any SQLite entity-generator snapshot has decimals.Assistant
Let me check how the entity-generator uses precision/scale to ensure my
initchange doesn't introduce uncovered regressions.Assistant
Let me check the index-expressions.sqlite snapshot for decimal columns that might now get precision/scale.
Assistant
The
positioncolumn usesp.decimal()which is a helper that generatesnumeric(10,2)(default precision 10, scale 2). Wait, let me checkp.decimal()— what's the default? Let me check. Actually, the index-expressions test passed, meaning the entity generation output matched. But wait — is this the entity-generator output (introspection → entity code)? Let me check the test to see if it's generating entities from introspection.Actually,
index-expressions.sqlite.test.tspassed (1 test). The snapshot matched. So either:- The
position: p.decimal()is from the schema definition (not generated entity), OR - The generated entity didn't include precision/scale before, and still doesn't (if
p.decimal()producesnumeric(10,0)or something where precision extraction matches).
Let me check what
p.decimal()produces. Actually, let me look at the snapshot more closely — is there a generated entity with a decimal column?Wait, the test passed, so the snapshot matched with my changes. So no regression there. Let me verify by re-running just that test with
--no-updateto be sure it's not auto-updating.Actually vitest by default doesn't auto-update snapshots unless
--updateis passed. So if the test passed, the snapshot matched exactly. Good.But let me double check: maybe
p.decimal()generatesnumericwithout precision (sonumeric(10,0)?). Let me check the helper. Actually, let me check the snapshot line 51 context — is it in adefineConfig/entity definition or generated output?Let me look at the snapshot around line 51.
- The
Assistant
The index-expressions.sqlite test passed, so no entity-generator regression. Let me write a comprehensive test with many column types to catch any remaining divergences ("a bunch of stuff" suggests there may be more).
Assistant
No divergences across all those column types. Let me test composite primary keys, check constraints, and indexes with options.
Assistant
All comprehensive tests pass — no divergences for composite PKs, check constraints, indexes, FKs, etc. on SQLite. Now let me clean up my temporary reproduction test files (per instructions, not to add new tests).
Assistant
All temp files removed. Let me review the full diff to verify correctness.
Assistant
The diff looks clean. Let me verify there are no TypeScript compilation errors in the affected packages.
Assistant
All type checks pass. Let me also verify the mysql, postgresql, pglite, and libsql packages type-check since I modified their platforms.
Assistant
The pglite errors are pre-existing third-party type definition issues (unrelated to my changes — I didn't touch pglite, and the errors are in
@electric-sql/pglitenode_modules types).Let me do a final verification run of the key affected SQLite test suites after cleanup.
Assistant
All 106 tests pass. Let me verify my understanding of the existing GH #7607 stability test by checking if
initORMSqlitepre-creates the schema (so introspection has tables to compare).Assistant
Let me test generated columns on SQLite, which is another potential divergence point.
Assistant
Generated columns round-trip fine. Let me clean up this last test file and do a final review.
Assistant
All clean. Let me run the linter on the changed files to ensure code style compliance.
Assistant
Linting passes. Let me verify formatting with oxfmt.
Assistant
There's a formatting issue in DatabaseTable.ts. Let me fix it with oxfmt.
Assistant
Let me check what oxfmt changed to ensure it didn't break the code.
Assistant
oxfmt just removed unnecessary parens (semantically identical). Let me run a quick test to confirm everything still works after formatting.
Assistant
Let me do a final check: search for any existing SQLite tests that set comments and verify they're in the snapshot (which my change would affect).
Assistant
Let me verify the
GH6105.test.ts(sqlite + comment) doesn't rely on column comments in snapshots.Assistant
That's a property named
comment, not a column comment. Let me check the other sqlite test with comment to be safe.Assistant
Those are entity property names, not column comments. All good. Let me run the complete set of SQLite schema-generator tests one final time to ensure the comparator change didn't break anything.
Assistant
All SQLite schema-generator tests pass. Let me do a final full diff review before submitting.
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 →