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
Good, existing tests pass. Let's write a temp reproduction test with richer entities including relations, enums, decimals, unique constraints to see if snapshot after
updiffers incorrectly from expected.Assistant
Good, that matches. Now let's fix the missing trigger restoration.
Assistant
Now let's rerun the trigger reproduction test to confirm the fix.
Assistant
Good,
DecimalTypealready imported. Let's add precision/scale recovery ininit().Assistant
Now let's rerun the reproduction test to confirm the fix.
Assistant
Good, this failure is pre-existing and unrelated to my change (likely a missing DB driver in this sandbox). Let's run the broader relevant test suites (sqlite, postgres, mysql, mssql migrations & schema-generator) to check for regressions from my fix.
Assistant
Good, mssql already sets precision. Let's also check libsql/node-sqlite dialects since they might reuse SqliteSchemaHelper (fine, same fix applies). Let's check oracledb too.
Assistant
Good — all other dialects already provide precision/scale directly. Only sqlite lacked it, confirming this was the genuine, previously-unfixed gap. Now let's build the full monorepo to ensure TypeScript compiles correctly.
Assistant
Both compile cleanly. Now let's run the full sqlite-related test suite once more plus the general (non-DB) test suite for schema/sql packages to be thorough, and check git diff.
Assistant
Clean. Now let's run the broader test suite for schema-generator and entity-generator to check nothing else regressed, plus the full sqlite suite once more for good measure.
Assistant
All sqlite-based schema-generator tests pass. The failures are all for DB drivers unavailable in this sandbox (mysql/postgres/mssql/mariadb/oracle), unrelated to my change. Let's confirm this is pre-existing by checking without my change too, quickly on a subset.
Assistant
Confirmed pre-existing failures unrelated to my changes (missing DB connections in sandbox). Let's also run the entity-generator and migrations full sqlite suites once more, plus a broader non-DB-dependent generic run to be safe.
Assistant
All failures are again due to unavailable DB servers (mysql/postgres/mssql). The sqlite ones (collation, index-expressions, CustomBase) all pass. Let's run the full repo-wide sqlite + generic (non-db-driver) test suite as a final sanity check.
Assistant
All sqlite/mongo-related migration tests pass; remaining failures are due to unreachable postgres/mssql servers in this sandbox (pre-existing/environmental). Let's do a final review of the diff.
Assistant
Now let's re-run the sqlite test suites and typecheck again to confirm this refinement doesn't break anything.
Assistant
Let's quickly re-verify the reproduction scripts one more time with the refined fix.
Assistant
Confirmed working. Let's do a final check of git status to ensure only intended files are modified.
Assistant
Summary
I traced the
orm.migrator.up()snapshot-corruption bug reported in the issue to two concrete asymmetries between the two snapshot sources described in the migrations docs (migration:createfrom entity metadata viagetTargetSchema(), vs.migration:up/migration:downfrom live DB introspection viaDatabaseSchema.create()):-
Decimal
precision/scalelost onsqliteafterup()/down()(packages/sql/src/schema/DatabaseTable.ts):addColumnFromProperty()(metadata path) recoversprecision/scalefor decimal-family columns by regex-parsing the declared type (e.g.numeric(10,2)), butDatabaseTable.init()(introspection path) never did this — sqlite'sPRAGMA table_infoonly reports the raw declared type string, unlike mysql/postgres/mssql/oracle which exposenumeric_precision/numeric_scaledirectly from their information schema. As a result, every decimal/numeric column in the snapshot silently lost its precision/scale info as soon as a migration was applied, causing spurious ALTER-COLUMN diffs (and repeated snapshot rewrites) on every subsequentmigration:create(). I mirrored the same regex-based recovery intoinit(), guarded so it's a no-op wherever the schema helper already supplies real precision/scale (mysql/postgres/mssql/oracle). -
Triggers dropped when re-reading a snapshot (
packages/migrations/src/Migrator.ts):getSchemaFromSnapshot()reconstructsDatabaseTableinstances from the JSON file but never calledtable.setTriggers(...), even thoughtoJSON()serializes triggers. Any entity-level trigger vanished from the in-memory schema the moment the snapshot was read back, so the next diff/create treated existing triggers as missing and tried to recreate them.
Both were verified with targeted reproduction scripts (create → run migration for real against a fresh DB → compare the pre/post snapshot JSON) that failed before the fix and pass after it, then removed before submitting.
Checklist:
- Reproduced the reported error: yes — built minimal entities (decimal column, and separately a trigger-bearing entity) and confirmed the on-disk snapshot silently lost
precision/scaleandtriggersinfo aftermigrator.up(), causing bogus diffs on the nextcreate(). - Confirmed the fix resolves it: yes — same repro scripts show an empty diff between the metadata-derived and introspection-derived snapshots after the fix; removed the temporary test files afterward.
- Considered edge cases: guarded the precision/scale recovery to only fire when the schema helper hasn't already supplied real values (so mysql/postgres/mssql/oracle, which do report them, are unaffected), used
??=semantics matching the existing metadata-side logic, and kept the trigger restoration behind the existingif (tbl.x)pattern used for other optional legacy-snapshot fields. - Ran existing tests: full sqlite test suite (
Migrator.sqlite.test.ts,SchemaGenerator.sqlite*.test.ts, all*.sqlite.test.ts,entity-generator,schema-generator) passes;tsc --noEmitclean forpackages/sqlandpackages/migrations. Failures seen in mysql/postgres/mssql/mariadb/oracle-backed tests are pre-existing (no DB servers available in this sandbox) and reproduce identically on the unmodifiedHEAD.
-
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 →
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 →