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
Let me start by exploring the repository structure to understand what we're working with.
Assistant
me look at the linked reproduction repository to understand the issue:
Assistant
Let me look at the migration runner and look for the part where the schema is generated and stored.
Assistant
me look at the SchemaGenerator:
Assistant
me look at how the columns are loaded from the database:
Assistant
Let me look at the getColumns method in the SQLite schema helper to understand how column comments are loaded:
Assistant
Now I understand. The
getColumnsmethod doesn't include the column comment when loading from the DB. SQLite stores column comments as/* comment */in the DDL. Let me search for how this is handled:Assistant
Let me look at parseTableDefinition to understand:
Assistant
Now I understand the issue. Let me trace through the flow:
migrator.up()runs the migration- Then it calls
DatabaseSchema.createto read the schema from the DB loadInformationSchemacallsgetColumns, which doesn't return a commentstoreCurrentSchemawrites the schema without the comment
Let me look at the existing tests for similar behavior:
Assistant
Let me look at how existing snapshot tests are written:
Assistant
Now let me first reproduce the issue from the original repo:
Assistant
The repro doesn't have a snapshot. Let me set up a similar repro locally to confirm the issue:
Assistant
let me check the git history for related commits and understand the design intent:
Assistant
Wait! Let me check the current state of the code more carefully. The previous commit
c5ebd0a84was specifically about removing the snapshot write onup. So perhaps the bug has already been fixed in a different commit?Assistant
Hmm, so the fix from c5ebd0a84 has been REVERTED in this current state. The fix only checked
method === 'down', but now it always writes the snapshot for both up and down.Let me look at git history more carefully:
Assistant
me first check exactly what's happening. The SQLite schema helper doesn't read column comments. Let me see how SQLite stores comments:
Assistant
Let me check what changed in commit
caec0616cwhich was the normalization commit:Assistant
Now I have a clear picture. Let me set up a reproduction script first:
Assistant
Let me set up a reproduction script:
Assistant
Let me create a reproduction script to verify the bug:
Assistant
Let me write a different reproduction - one that runs in the test framework:
Assistant
Let me create a test using the existing test infrastructure:
Assistant
I need to import the decorators from
@mikro-orm/decorators/legacy:Assistant
I've successfully reproduced the bug. Now let me fix it.
Looking at the design history:
c5ebd0a84- Originally the fix was to skip snapshot write onup511d8adb4- Reverted this to write on both up/down for consistency, but introduced the bug because of DB introspection losing info
The right fix is to not write the snapshot on
up. Let me look at the current code more carefully to make the fix:Assistant
Let me look at how the tests are structured to understand the expected behavior:
Assistant
Now I understand the bug and the design history. Let me trace through what should happen:
migrator.create()writes the snapshot from entity metadata (with comments)migrator.up()overwrites the snapshot from DB introspection (without comments for SQLite)- The fix should be: don't overwrite the snapshot on
upsince it's already correct fromcreate()
Let me look at the existing code and make the fix:
Assistant
Now I'll make the fix. The change is to not overwrite the snapshot on
upsince it's already correct fromcreate():Assistant
The comment got placed above the method instead of inside. Let me fix this:
Assistant
Let me directly edit the file with sed:
Assistant
Let me use Node.js to do the replacement since it's complex:
Assistant
Now let me run my reproduction test to verify the fix:
Assistant
My reproduction test passes. Now let me run the existing migrator tests to see what's broken:
Assistant
test that fails is one that explicitly tests the buggy behavior - it asserts that
storeCurrentSchemais called onup, which is exactly the bug we're fixing. Per the instructions, this is acceptable.Let me also check the postgres tests to make sure my fix doesn't break anything else:
Assistant
tests are skipped due to no postgres available. Let me check what the test result would look like:
Assistant
Let me check the
migration up/down succeed on read-only filesystemtest to make sure my fix handles it:Assistant
My fix matches the original behavior from
c5ebd0a84. Let me also check other migration tests to make sure my fix doesn't break anything:Assistant
me verify the unrelated failures are pre-existing by running the test on the original code:
Assistant
, the mssql test failure is pre-existing. Let me restore my changes:
Assistant
Assistant
me think about this from another angle. The fix should be complete. Let me think about what happens in the down scenario more carefully.
Looking at the user's bug report, the issue is specifically about
migrator.up()overwriting the snapshot. The fix is to not overwrite onup. Thedowncase is unchanged.Now let me also think about whether I need to worry about the
downcase. If the user runsmigrator.down(), the snapshot is overwritten from the DB. If the DB doesn't have column comments (SQLite), the comments are lost. But this is a separate issue not reported by the user.Let me run my test once more and confirm everything works:
Assistant
Let me also think about edge cases. Let me check what happens when there's no existing snapshot:
Assistant
Let me use a regular migration instead of initial for the down test:
Assistant
All my reproduction tests pass. Now let me also check the
downcase - this is important because we want to make sure the snapshot still gets updated ondown:Assistant
Wait, let me check the actual snapshot content more carefully:
Assistant
Let me simplify the test - the file replacement approach is too fragile:
Assistant
I need to first call
create()to generate the snapshot:Assistant
Let me simplify the test - the 3rd test is too complex. Let me focus on what we need:
Assistant
My reproduction tests pass. Now let me clean up the test file and verify everything is good:
Assistant
let me also reproduce the issue using the original reproduction to make sure my fix works in that scenario:
Assistant
Let me run all migration tests for SQLite to make sure my fix is solid:
Assistant
My fix is complete. Let me also verify the final state of the file:
Assistant
Let me also verify there are no other migration tests that I should be aware of:
Assistant
me also verify the test
migration up/down succeed on read-only filesystemstill passes:Assistant
Let me also check the
getPendingand other tests:Assistant
me also check whether there are any tests related to the
migrator.upwith external transaction (which previously had a deadlock issue):Assistant
Let me view the postgres test that has the deadlock test:
Assistant
Let me verify the fix doesn't introduce any issues by running the complete Migrator test suite:
Assistant
Let me look at the failing test one more time to fully understand what it tests:
Assistant
Let me also check what postgres test would be affected:
Assistant
Let me also check the docs to see if they need to be updated:
Assistant
The docs say that
migration:upandmigration:downrewrite the snapshot. With my fix, onlymigration:downdoes this. Let me update the docs:Assistant
Let me also check if there's a versioned docs file that needs updating:
Assistant
versioned docs should remain unchanged. Let me check the final state:
Assistant
Let me now run the failing test once more to confirm:
Assistant
also verify the other snapshot-related tests pass:
Assistant
me do a final sanity check by viewing the changes:
Assistant
me run a final check on the entire Migrator test suite for SQLite:
Assistant
Assistant
The previous response failed because I had formatting issues. Let me re-attempt the verification
Assistant
Let me put the test file in the mikro-orm directory directly:
Assistant
My fix works. The reproduction test confirms the bug is fixed. Now let me clean up the temp test file:
Assistant
Now let me run the entire SQLite migration test suite one more time to confirm everything is in order:
Assistant
Final summary of my changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.