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 snapshots incorrectly. When migrations are run, the snapshot gets corrupted with wrong data.Let me explore the repository structure to understand what we're dealing with.
Assistant
This is a monorepo. Let me look at the packages directory to understand the structure better.
Assistant
The issue relates to the migrator. Let me look at the migrations package and also check the reproduction repo for more
Assistant
Let me look at the source files in the migrations package.
Assistant
Let me look at the Migrator.ts file, as that likely contains the
up()method and snapshot logicAssistant
Let me just cat the file first to see the contents.
Assistant
Let me look at the DatabaseSchema class to understand how it serializes to JSON.
Assistant
Let me look at DatabaseSchema.
Assistant
Let me look at the DatabaseTable class to see its toJSON method:
Assistant
Let me check the git log for recent changes related to snapshot or migrations.
Assistant
Let me check the most recent commit which seems related to snapshot serialization.
Assistant
Let me look at the test for GH7796 and the current changes to understand what's been addressed and what might remain
Assistant
Let me search for any existing tests or issues related to snapshot mismatches for SQLite specifically
Assistant
Let me look at the test for snapshot-related issues and see what exists
Assistant
Let me search for test files related to migration snapshot
Assistant
Let me look at the SQLite migrator test for snapshot-related tests
Assistant
Let me look at the GH #7607 test and the GH #7234 test to see what's already tested for snapshot stability
Assistant
Let me run the existing snapshot test for SQLite to see if it passes
Assistant
Let me run the GH #7796 test to see its current state
Assistant
Let me look at the SQLite schema helper to understand how introspection works
Assistant
Let me look at the SQLite schema helper
Assistant
Let me look at the base SchemaHelper to understand how columns are
Assistant
Let me look at the SchemaHelper base class for getColumns method.
Assistant
Let me search for getColumns in the SQLite helper more carefully
Assistant
Let me look at the getColumns method in the SqliteSchemaHelper
Assistant
Let me look at the schema generator's getTargetSchema method
Assistant
Let me look at the getTargetSchema method
Assistant
Let me run all the SQLite migration tests to see their
Assistant
Let me look at the GH #7234 test more carefully
Assistant
Let me look at the test setup/fixtures to understand the test environment
Assistant
Let me look at the entities used in tests
Assistant
Let me look at the entity definitions
Assistant
Let me check the bootstrap to understand the test setup
Assistant
Let me look at the initORMSqlite function
Assistant
Let me look at the SQLite getForeignKeys implementation
Assistant
Let me look for
addRoutinesFromMetadatamethodAssistant
Let me create a reproduction script to test the snapshot consistency issue
Assistant
Let me run this script.
Assistant
Let me try a different approach using defineEntity like the tests do
Assistant
Let me run the test
Assistant
Let me fix the snapshot filename
Assistant
Let me check where the snapshot was
Assistant
Let me fix the test with the correct path and also check: the dbName has colons removed
Assistant
Let me test with more complex entities, similar to the GH 7796 test
Assistant
Let me fix the entity ordering issue
Assistant
Let me search for any open issues or PRs related to this snapshot issue
Assistant
Let me look at the SQLite platform's normalizeColumnType
Assistant
Let me look at the SQLite normalizeColumnType
Assistant
Let me check
formatDecimalon the platformAssistant
Let me look at formatDecimal
Assistant
Let me find the extractSimpleType method
Assistant
Let me look at the base Platform.extractSimpleType
Assistant
Let me look at the full DatabaseTable.toJSON method focusing on the normalization logic
Assistant
Let me look at the toJSON method
Assistant
Let me see the rest of the toJSON
Assistant
Let me check if TextType has getDefaultLength
Assistant
Let me check the SQLite default schema
Assistant
Let me check the SQLite getDefaultSchemaName
Assistant
Let me check supportsSchemas for
Assistant
Let me check supportsSchemas
Assistant
Let me look at the full diff of the HEAD commit
Assistant
Let me look at how SchemaComparator uses the fromSchema value
Assistant
Let me look at prepareSchemaForComparison
Assistant
Let me check the SQLite createTableColumn to see when AUTOINCREMENT is added
Assistant
Let me see the rest of the createTableColumn
Assistant
Let me run the GH #7607 and GH #7234 tests specifically
Assistant
Let me search for all tests that compare snapshots between create and up
Assistant
Let me look at the SQLite test schema
Assistant
Let me find and check normalizeDefaultValue for SQLite
Assistant
Let me look at the base normalizeDefaultValue in SchemaHelper
Assistant
Let me check what version the repo is at
Assistant
Let me check the package version
Assistant
Let me check migrations package version
Assistant
Let me search for any TODO or FIXME related to snapshot in the code
Assistant
Let me look at how SchemaComparator compares column properties
Assistant
Let me look at hasSameDefaultValue and the diffColumn method
Assistant
Let me look at diffColumn
Assistant
Let me check getVarcharTypeDeclarationSQL for
Assistant
Let me check SQLite's getVarcharTypeDeclarationSQL
Assistant
Let me check git log for any references to this specific issue or SQLite snapshot issues
Assistant
Let me look at the #7608 commit for context
Assistant
Let me check how SchemaComparator handles routines
Assistant
Let me create a test using the existing test infrastructure to check snapshot consistency with real
Assistant
Let me use defineEntity properly
Assistant
Let me look at the SQLite getIndexes method
Assistant
Let me check the mapIndexes method
Assistant
Let me look at map
Assistant
Let me search for where enum checks are added
Assistant
Let me search for where checks are added for enum
Assistant
Let me check the context around line 350 in DatabaseSchema.ts
Assistant
Let me search for where enum checks/constraints are created
Assistant
Let me search for where enum constraints are created during table
Assistant
Let me search for where checks are added for enum properties
Assistant
Let me search for how enum check constraints are generated
Assistant
Let me look at the SchemaHelper createTable method
Assistant
Let me search broadly for where enum check constraints are added to
Assistant
Let me search in SqlSchemaGenerator for check creation
Assistant
Let me look at the fromMetadata method more carefully to find where enum checks are added
Assistant
Let me look at the SQLite createTable method
Assistant
Let me search for where enum checks are created for the metadata path
Assistant
Let me look at the full createTableColumn for SQLite
Assistant
Let me search for where enum checks are created for SQLite specifically
Assistant
Let me look at the base createTable method in SchemaHelper
Assistant
Let me look at SqlSchemaGenerator for table
Assistant
Let me search for getEnumCheck
Assistant
Let me look at MetadataDiscovery where enum checks are created
Assistant
Let me look at the SQLite getChecks
Assistant
Let me check the base getEnumCheck
Assistant
Let me check if SQLite introspection loads table comments
Assistant
Let me check SQLite's getMapped
Assistant
Let me check Platform.getMapped
Assistant
Let me check isTableNameAllowed
Assistant
Let me check isTableNameAllowed
Assistant
Let me search for any recently modified files related to the issue
Assistant
Let me check how p.integer().default() sets default
Assistant
Let me check the p helper implementation
Assistant
Let me search for where p is exported
Assistant
Let me find the default implementation
Assistant
Let me see the default
Assistant
Let me search for how default vs defaultRaw are handled in entity definition
Assistant
Let me run the full SQLite migration tests and look at the snapshot-related ones more carefully
Assistant
Let me look at the snapshot tests and see what they cover
Assistant
Now I have a good understanding. Let me focus on identifying and fixing the specific bugs. Let me check the
toJSON()ofDatabaseSchema- I noticed it's missingroutines, andgetSchemaFromSnapshotdoesn't restore tabletriggers. Let me also check for thepartitioningissue.Let me first verify my understanding by looking at the full
DatabaseSchema.toJSON()andgetSchemaFromSnapshot()Assistant
Let me look at the full toJSON and getSchemaFromSnapshot methods side by side
Assistant
Let me see the full getSchemaFromSnapshot
Assistant
Now I understand the issue. The
toJSON()normalization inDatabaseTabledoesn't normalize thedefaultvalue casing for SQL expression defaults likeCURRENT_TIMESTAMP. The metadata path may have'current_timestamp'(from user'sdefaultRaw) while the introspection path (SQLitepragma table_xinfo) returns'CURRENT_TIMESTAMP'. This causes the snapshot to change afterup().Let me verify this theory:
Assistant
Let me check what SQLite actually returns for default
Assistant
Let me fix the column name
Assistant
Let me check with uppercase default
Assistant
Let me test with decimal defaults
Assistant
Let me check DecimalType for getDefaultLength
Assistant
Let me check inferLengthFromColumn
Assistant
Let me look at the base inferLengthFromColumn
Assistant
Let me check the PostgreSQL inferLengthFromColumn
Assistant
I found the bug! When comparing the snapshot from
create()(metadata) vsup()(introspection), theprecisionandscalevalues differ for decimal columns. SQLite introspection doesn't parseprecision/scalefrom the column type declaration (e.g.,NUMERIC(10,2)), while metadata sets them.Let me verify and fix this
Assistant
Let me look at the
initmethod in DatabaseTable.tsAssistant
Now I have a clear understanding. The bug is that SQLite introspection doesn't parse
precision/scalefrom column type declarations likeNUMERIC(10,2), but the metadata path does. This causes the snapshot to differ betweencreate()andup().Let me fix this:
Assistant
Let me use the replace command with the proper multi-line syntax
Assistant
Let me use the proper multi-line syntax with hered
Assistant
Let me try using the edit command with heredoc syntax
Assistant
Let me use node to patch the
Assistant
Let me verify the change was applied
Assistant
The regex got mangled. Let me fix it
Assistant
Let me fix the regex properly using sed
Assistant
Let me find the regex line
Assistant
Let me fix line 142 with the
Assistant
Let me verify the line
Assistant
Let me rewrite just the problematic lines precisely
Assistant
Let me verify the fix
Assistant
Now let me test the fix
Assistant
Let me run the existing tests to verify no regressions
Assistant
Let me run the broader test suite for SQL-related packages to check for reg
Assistant
The failures are all due to missing database servers (PostgreSQL, MSSQL, etc.). Let me verify the SQLite tests pass specifically
Assistant
Let me check what failed in the runtime schema test
Assistant
Let me check if that test was already failing before my change
Assistant
Let me run the test again with my change
Assistant
Let me create a comprehensive test with various column types to make sure the fix works broadly
Assistant
The entity reference issue is unrelated to my fix. Let me also fix the other issues I found:
DatabaseSchema.toJSON()missingroutinesandgetSchemaFromSnapshotmissingtriggersrestore.Let me also fix the
routinesandtriggersissuesAssistant
Let me check the routines getter
Assistant
Let me check if there's a setRoutines
Assistant
Let me now fix
getSchemaFromSnapshotto restore table triggers. Let me look at the code:Assistant
Let me fix the missing triggers restore in
getSchemaFromSnapshotand add routines support.First, let me add the triggers restore:
Assistant
Now let me add routines to
DatabaseSchema.toJSON()andgetSchemaFromSnapshot(). First, let me updatetoJSON()Assistant
Now add the
setRoutinesmethodAssistant
Now add routines restore in
getSchemaFromSnapshotAssistant
Let me run the tests to verify
Assistant
Let me verify the decimal fix still
Assistant
Let me check if defaultConstraint is set
Assistant
Let me verify all my
Assistant
Let me verify the DatabaseTable.toJSON() return value includes all table-level properties
Assistant
Let me see the return statement of DatabaseTable.toJSON()
Assistant
Let me run the full SQLite migration tests one more time
Assistant
Let me also run the schema generator tests for SQLite to check for regressions
Assistant
Let me run just SQLite schema
Assistant
Let me also run a broader set of SQLite tests
Assistant
Let me run the snapshot-related tests specifically
Assistant
Let me do a final review of all changes
Assistant
Let me run the final comprehensive test to make sure everything is working
Assistant
Let me run the reproduction test one final time to confirm the fix
Assistant
All tests pass and the fix resolves the issue. Let me clean up 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.