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 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 explore the repository structure first to understand what we're working with.
Assistant
Let me look at the reproduction to understand the issue better. The issue is about
orm.migrator.up()rewriting snapshot incorrectly. Let me look at the snapshot-related code.Assistant
Now let me look at the
runMigrationsmethod and thestoreCurrentSchemamore closely. The issue is that after running migrations,storeCurrentSchemais called which stores the schema as read from the database (viaDatabaseSchema.create). Let me understand whatDatabaseSchema.createdoes and whatgetTargetSchemareturns.Assistant
So
getTargetSchemabuilds the schema from metadata. ButstoreCurrentSchemainrunMigrationscallsDatabaseSchema.createwhich reads from the database. Let me look at therunMigrationsmethod again carefully.In
runMigrations, after running migrations, they do:const schema = await DatabaseSchema.create(...) await this.storeCurrentSchema(schema);So the schema is created from the database (reading actual current state). Then
storeCurrentSchemawrites that schema as JSON.The issue is the snapshot is "rewritten incorrectly". Let me look at the reproduction repo to understand what exactly happens.
Let me check the repo on GitHub... I can't. Let me think about what
DatabaseSchema.createdoes vsgetTargetSchema.The issue is that
DatabaseSchema.createreads the schema from the database after migration. This should reflect the actual state. But the problem may be that after migration, the database schema read might differ from what the snapshot should contain (e.g., missing defaults, types, etc. that aren't read back the same way).Let me look at the
DatabaseSchema.createmethod andtoJSON/serialization.Assistant
Now let me look at the
DatabaseTable.toJSONmethod, which is where the columns are serialized.Assistant
Let me look at the git history of the
runMigrationsmethod to understand if there was a recent change.Assistant
Let me look at the existing tests for the migrator snapshot behavior.
Assistant
Let me look at the test setup and entities used.
Assistant
Let me build the project and create a reproduction.
Assistant
Let me build the packages first so I can run a reproduction.
Assistant
No dist directories. Let me try to build only the relevant packages. Let me check what build commands are available.
Assistant
The build is taking too long. Let me try building only the core package first, then sql, then migrations. Let me run builds in the background.
Assistant
Core built. Now let me build sql, migrations, sqlite, and other dependencies.
Assistant
Let me build other packages needed for tests: seeder, entity-generator, knex-compat.
Assistant
Now let me try running one of the existing migrator tests to see if the build works.
Assistant
The build works. Now let me create a reproduction script to understand what's "wrong" in the snapshot. Let me look at the reproduction repo referenced in the issue. Since I can't access it, let me create a test based on a more complex schema that exercises things like enum items, default values, foreign keys, etc. Let me look at the test entities.
Assistant
Let me create a reproduction script that creates a migration with a snapshot, then runs
up()and compares the snapshot before and after.Assistant
Let me put the repro in the repo root so relative imports work.
Assistant
I need to use the right driver. The
SqliteDrivershould be imported from@mikro-orm/sqlite. But the issue is thedriveroption needs to be the class. Let me import it correctly. Actually I imported SqliteDriver from@mikro-orm/sqlitebut the error says no driver specified. Thedriveroption needs to be passed. Let me check the import path.Assistant
The error came from grep finding no driver line. Let me just add driver: SqliteDriver to the init options.
Assistant
No diff for the simple schema. The issue must involve specific features. Let me try to clone the reproduction repo to see the exact entities.
Assistant
The issue is the
commentfield flips from a value tonullafterup(). SQLite doesn't support column comments, so reading from DB loses them. Let me examine the entity and config.Assistant
Now I understand the issue clearly. Let me examine the git history to understand the design intent. Let me look at the commit that originally removed snapshot writing on up.
Assistant
Let me look at the test that verifies snapshot writing on up.
Assistant
Let me check the CHANGELOG and look for any related discussion or the actual fix approach.
Assistant
Let me look at the AbstractMigrator to understand the runMigrations flow.
Assistant
Let me look at the recent commit that stabilized snapshot serialization to understand the maintainer's approach.
Assistant
This is very informative. The maintainer's approach has been normalizing serialization in
toJSON(). But SQLite doesn't store column comments at all, so introspection can never recover them. Let me check how SQLite handles comments and whether the comparator diffs them.Assistant
So when only the comment changed, it skips the column alter but calls
getChangeColumnCommentSQL. Let me check the SQLite implementation of that.Assistant
Let me check whether the down→up cycle is tested anywhere, to understand if not-writing-on-up would break important flows.
Assistant
Let me look at the postgres migrator snapshot tests to understand the expected behavior there.
Assistant
This postgres test is very revealing — it asserts the snapshot CHANGES after
up. This conflicts with the issue's expectation. Let me check the git branches/tags to see if there's a fix already in progress.Assistant
This is the key insight. Let me look at the exact c5ebd0a84 commit (the pre-#7235 behavior) to understand the original design.
Assistant
This is the key finding. The c5ebd0a84 commit (pre-#7235) had EXACTLY the behavior the issue wants: only write on
down, and the test assertedsnapshotAfterUp === snapshotAfterCreate(unchanged). #7235 reverted this, causing #7798. Let me check if there's a test stub for #7798 and search for related context.Assistant
No test stub yet. Let me reproduce the exact issue using the Account entity (with comment) to confirm the bug, then verify my fix.
Assistant
The decorator needs reflect-metadata. Let me fix the repro.
Assistant
The legacy decorators have an ESM/tsx issue. Let me use the
defineEntityAPI used in the tests, which supports comments.Assistant
Bug reproduced exactly:
notes.commentflips from"free-form notes"tonullafterup(). Now let me apply the fix. Let me view the currentrunMigrationsmethod.Assistant
Now let me apply the fix — only refresh the snapshot on
down:Assistant
Now let me rebuild the migrations package and re-run the reproduction.
Assistant
The fix works. Now let me verify the down path still writes the snapshot, and test the diverged-metadata scenario to ensure no regression.
Assistant
The auto-generated migration has no
down. Let me use a custom migration class with explicit up/down.Assistant
The
migrationsListwith a class instance doesn't work directly with tsx. Let me use the propermigrationsListformat with{ class: TestMigration }or{ name, class }. Let me check the expected format.Actually, let me check how migrationsList is used in tests.
Assistant
My fix works as expected: up doesn't churn the snapshot, down still writes the reverted DB state. This matches the canonical c5ebd0a84 design. Let me run the existing sqlite migrator tests to see exactly which are affected.
Assistant
Only the
migration up and down both write the snapshottest fails — this is exactly the test #7235 created to encode its (now-reverted) behavior. The c5ebd0a84 commit had a different version ("migration up should not write the snapshot") assertingstoreSchemaSpyis NOT called on up — which my fix satisfies. All other tests pass, including #7607 and #7234 which explicitly wantafterUp == afterCreate.Let me verify there are no other test files relying on up writing the snapshot, and run the migrations-mongodb tests (which don't use SQL snapshots but share AbstractMigrator).
Assistant
Important — there's a
GH7796.test.tsthat tests snapshot flip-flop betweencreateandupon MySQL. Let me read it carefully.Assistant
This GH7796 test (added in the HEAD commit) is critical — it asserts
expect(upSnapshot).toEqual(createSnapshot). With my fix ("up doesn't write"), the snapshot is unchanged afterup(), soupSnapshot === createSnapshottrivially. Let me verify the GH7773 test too.Assistant
GH7773 only tests
create()(noup()), so unaffected. GH7796 wantsupSnapshot.toEqual(createSnapshot)— my fix satisfies this trivially (snapshot unchanged). Let me check the Migrator.test.ts snapshot test (line 112) and mssql tests.Assistant
The Migrator.test.ts snapshot test uses
create(), notup(), so unaffected. Let me check the mssql migrator snapshot test.Assistant
The mssql snapshot test only uses
create(). Now let me verify the diverged-metadata scenario — that adding an entity and runningup()(for an existing migration) does NOT causecreate()to silently skip the new entity's migration. This is the key safety property of "up doesn't write".Assistant
Let me simplify the diverged-metadata test using file-based migrations.
Assistant
The diverged-metadata scenario works correctly — no silent migration skip. This confirms "up doesn't write" is the safe choice. Let me clean up repro files and check if MySQL/Postgres are available to run GH7796/GH7773.
Assistant
No external DBs available. GH7796 passes trivially with my fix (snapshot unchanged →
upSnapshot === createSnapshot), and GH7773 only usescreate(). Let me verify my final change and check allstoreCurrentSchemacall sites.Assistant
The diff is clean and minimal. Let me rebuild and run the full sqlite migrator suite plus related sqlite issue tests one final time.
Assistant
Only the
migration up and down both write the snapshottest fails — this is exactly the test #7235 created to encode its (now-reverted) behavior. The canonical c5ebd0a84 version of this test ("migration up should not write the snapshot") assertsstoreCurrentSchemais NOT called onup, which my fix satisfies. Let me confirm no stray files remain and verify the final state.Assistant
Clean — only
packages/migrations/src/Migrator.tsis modified. Let me check for any leftover temp directories from repro runs.Assistant
temp/is gitignored. Let me do a final review of the complete change.Assistant
The change is complete and correct. Let me run the libsql migrator tests (same Migrator code path) as a final sanity check.
Assistant
The 7 snapshot tests pass except the single one that explicitly encodes the reverted #7235 up-write behavior — which is exactly what the issue identifies as wrong. Let me do a final TypeScript compile check on the migrations package to ensure no errors.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.