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
I'll start by exploring the repository structure to understand the codebase.
Assistant
Now let me look at the reproduction repository to understand the issue. Let me check the
getTargetSchemaandstoreCurrentSchemamethods more closely. The issue is aboutorm.migrator.up()rewriting the snapshot incorrectly.Let me look at the
getTargetSchemamethod in the schema generator.Assistant
Now let me understand the issue. The
runMigrationsmethod in Migrator.ts callsstoreCurrentSchema(schema)after migrations run. The schema is created from the database:tsconst schema = await DatabaseSchema.create( this.em.getConnection(), this.em.getPlatform(), this.config, undefined, undefined, undefined, undefined, undefined, ctx, );But in
storeCurrentSchema, if schema is provided, it uses it directly. Otherwise it usesgetTargetSchema().The issue is that the snapshot is being rewritten incorrectly. Let me look at the reproduction repo to understand what's happening.
Let me look at the
runMigrationsin the base classAbstractMigrator.Assistant
Let me clone the reproduction repo to understand the issue.
Assistant
The snapshot has
notes.comment = "free-form notes", but the migration SQL doesn't include the comment. Aftermigrator.up(), the snapshot is rewritten by reading from the DB (which has no comment), losing the comment.Let me check the git history of the
runMigrationsmethod to understand what changed.Assistant
So before this commit, the snapshot was always written from
getTargetSchema()(i.e., from metadata). The change introduced reading the schema from the DB after migration. The rationale was that when runningmigration:down, the snapshot wasn't updated, causing subsequentmigration:createto produce empty diff since the stale snapshot still matched the target entity schema.But this change introduced a bug: reading from the DB after migration loses information that's in the metadata but not in the DB (like comments). The DB schema after migration might not perfectly reflect the metadata (e.g., the migration SQL might not include comments, or other DB-specific things).
Let me look at the follow-up commit that added the transaction context.
Assistant
Let me look at the test that was added with the original commit to understand the
downscenario.Assistant
Let me look at the current version of this test and check what the snapshot test expects now.
Assistant
Let me look at the commit that stabilized snapshot serialization (GH #7607).
Assistant
Let me examine how SQLite handles comments in the schema helper.
Assistant
SQLite supports table comments (via inline
/* ... */) but not column comments. The repro uses a column comment, which SQLite can't store, so DB introspection can't recover it. Let me reproduce the issue within the repo's test environment.Assistant
Let me check if the packages are built and tests can run.
Assistant
Tests run against source directly. Let me run the existing snapshot-related tests to establish a baseline.
Assistant
Now let me create a temporary reproduction test that mirrors the issue (entity with a column comment, SQLite can't store it).
Assistant
Let me create a temporary reproduction test file.
Assistant
I've reproduced the issue. The column comment is lost after
up(). Now let me implement the fix. Let me look at the currentrunMigrationsmethod.Assistant
Now let me check the postgres migrator tests for
downsnapshot behavior to make sure my fix won't break them.Assistant
The postgres test expects
upto use DB introspection. Let me check the current HEAD and any related recent changes.Assistant
The HEAD commit is about stabilizing MySQL snapshot serialization including
comment. Let me examine this recent fix to understand the maintainers' approach.Assistant
The HEAD commit shows the maintainers' approach: make
toJSON()consistent for both metadata and introspection paths. But SQLite fundamentally can't store column comments, so introspection can't recover them. Let me verify how SQLite handles table/column comments in introspection.Assistant
So SQLite introspection of columns doesn't extract
commentat all (no comment field). So column comments are entirely lost. Table comments:t.table_comment— let me check how SQLite gets table_comment. Let me look at thegetTablesmethod (in SchemaHelper base or SqliteSchemaHelper).Assistant
Both table and column comments are lost on SQLite introspection. Let me check all tests that depend on the snapshot content after
up.Assistant
Let me look at the GH #7424 deadlock test.
Assistant
The merge approach (introspect DB, then overlay metadata-only comments) would keep all tests passing AND fix the issue. Let me verify the postgres test's DB-vs-target diffs are structural, not comment-only.
Assistant
The postgres test's DB has structural diffs (extra columns, type changes), not comment-only diffs. The merge approach (introspect DB, then overlay metadata-only comments) keeps all tests passing AND fixes the issue. Let me verify the
DatabaseTable/Column structure forcomment.Assistant
Good. So
Columnhascomment?: string, andDatabaseTablehascomment?: stringandgetColumns(): Column[].Now let me check
DatabaseTable.getColumn(name)method to look up columns by name.Assistant
Now let me check
DatabaseSchemaAPI for table lookups.Assistant
Let me check what schema name SQLite uses (getDefaultSchemaName) and how
fromMetadatasets table schema.Assistant
I have a clear plan. The fix: when rewriting the snapshot after
up, restore metadata-only comments (which databases like SQLite can't store) from the target schema so the snapshot doesn't churn.downkeeps pure introspection (to reflect the reverted state). Let me view the exact lines to edit.Assistant
Now let me implement the fix:
Assistant
Now let me add the
#restoreMetadataCommentsprivate method. Let me add it right after therunMigrationsmethod (after line 237).Assistant
Let me verify
DatabaseTableis imported in Migrator.ts.Assistant
DatabaseTableandDatabaseSchemaare both imported. Now let me run my reproduction test.Assistant
The reproduction test passes now. Let me also test the scenario where the snapshot after
upexactly matchescreate's snapshot (full equality, not just the comment). Let me enhance the repro test temporarily to verify full equality, and also test a table comment.Assistant
Full snapshot equality holds (including table comment), and a second
createis a no-op. Now let me run the existing migration tests. First the SQLite ones.Assistant
SQLite migrator tests pass. Now let me run the postgres migrator tests (requires docker).
Assistant
Docker isn't available. Let me check if a postgres is reachable another way.
Assistant
No postgres available and no docker. I can't run the postgres tests in this environment. Let me check if there's a way to install/start postgres. Let me check available package managers.
Actually, let me check if postgres is installed but not running.
Assistant
PostgreSQL is installed. Let me start it and configure access for the tests.
Assistant
The globalSetup connects via TCP with no password. Let me configure pg_hba for trust auth.
Assistant
Postgres is now reachable. Let me check how globalSetup handles missing MySQL/MSSQL services.
Assistant
globalSetup tolerates unavailable DBs. Let me run the postgres migrator test.
Assistant
All postgres migrator tests pass, including the one that encodes the introspection behavior. Let me run the broader migrations test suite.
Assistant
Let me see the full failure list for Migrator.test.ts.
Assistant
All tests are skipped but
afterAllfails becauseormis undefined — meaningbeforeAllfailed. Let me see the actual error.Assistant
The error is
ECONNREFUSED 127.0.0.1:3308— that's MySQL, which isn't available in this environment. These failures are environmental, not from my change. Let me confirm Migrator.test.ts uses MySQL.Assistant
Migrator.test.ts uses MySQL (port 3308), which isn't available — these are environmental failures unrelated to my change. Let me try to set up MySQL/MariaDB to actually run them, since snapshot stabilization is heavily MySQL-related. Let me check the connection details.
Assistant
Let me install MariaDB to run the MySQL-based migration tests.
Assistant
Let me start a MariaDB instance on port 3308 (for the MySQL tests) with no-password root access.
Assistant
MariaDB is running on 3308. Let me run the MySQL-based Migrator.test.ts and the GH7796 test.
Assistant
6 failed, 40 passed. Let me check if these failures are pre-existing (MariaDB vs MySQL differences) by stashing my change and re-running.
Assistant
Let me verify these failures are pre-existing (MariaDB vs MySQL) by stashing my change and re-running.
Assistant
Confirmed: the same 6 tests fail on the original HEAD (without my change) — these are pre-existing MariaDB-vs-MySQL introspection differences, not caused by my fix. Let me restore my change.
Assistant
Now let me run the GH7796 test (MySQL snapshot stability) to confirm my change doesn't break it.
Assistant
GH7796 fails on MariaDB. Let me check whether this is pre-existing (without my change).
Assistant
GH7796 also fails on the original HEAD (MariaDB-vs-MySQL
updated_atprecision) — pre-existing, not from my change. Let me run the mongo migrator tests to be thorough.Assistant
Mongo migrator tests pass (unaffected). Let me verify the
getColumnimplementation and double-check my overlay handles the schema matching correctly.Assistant
getColumnkeys by column name — matching works. Let me search for other issue tests that exercisemigrator.upsnapshot writing.Assistant
GH3287 is about lazy-loading snapshots, not migration snapshots. My change is isolated to
Migrator.runMigrations. Let me run a final combined check of the sqlite + postgres migrator tests plus my repro, then clean up.Assistant
All 45 tests pass. Now let me remove the temporary reproduction test file.
Assistant
Let me review the final diff.
Assistant
The diff looks good. Let me run a TypeScript type-check on the migrations package to ensure no type errors.
Assistant
Type-check passes. Let me check the repo's formatting config to ensure my code conforms (the
findTargetTableline may be too long).Assistant
Print width is 120. Let me check if oxfmt is available and format the file.
Assistant
Line 246 is 124 chars — over the 120 limit. Let me reformat that line to comply.
Assistant
Let me run oxfmt to verify formatting and the type-check again.
Assistant
oxfmt reformatted it. Let me re-run the type-check and the reproduction + migrator tests to confirm everything still passes after formatting.
Assistant
All tests pass after formatting. Let me remove the temporary reproduction test file and do a final review.
Assistant
The fix is complete and clean. Let me do a final verification that the build/typecheck is clean and submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.