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.
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 start by understanding the issue. The bug is about
orm.migrator.up()rewriting the snapshot incorrectly. When migrations are run, the snapshot file gets changed incorrectly.Let me explore the repository structure first and then look at the reproduction repo to understand the issue better.
Let me start by exploring the repository structure.
Assistant
This is a monorepo with packages. Let me explore the packages directory to find the migrator-related code.
Assistant
Let me look at the migrations package since that's where the migrator code likely lives
Assistant
Let me look at the source files in the migrations package.
Assistant
The Migrator.ts file likely contains the
upmethod. Let me read it.Assistant
Let me try the correct syntax for open.
Assistant
Let me see the rest of the file, particularly around the
upmethod and snapshot handling.Assistant
Let me continue scrolling down to see the rest.
Assistant
Let me see the rest of the file.
Assistant
Let me see the rest of the file
Assistant
Now I need to look at the
DatabaseSchemaclass to understand how it serializes, andgetTargetSchemato understand the differenceAssistant
Let me look for DatabaseSchema and DatabaseTable in the SQL package.
Assistant
Let me open the DatabaseSchema file.
Assistant
Let me search for the
toJSONmethod in DatabaseSchema.Assistant
Let me read from line 650.
Assistant
Now let me understand how
getTargetSchemaworks. Let me search for itAssistant
Let me look at
getTargetSchemain SqlSchemaGenerator.Assistant
Let me find the static
createmethod onDatabaseSchemaAssistant
Let me look at the static
createmethod in DatabaseSchema.Assistant
Now let me look at
DatabaseTable.toJSONsince it's what gets serialized.Assistant
Let me look at DatabaseTable.toJSON.
Assistant
Let me see the rest of
toJSONin DatabaseTable.Assistant
Let me look at
DatabaseSchema.fromMetadatato understand how it builds the schema from metadataAssistant
Let me continue reading
fromMetadataAssistant
Let me look at recent git history related to this file to understand recent changes.
Assistant
Let me look at the diff for these commits.
Assistant
Let me look at the migration tests to understand the issue
Assistant
Let me look at relevant tests in Migrator.sqlite.test.ts to see if there are snapshot tests
Assistant
Let me look at the test around line 188 (GH #7607) and line 223 (GH #7234)
Assistant
Let me look at
AbstractMigratorto understand the baserunMigrationsmethodAssistant
Let me look at the AbstractMigrator.
Assistant
Let me find
runMigrationsin AbstractMigratorAssistant
Let me look at the base
runMigrationsat line 715Assistant
Let me look at the SqliteSchemaHelper to understand how it loads schema information
Assistant
Let me look at
loadInformationSchemain SchemaHelperAssistant
Let me look at the SchemaHelper abstract class
Assistant
Let me find
loadInformationSchemain SqliteSchemaHelperAssistant
Let me look at
loadInformationSchemain SqliteSchemaHelperAssistant
Let me look at
DatabaseTable.init()to understand how it processes introspected dataAssistant
Let me look at
DatabaseTable.init()at line 112Assistant
Let me look at the most recent commit that changed Migrator.ts
Assistant
Let me look at the commit that added snapshot writing on up/down
Assistant
Let me run the migration tests to see if there are any failures related to snapshot serialization
Assistant
Let me look at how SQLite
getColumnsworks to understand what fields are returned fromAssistant
Let me look at
getColumnsat line 400Assistant
Let me look at
getOrderedMetadatamore carefullyAssistant
Let me look at the base
getOrderedMetadataAssistant
Let me look at the base
getOrderedMetadataat line 125 of AbstractSchemaGeneratorAssistant
Let me check the current version tag of the repository
Assistant
Interesting! Let me look at the most recent commit about stabilizing MySQL snapshot serialization
Assistant
Let me create a reproduction script to understand the issue better. Let me first look at how the test setup is done
Assistant
Let me search for
ignoreSchemaChangesto understand when it's setAssistant
Let me create a simple reproduction script to understand the issue better
Assistant
Let me run the reproduction
Assistant
Let me try with actual entities to reproduce
Assistant
Let me look at how the test entities are set up and use that approach instead
Assistant
Let me look at the bootstrap entities setup
Assistant
Let me check what
getDefaultSchemaNamereturns forAssistant
Let me check SqlitePlatform's default schema name
Assistant
Let me look for existing issue tests related to snapshot/GH issues
Assistant
Let me look for issue tests related to snapshots/m
Assistant
Let me check
inferLengthFromColumnTypefor SQLite to understand how length is recoveredAssistant
Let me look at the base
inferLengthFromColumnTypein SchemaAssistant
Let me check the current version
Assistant
Let me check the migrations package version
Assistant
Let me look at how
getSchemaNameworks inDatabaseSchemaAssistant
Let me look at
getSchemaNameat line 546Assistant
Let me look at the actual entity schemas used in the test
Assistant
Let me look at BaseEntity5 and FooBar4 to understand the entity sche
Assistant
Let me look at FooBar4
Assistant
I believe I've found the issue. Let me verify my understanding by looking at how
checkSchemauses the snapshot and howrunMigrationswrites it.The issue is:
runMigrationsstores the snapshot from database introspection (DatabaseSchema.create), whilecreate/createInitialstore it from entity metadata (getTargetSchema). These two paths can produce different results because:DatabaseSchema.createincludes ALL tables from the database, whilegetTargetSchemaonly includes tables from entity metadata- Certain column attributes might serialize differently between introspection and metadata
The fix should be to use
getTargetSchema()inrunMigrationsinstead ofDatabaseSchema.create(), making the snapshot consistently represent the target schema from metadata.Let me confirm by looking at how
storeCurrentSchemais called:Assistant
Let me now look at the full
runMigrationsmethod and implement the fixAssistant
Now I have a clear understanding of the issue. The
runMigrationsmethod creates a snapshot from database introspection (DatabaseSchema.create), whilecreate/createInitialmethods create it from entity metadata (getTargetSchema). These two sources can produce different results.The fix is to make
runMigrationsalso usegetTargetSchema()viastoreCurrentSchema()without arguments, just likecreateandcreateInitialdo.Let me implement the fix
Assistant
Now let me run the tests to see if the changes are correct
Assistant
All SQLite tests pass. Let me also run other migration tests to ensure nothing is
Assistant
PostgreSQL server isn't available - those failures are unrelated. Let me run the general Migrator test
Assistant
These failures are pre-existing (no MySQL server). Let me run the CLI migration tests and other SQLite-related tests
Assistant
Let me run a few more relevant
Assistant
Let me also run the specific test related to the snapshot serialization
Assistant
Let me check the final state of my
Assistant
Let me check if the
Utilsimport is still neededAssistant
Utilsis no longer used. Let me remove it from the importsAssistant
Let me verify Utils is not used elsewhere in the
Assistant
Let me also check if
MigrateOptionsisAssistant
Good,
MigrateOptionsis still used. Let me rerun the SQLite migration tests to ensure everything still passes after the import cleanupAssistant
All 24 tests pass. Let me also check for TypeScript errors
Assistant
TypeScript check passes. Let me verify
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.