Commutative Migrations #5005
AndriiSherman
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
As part of the migration flow upgrades with drizzle-kit, we are introducing Commutative Migrations Checking. The Postgres implementation is already complete, and we are working on extending support to other dialects
Commutative Migrations will use a new migrations folder structure, explained here. This approach is especially useful for teams of all sizes who work on the same database schema and update it frequently
There may be situations where two feature branches modify the same database entity, making the migrations non-commutative (where order matters). We will detect such cases automatically.
Case 1:
If all migrations are commutative, you can apply them in any order without consequences. (Drizzle will automatically check if migrations are commutative)
Case 2:
Suppose we have migrations
123and124on the dev/prod branches, and two new features are assigned to different team members. These create two branches:Branch A:
129→143→150Branch B:
135→145Migrations
143and145are non-commutative (marked in red). The team must manually decide which should run first. For example:To handle such cases, we will update how the
check,generate, andmigratecommands work.To handle such cases we will update how each of
check,generate,migratecommands work. Let's go one by one:drizzle-kit checkThis command now checks migration folders for commutativity and possible conflicts (non-commutative migrations)
For the example above, running
drizzle-kit checkmay produce output like this:This command is useful for CI/CD pipelines, development workflows, and general checks.
Rule of thumb for resolving conflicts:
drizzle-kit generateThis command now runs check by default to ensure that new migrations do not introduce conflicts. Possible outcomes:
Situation 1: No conflicts detected
A new migration will be generated with connections to all open branches in the migration history.
Example: When generating a new migration (152_migration), drizzle-kit will check for all migrations without a child and use them as parents. This marks the new migration as generated after all branches. We call this action "merging branches”
Situation 2: Conflict detected
If a conflict is detected,
generatewill output an error explaining how to resolve it. Typically, this requires manually removing a few migration folders, syncing the database schema downstream, and runningdrizzle-kit generateagain.Example: If
drizzle-kitdetects non-commutative migrations (145and143) after pulling from dev, you must remove migration145, sync the dev schema locally, and then generate a new migration (152). This migration will have only one parent (150), ensuring no further conflictsdrizzle-kit migrateThis command behaves the same as generate - it runs
checkby default. If a conflict is found, you will be guided through the same resolution steps. After regenerating the migration, you can safely run migrate again--ignore-conflictsIf you want to skip conflict checking, you can use the
--ignore-conflictsflag:Beta Was this translation helpful? Give feedback.
All reactions