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 understand the issue:
orm.migrator.up()rewrites snapshot incorrectly. This is about MikroORM's migration system. When running migrations, the snapshot file gets rewritten with incorrect content.Let me start by exploring the repository structure and understanding the codebase.
Assistant
This is a monorepo. Let me explore the packages directory to understand the structure.
Assistant
Let me look at git log for recent changes related to snapshot/migration and explore the migrations package.
Assistant
Let me look at recent changes related to snapshot and migration, and explore the relevant code
Assistant
Let me look at the migration-related code. Let me first check the structure of the migrations package and the core package
Assistant
Let me look at the migrations source code and also search for "snapshot" related code in the core package.
Assistant
Let me look at the Migrator.ts and search for snapshot-related code across the packages.
Assistant
Let me look at the DatabaseSchema and DatabaseTable classes to understand how the snapshot serialization works.
Assistant
Let me look at these files.
Assistant
I need to run one command at a time.
Assistant
Now let me look at the DatabaseTable's
toJSON:Assistant
Let me find the
getTargetSchemamethod used in the migrator:Assistant
Let me look at the
getTargetSchemamethod in the SqlSchemaGenerator:Assistant
Let me read around line 82 to see the
getTargetSchemamethod implementation.Assistant
Let me look at the test file for migrator snapshot behavior to understand what might be going
Assistant
Let me look at the GH7796 test which seems closely related:
Assistant
Let me look at the specific snapshot-related tests in the SQLite migrator test:
Assistant
Let me look at the GH7773 test:
Assistant
Let me look at the sqlite SchemaHelper to understand how introspection works:
Assistant
Let me look at the SqliteSchemaHelper:
Assistant
Let me look at the most recent commit's changes:
Assistant
Let me look at the
getColumnsmethod in SqliteSchemaHelper:Assistant
Let me look at the
getColumnsmethod:Assistant
Let me look at the AbstractMigrator class
Assistant
Let me look at the AbstractMigrator:
Assistant
Let me look at the SqlitePlatform to understand how column types are normalized
Assistant
Let me read around those lines:
Assistant
Let me look at
supportsUnsignedandextractSimpleType:Assistant
Let me look at
supportsUnsignedand other relevant platform methods:Assistant
Let me search in Platform.ts
Assistant
Let me look at the
supportsUnsignedin Platform.ts:Assistant
Let me look at
addColumnFromPropertymore carefully to see howuniqueis set:Assistant
Let me look at the code around line 255:
Assistant
Let me find the
extractSimpleTypemethod:Assistant
Let me look at recent git changes to relevant files:
Assistant
Let me look at commit caec0616c which is about stabilizing migration snapshot serialization:
Assistant
Let me check the default migration options:
Assistant
Let me look for any issues or tests related to this specific problem by searching for recent changes or test patterns
Assistant
Let me look at the diff for the commit that fixed MySQL snapshot serialization to see if it introduced any issues for SQLite:
Assistant
Let me look at the full diff of the last commit:
Assistant
Let me check how
addTablehandles the comment parameter:Assistant
Let me look at the
addTablemethod and trace the comment:Assistant
Let me run the relevant test to see if there's a current failure:
Assistant
Let me run the SQLite snapshot tests to see if there's a failure:
Assistant
Let me create a reproduction script to understand the issue better:
Assistant
Let me run the reproduction script:
Assistant
Let me look at how the existing tests set up entities and create a better reproduction:
Assistant
Let me look at how the test ORM is initialized:
Assistant
Let me look around line 319 in bootstrap.ts:
Assistant
Let me look at the GH #7607 test (snapshot serialization stable test):
Assistant
Let me look at the
extractSimpleTypemethod:Assistant
Let me look at MULTI_WORD_SQL_TYPES:
Assistant
Let me look at the full list:
Assistant
Let me look at
normalizeDefaultValuein the SchemaHelper:Assistant
Let me look at the normalizeDefaultValue function:
Assistant
Let me look at the test entities used in the SQLite test:
Assistant
Let me look at the test entities:
Assistant
Let me look at FooBar4 and FooBaz4:
Assistant
Let me also check FooBaz4:
Assistant
Let me check the current version:
Assistant
Let me check the core package version:
Assistant
Let me look at how
diffColumnhandles precision/scale in SchemaComparator:Assistant
Let me look at lines 1290-1310:
Assistant
Let me look at
inferLengthFromColumnTypein SchemaHelper:Assistant
Let me look at
inferLengthFromColumnType:Assistant
Let me check DecimalType:
Assistant
Let me check the base Type class for
getDefaultLength:Assistant
Let me check for types that define
getDefaultLength:Assistant
Let me look at the
initmethod in DatabaseTable once more:Assistant
I believe I've found the issue. Let me verify my hypothesis by checking how
init()handles columns vs howaddColumnFromProperty()handles them, specifically forprecisionandscale.Let me first create a simple test to confirm the issue
Assistant
Let me fix the import:
Assistant
Let me fix the method name:
Assistant
I've confirmed the bug. The
precisionandscalefields differ between the metadata and introspection snapshots for decimal columns. Aftercreate()(metadata path), they're"precision":10,"scale":2, but afterup()(introspection path), they're"precision":null,"scale":null.Now let me fix the
init()method inDatabaseTable.tsto also parse precision/scale from the column type:Assistant
Let me use the open command properly.
Assistant
Now I'll add precision/scale parsing in the
init()method. This will make introspection match metadata for decimal columns:Assistant
Let me use the replace command instead. The file is already open. Let me replace the specific lines:
Assistant
Now let me run the reproduction script again to verify the fix:
Assistant
The fix works for the decimal precision/scale case. But let me also check for other potential differences by creating a more comprehensive test:
Assistant
Let me run a more comprehensive test with enums and comments:
Assistant
I see there are also differences in the
commentfield. Let me check if SQLite introspection can extract column comments:Assistant
Let me look at the
parseTableDefinitionmethod:Assistant
Let me check how SQLite handles column comments in DDL:
Assistant
Let me check for boolean default differences specifically:
Assistant
Good - the snapshots are equal for entities without comments. The precision/scale fix works. Let me now run the existing test suite to ensure nothing is broken:
Assistant
Let me also run the PostgreSQL migration tests:
Assistant
Let me run the related issue tests:
Assistant
Let me also check if there's a GH7796-equivalent SQLite test I should be aware of. Let me also think about what other tests to run. Let me run the full migration test suite for SQLite:
Assistant
Let me run the specific snapshot test for SQLite to confirm:
Assistant
Let me try a different test filter:
Assistant
Good, the GH #7607 test passes. Let me also verify the GH #7234 test:
Assistant
Let me run the schema generator tests for SQLite:
Assistant
Let me try running the SQLite schema generator tests specifically:
Assistant
Let me check how column comments are generated in the DDL:
Assistant
Let me check how SQLite handles column comments in DDL:
Assistant
Let me check if test entities have column comments that could cause the GH #7607 test to fail if comments were an issue:
Assistant
Let me look at how SQLite gets foreign keys:
Assistant
Let me also look at whether foreign key rule casing could be an issue. Let me check the
normalizeFkfunction intoJSON:Assistant
Let me look at the
normalizeFkfunction:Assistant
Let me find and look at
normalizeFk:Assistant
Let me read the normalizeFk function:
Assistant
My fix addresses the precision/scale issue for decimal columns. Let me now run all the SQLite-specific tests to ensure nothing is broken
Assistant
All tests pass. Let me also check if there are any other related tests I should run:
Assistant
Let me check the migrator test setup:
Assistant
Let me verify the final state of the edited code:
Assistant
The fix looks good. Let me also clean up my temporary scripts:
Assistant
Let me also check the boolean default case to make sure it doesn't cause differences:
Assistant
Let me clean up and run the final comprehensive test:
Assistant
Let me run all SQLite-related tests one final time:
Assistant
All 34 tests pass. Let me verify the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.