Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-5· anthropicHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION orm.migrator.up() rewrites snapshot incorrectly Describe the bug When…

  1. User

    ISSUE DESCRIPTION

    orm.migrator.up() rewrites snapshot incorrectly

    Describe 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

    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 using git reset --hard command. Additionally, you can use git stash and git stash pop to 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 submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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-$

  2. Claude 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 up differs incorrectly from expected.

  3. Claude Assistant

    Good, that matches. Now let's fix the missing trigger restoration.

  4. Claude Assistant

    Now let's rerun the trigger reproduction test to confirm the fix.

  5. Claude Assistant

    Good, DecimalType already imported. Let's add precision/scale recovery in init().

  6. Claude Assistant

    Now let's rerun the reproduction test to confirm the fix.

  7. Claude 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.

  8. Claude 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.

  9. Claude 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.

  10. Claude 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.

  11. Claude 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.

  12. Claude 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.

  13. Claude 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.

  14. Claude 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.

  15. Claude 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.

  16. Claude Assistant

    Now let's re-run the sqlite test suites and typecheck again to confirm this refinement doesn't break anything.

  17. Claude Assistant

    Let's quickly re-verify the reproduction scripts one more time with the refined fix.

  18. Claude Assistant

    Confirmed working. Let's do a final check of git status to ensure only intended files are modified.

  19. Claude 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:create from entity metadata via getTargetSchema(), vs. migration:up/migration:down from live DB introspection via DatabaseSchema.create()):

    1. Decimal precision/scale lost on sqlite after up()/down() (packages/sql/src/schema/DatabaseTable.ts): addColumnFromProperty() (metadata path) recovers precision/scale for decimal-family columns by regex-parsing the declared type (e.g. numeric(10,2)), but DatabaseTable.init() (introspection path) never did this — sqlite's PRAGMA table_info only reports the raw declared type string, unlike mysql/postgres/mssql/oracle which expose numeric_precision/numeric_scale directly 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 subsequent migration:create(). I mirrored the same regex-based recovery into init(), guarded so it's a no-op wherever the schema helper already supplies real precision/scale (mysql/postgres/mssql/oracle).

    2. Triggers dropped when re-reading a snapshot (packages/migrations/src/Migrator.ts): getSchemaFromSnapshot() reconstructs DatabaseTable instances from the JSON file but never called table.setTriggers(...), even though toJSON() 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/scale and triggers info after migrator.up(), causing bogus diffs on the next create().
    • 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 existing if (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 --noEmit clean for packages/sql and packages/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 unmodified HEAD.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.