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 repository structure and the issue. The issue is about
orm.migrator.up()rewriting snapshots incorrectly. Let me explore the repository structure first.Let me start by understanding what this project is about and then try to reproduce the issue.
Assistant
This is a monorepo using Lerna. Let me explore the packages directory and find the relevant code for migrations/snapshots.
Assistant
The migration-related code is likely in the
migrationspackage. Let me exploreAssistant
Let me look at the Migrator.ts file since it likely contains the
up()method and snapshot handling.Assistant
Let me use the open command correctly.
Assistant
Let me look at the rest of the file, particularly the
up()method and snapshot handlingAssistant
Let me continue scrolling to find the up() method and storeCurrentSchema method.
Assistant
Let me continue scrolling to see the
storeCurrentSchemamethod and the rest of the file.Assistant
Let me see the rest of the file and then look at the
DatabaseSchemaclassAssistant
Now let me look at the
DatabaseSchemaclass to understand itstoJSONmethod.Assistant
Let me look at DatabaseSchema.ts to understand how it serializes to JSON.
Assistant
The open command seems to be different. Let me try again with the correct syntax.
Assistant
Let me look for the
toJSONmethod in DatabaseSchema:Assistant
Let me see the
toJSONmethod:Assistant
Let me look at the DatabaseTable toJSON method
Assistant
Let me find the toJSON in DatabaseTable
Assistant
Let me see the toJSON method at line 1304.
Assistant
Let me see the rest of toJSON.
Assistant
Let me look at how
DatabaseSchema.createworks - this is the method used to create a schema from theAssistant
Let me look at the
createmethod at line 152Assistant
Let me open DatabaseSchema.ts to see the
createmethodAssistant
Let me look at the SchemaHelper to understand how columns are loaded from the
Assistant
Let me look at the SQLite schema helper implementation
Assistant
Let me find the SQLite schema helper
Assistant
Let me look at the SQLite schema helper
Assistant
Let me find the
loadInformationSchemamethod in the SQLite schemaAssistant
Let me see the
loadInformationSchemamethodAssistant
Let me look at the
initmethod in DatabaseTableAssistant
Let me look at the
initmethod at line 112Assistant
Let me open DatabaseTable.ts at line 112 to see the init method
Assistant
Let me look at git history for recent changes related to snapshots
Assistant
Let me look at the base
SchemaHelper.getColumnsand the SQLite overrideAssistant
Let me look at the getColumns method at line 400 in SqliteSchema
Assistant
Let me open the SqliteSchemaHelper file and find the getColumns
Assistant
Let me look for the
normalizeColumnTypemethod in the SQLAssistant
Let me look at the SQLite normalizeColumn
Assistant
Let me look at existing migration tests for snapshots
Assistant
Let me look at the Migrator.test.ts for snapshot-related tests
Assistant
Let me look at line 112
Assistant
Let me open the correct test file
Assistant
Let me also look at the SQLite-specific migration tests
Assistant
Let me look at the GH #7607 and GH #7234 tests in
Assistant
Let me open the SQLite migration test file
Assistant
Let me continue reading the GH #7234 test
Assistant
Let me look at the toJSON test at line 716 and surrounding code
Assistant
Let me check the current version of the package
Assistant
Let me run the existing tests related to snapshots to see if they
Assistant
Let me look at the recent commit related to snapshot serialization
Assistant
Let me look at the recent GH issues related to migrator snap
Assistant
Let me look at
DatabaseSchema.fromMetadatato understand how metadata creates the schemaAssistant
Let me look at the full
fromMetadatamethodAssistant
Let me open DatabaseSchema.ts to see
fromMetadataAssistant
Let me continue scrolling
Assistant
Let me check
extractSimpleTypein the base platformAssistant
Let me look at the base
extractSimpleTypeand `normalizeColumnAssistant
Let me look at the test entities used in the SQLite migration tests
Assistant
Let me check the entities used in tests
Assistant
Let me look at the entity definitions
Assistant
Let me look at the test
Assistant
Let me look at the AbstractMigrator class
Assistant
Let me look at the AbstractMigrator
Assistant
Let me look at the
runMigrationsmethod in AbstractMigratorAssistant
Let me look at the
runMigrationsin AbstractMigrator at line 715Assistant
Let me look at
SqlSchemaGenerator.getTargetSchemaAssistant
Let me look at
getTargetSchemaAssistant
Let me try to create a reproduction script. First, let me see how the tests set up the ORM instance
Assistant
Let me look at the
initORMSqlitesetupAssistant
Let me check
inferLengthFromColumnTypein the SQLite schema helperAssistant
Let me check the base Schema
Assistant
Let me see the method
Assistant
Let me check how string column types are resolved for
Assistant
Let me check
initORMSqlitemore carefullyAssistant
Let me create a reproduction script to understand the issue
Assistant
Let me try running the reproduction script
Assistant
Let me fix the
Assistant
Let me run the script
Assistant
Let me check what mapped type is used for string in SQLite
Assistant
Let me check if TextType has
getDefaultLengthand how column types are setAssistant
Let me check for recent issues related to snapshot and migrator in the changelog
Assistant
Let me look at the GH7773 test to understand recent snapshot
Assistant
Let me look at the GH7796 test which was the most recent fix
Assistant
Let me create a more comprehensive reproduction script
Assistant
Let me run the extended reproduction
Assistant
Let me fix the entity
Assistant
Let me run the reproduction script
Assistant
Let me check what mapped type is used for float/real in
Assistant
Let me search for the
tobject definition to see what keys itAssistant
Let me look at the types object
Assistant
Let me check
Utils.keysAssistant
Let me find the
keysstatic methodAssistant
Let me look at
Utils.keysat line 1097Assistant
Let me open Utils.ts at line 1097
Assistant
Let me check what happens when no type matches in getDefaultMapped
Assistant
Let me look at the Platform.ts getDefaultMappedType switch statement after line 450
Assistant
Let me check what git tags exist
Assistant
Let me look at recent changes to the Migrator
Assistant
Let me look at the diff from that commit
Assistant
Let me see the Migrator.ts diff from this commit
Assistant
Let me look at the DatabaseTable.ts diff from the same
Assistant
Let me look at the latest commit's changes to DatabaseTable.ts
Assistant
Let me search for any open issues or tests related to version 7.1.2
Assistant
Let me look at how
ORM_TYPEis usedAssistant
Let me check the ORM_TYPE symbol definition
Assistant
Let me look at the Type.ts file for ORM_TYPE usage
Assistant
Let me try a more realistic reproduction: create a normal migration, then run up
Assistant
Let me run this
Assistant
Let me test the round-trip stability of the toJSON/snapshot mechanism
Assistant
Let me run this more complex test
Assistant
Let me search for any references to the reproduction repo or related issues
Assistant
Let me check if the
toJSONmappedType lookup has a known issueAssistant
Let me check how SQLite DDL handles autoincrement vs
Assistant
Let me look at those lines
Assistant
Let me open the SQLite schema helper at line 250
Assistant
Let me look at the package versions to understand where we are
Assistant
Let me check the SQLite datetime type declaration
Assistant
Let me look at the SQLite datetime type declaration
Assistant
Let me open the SqlitePlatform.ts at line 32
Assistant
Let me look at the git log more carefully to see if there are any branches with upcoming changes
Assistant
Let me write a test for the round-trip stability of toJSON vs getSchemaFromSnapshot
Assistant
Let me run the round-trip test
Assistant
Let me fix the script
Assistant
Let me run it
Assistant
Let me check what
t.floatresolves toAssistant
Let me check if there's a way to build and see what's different
Assistant
Let me try to build and run the relevant tests
Assistant
I think I found the issue! Let me compare the
DatabaseSchema.createcalls. InrunMigrations,skipTablesandskipViewsareundefined, but inprepareSchemaForComparison, they're passed from options. Let me verify.Assistant
Let me look at those lines in SqlSchemaGenerator
Assistant
I found the issue! In
runMigrations,DatabaseSchema.createis called withundefinedforskipTablesandskipViews, but the schema generator passes these options in other places likeprepareSchemaForComparison. This means:- When
create()generates a migration, it usesskipTables/skipViewsfrom config - When
up()stores the snapshot, it does NOT useskipTables/skipViews - This causes the snapshot after
up()to include tables/views that were skipped duringcreate()
Let me verify and fix this:
- When
Assistant
Let me check the SchemaGenerator options
Assistant
Let me look at how the SqlSchemaGenerator accesses its options
Assistant
Let me check the SchemaGenerator class for its options property
Assistant
Let me find the SchemaGenerator base
Assistant
Let me look at AbstractSchemaGenerator for the options property
Assistant
Let me look at
getOrderedMetadatain the base classAssistant
Let me look at
getOrderedMetadataAssistant
I found the issue. In
Migrator.runMigrations,DatabaseSchema.createis called withundefinedforskipTablesandskipViews, but when the schema generator creates the snapshot duringcreate(), it passes these options from the config. Let me fix this.Assistant
Now I'll fix the issue by passing
skipTablesandskipViewstoDatabaseSchema.createin therunMigrationsmethod.Assistant
Let me use the correct edit syntax
Assistant
Let me run the migration tests to make sure my change doesn't break anything
Assistant
Let me check the commit history to see when
collationwas added to toJSONAssistant
Let me check the state before the latest commit
Assistant
Let me see the previous toJSON to see what changed
Assistant
I found another issue! The
getSchemaFromSnapshotmethod doesn't readtriggersfrom the snapshot JSON, even thoughtoJSONincludes them. Let me fix this.Assistant
Let me check if DatabaseTable has setTriggers and if the toJSON includes triggers properly
Assistant
Now let me fix the
getSchemaFromSnapshotto also readtriggersfrom the JSON.Assistant
Let me use replace to add the triggers line
Assistant
Let me run the tests again to verify both changes
Assistant
Let me also run the main Migrator test suite
Assistant
The
Migrator.test.tsfailures are due to missing database connections (need PostgreSQL/MySQL), not related to my changes. Let me verify my changes by running the specific issue tests and the SQLite migrator tests.Assistant
Let me check if there are other places that call
DatabaseSchema.createwithout the proper filtersAssistant
Let me look at the
validateInitialMigrationcallAssistant
Let me verify my changes by looking at the git diff
Assistant
Let me run the broader SQLite migration tests to make sure nothing is broken
Assistant
Let me verify the
skipTablesandskipViewstypes match whatDatabaseSchema.createexpectsAssistant
Let me verify the DatabaseSchema.create parameter types
Assistant
Let me run all SQLite-related migration tests one final time to ensure everything works
Assistant
Let me verify the final state of the file
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.