Skip to content

Commit ea816b6

Browse files
AndriiShermanAleksandrShermanSukairo-02RomanNabukhotnyi
authored
Beta.16 (#5376)
* Add new migrations versioning * added up logic for migration tables for mssql, cockroach, sqlite, mysql * updated all migrators for rest dbs * moved CURRENT_MIGRATION_TABLE_VERSION to common folder * fixed up migration table for sqlite * updates in up migrators (name fix + sqlite fixes) * cut millis to sec + 000 * Fix migrator utils * updates + fixed up migrations * updates in up migrators * Fix mysql commutative * updates in pg migrator - check on table shape and remove "version" column * removed "version" column for all migrators + updated up-migrator for all dialects to detect by table shape * check if drizzle_migrations table exists by validating result length instead of value * added pg-effect up migrator * up-migrators - fixes in pg, effect-pg - removed export for CURRENT_MIGRATION_TABLE_VERSION * d1 migrator fix * migrator-fixes - postponed tests - uncommented some tests - fix integration tests with migrators - updated logic for sqlite,mysql proxy up migrations - fixed imports * more fixes * fixes in pg * custom wrapper for pg up migration * wrappers on sqlite and mysql up migrators * singlestore fix: looks like final fix * cockroach removed access to crdb_internal after v25. Fixed introspect * Add last check for commutative * Update brocli * Fixed migration upgrade function * Import fix * update schema * Update utils * cockroach introspect query fix * fix cockroach introspect query * add static cockroach version for docker since problems in v26 * snapshot generator * fixes in psql diff + updates in serializer-snapshot.test.ts * fix: update privilege grantee and grantor formatting in SQL statements * simplified generateLatestSnapshot and added tests * Postgres add snapshot composer for leafs * Finish skipif * fix * removed unnecessary statements * fix * up versions * Update double-conflicts check * ci/cd fix from codecs * remove guide link for now --------- Co-authored-by: Aleksandr Sherman <aleksandrserman@gmail.com> Co-authored-by: Sukairo-02 <sreka9056@gmail.com> Co-authored-by: RomanNabukhotnyi <nabukhotnyiroman@gmail.com>
1 parent 859cf75 commit ea816b6

78 files changed

Lines changed: 6926 additions & 814 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/release-feature-branch.yaml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ jobs:
8989
fail-fast: false
9090
matrix:
9191
include:
92-
- shard: int:gel
93-
dbs: [gel]
92+
# - shard: int:gel
93+
# dbs: [gel]
9494
# - shard: int:singlestore
9595
# dbs: [singlestore]
9696
# - shard: int:singlestore-core
@@ -104,7 +104,11 @@ jobs:
104104
- shard: int:sqlite
105105
dbs: []
106106
- shard: int:other
107-
dbs: [mysql, mssql, cockroach, singlestore, postgres, postgres-postgis, postgres-vector]
107+
dbs: [mysql, postgres, postgres-postgis, postgres-vector]
108+
- shard: int:seeder
109+
dbs: []
110+
- shard: int:imports
111+
dbs: []
108112
- shard: int:planetscale
109113
dbs: []
110114
- shard: int:cockroach
@@ -282,18 +286,10 @@ jobs:
282286
283287
int:bun) bun test ./tests/bun/ ;;
284288
285-
int:other)
286-
pnpm --stream vitest --reporter=verbose --silent=false run tests \
287-
--exclude ./tests/gel/ \
288-
--exclude ./tests/mysql/ \
289-
--exclude ./tests/cockroach/ \
290-
--exclude ./tests/singlestore/ \
291-
--exclude ./tests/mssql/ \
292-
--exclude ./tests/pg/ \
293-
--exclude ./tests/sqlite/ \
294-
--exclude ./tests/bun/ \
295-
--exclude tests/validators
296-
;;
289+
int:seeder) pnpm test:seeder ;;
290+
int:imports) pnpm test:imports ;;
291+
int:other) pnpm test:other ;;
292+
297293
*) echo "Unknown shard: ${{matrix.shard}}"; exit 1 ;;
298294
esac
299295
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Drizzle ORM beta.16 updates
2+
3+
We've fixed a regression in migrations introduced in `beta.13` that persisted through `beta.15`, and used the opportunity to significantly improve the entire migration infrastructure going forward.
4+
5+
After receiving github issue, we focused on not just fixing the symptoms but rethinking how migrations are tracked, validated, and applied. As we always do with Drizzle we went deeper and redesigned the underlying architecture so it can be stable for all future changes, additions, and edge cases
6+
7+
We went through how we check migrations, how we store them, and how we help developers keep their migration flow reliable. After several iterations of rewriting the `migrate` function in both ORM and Kit, adding migration table versioning, and building a new commutativity check system: here's what changed and why.
8+
9+
## What happened in beta.12–beta.15 and what caused the issue
10+
11+
On `latest` (pre-beta) versions, the migrations folder used a journal-based structure. A `meta/_journal.json` file stored a timestamp for each migration in **milliseconds**. That same millis value was stored in the database's `created_at` column and used to determine which migrations had been applied. Because the journal enforced ordering, we could simply fetch the last applied migration from the database and apply everything after it.
12+
13+
With the new v3 folder structure (introduced in beta), each migration lives in its own folder named `<YYYYMMDDHHmmss>_<name>`. This format only has **second** precision. The new structure also intentionally allows out-of-order migrations (as it should for team workflows), so we switched to reading *all* migrations from the database and comparing against all local migrations.
14+
15+
The problem appeared after `drizzle-kit up` converted the old journal structure to v3 folders. It mapped millis timestamps to the `YYYYMMDDHHmmss_name` format, stripping away the millisecond precision. So when the new migration checker compared what was in the database (millis) to what was on disk (seconds), nothing matched causing migrations to be re-applied on every run.
16+
17+
**This only affected beta users who upgraded from the journal-based format. Users on `latest` were not impacted.**
18+
19+
## How we fixed it
20+
21+
### 1. Versioned migration table
22+
23+
We introduced an internal version number for the migrations table schema. On `beta.16`, the table will automatically upgrade itself from `version_0` to `version_1` the first time you run `migrate`.
24+
25+
**Version 0** (old schema):
26+
| Column | Type |
27+
|---|---|
28+
| `id` | serial |
29+
| `hash` | text |
30+
| `created_at` | bigint (millis) |
31+
32+
**Version 1** (new schema):
33+
| Column | Type | |
34+
|---|---| ---|
35+
| `id` | serial |
36+
| `hash` | text |
37+
| `created_at` | bigint | (legacy) |
38+
| `name` | text |
39+
| `applied_at` | timestamp |
40+
| `version` | integer |
41+
42+
The `name` column stores the full folder name of the migration (e.g. `20250220153045_brave_wolverine`). The `applied_at` column records when the migration was actually executed. For pre-existing migrations that were backfilled during upgrade, `applied_at` is set to `NULL` to distinguish them from newly applied ones.
43+
44+
This versioning system means we can add more fields in the future (like migration state for rollbacks) without any disruption for developers
45+
46+
### 2. Matching by folder name instead of timestamps
47+
48+
All migrations are now checked against the **full folder name**: the combination of a 14-digit UTC timestamp and a name suffix (random or custom). Even if two migrations are generated within the same second, the name suffix guarantees uniqueness.
49+
50+
The detection logic is simple: build a set of `name` values from the database, filter local migrations whose name isn't in that set, and apply those. No more timestamp arithmetic, no more precision mismatches.
51+
52+
### 3. Automatic upgrade with smart backfilling
53+
54+
When `beta.16` detects a `version_0` table, it adds the new columns and backfills the `name` for each existing row using a multi-step matching strategy:
55+
56+
1. **Millis match**: truncate the stored millis to seconds and match against local migration folder timestamps
57+
2. **Hash tiebreaker**: if multiple migrations share the same second, use the SQL hash to pick the right one
58+
3. **Hash-only fallback**: if millis matching fails entirely, fall back to matching by hash alone
59+
60+
This means the upgrade handles all edge cases: normal single-developer projects, teams with closely-timed migrations, and even cases where the old journal data doesn't perfectly align with the new folder names.
61+
62+
### 4. New commutativity checks (`drizzle-kit check`)
63+
64+
When working in teams, multiple developers may generate migrations from the same base schema on different branches. These migrations can conflict in non-obvious ways, for example, two branches both adding a column to the same table, or one renaming a table that another is altering.
65+
66+
We built a new `drizzle-kit check` command that detects these non-commutative migrations. It works by:
67+
68+
- Building a DAG (directed acyclic graph) from snapshot `prevIds` to understand the branch structure
69+
- Finding fork points where branches diverged
70+
- Computing the DDL diff from the parent snapshot to each branch leaf
71+
- Checking for conflicting operations using a comprehensive footprint map that knows which DDL statement types can interfere with each other
72+
73+
This is available for PostgreSQL and MySQL today. If conflicts are found, the report tells you exactly which migrations on which branches are incompatible and what statements are conflicting.
74+
75+
The new folder structure also changed snapshot metadata from `prevId: string` to `prevIds: string[]`, enabling proper DAG representation of migration history across branches.
76+
77+
## Upgrading to beta.16
78+
79+
The upgrade is automatic. When you run `migrate` for the first time on `beta.16`:
80+
81+
1. Drizzle detects your migration table version
82+
2. If it's `version_0`, it adds the new columns and backfills `name` from your local migration files
83+
3. All future migrations are tracked by name
84+
4. No manual steps required
85+
86+
If you're also upgrading your migration **folder structure** from the old journal format, run `drizzle-kit up` first to convert to the v3 folder layout, then `migrate` will handle the rest.
87+
88+
## Upgrading to beta.16 is not fixing my problem
89+
90+
If you're still hitting migration issues after upgrading, please reach out to us directly - drop a message in [Discord](https://discord.gg/7NKUQWP9c8) or open a [GitHub issue](https://github.com/drizzle-team/drizzle-orm/issues/new/choose) with your migration folder structure and the contents of your migrations table. We'll help you sort it out.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Drizzle ORM beta.16 updates
2+
3+
We've fixed a regression in migrations introduced in `beta.13` that persisted through `beta.15`, and used the opportunity to significantly improve the entire migration infrastructure going forward.
4+
5+
After receiving github issue, we focused on not just fixing the symptoms but rethinking how migrations are tracked, validated, and applied. As we always do with Drizzle we went deeper and redesigned the underlying architecture so it can be stable for all future changes, additions, and edge cases
6+
7+
We went through how we check migrations, how we store them, and how we help developers keep their migration flow reliable. After several iterations of rewriting the `migrate` function in both ORM and Kit, adding migration table versioning, and building a new commutativity check system: here's what changed and why.
8+
9+
## What happened in beta.12–beta.15 and what caused the issue
10+
11+
On `latest` (pre-beta) versions, the migrations folder used a journal-based structure. A `meta/_journal.json` file stored a timestamp for each migration in **milliseconds**. That same millis value was stored in the database's `created_at` column and used to determine which migrations had been applied. Because the journal enforced ordering, we could simply fetch the last applied migration from the database and apply everything after it.
12+
13+
With the new v3 folder structure (introduced in beta), each migration lives in its own folder named `<YYYYMMDDHHmmss>_<name>`. This format only has **second** precision. The new structure also intentionally allows out-of-order migrations (as it should for team workflows), so we switched to reading *all* migrations from the database and comparing against all local migrations.
14+
15+
The problem appeared after `drizzle-kit up` converted the old journal structure to v3 folders. It mapped millis timestamps to the `YYYYMMDDHHmmss_name` format, stripping away the millisecond precision. So when the new migration checker compared what was in the database (millis) to what was on disk (seconds), nothing matched causing migrations to be re-applied on every run.
16+
17+
**This only affected beta users who upgraded from the journal-based format. Users on `latest` were not impacted.**
18+
19+
## How we fixed it
20+
21+
### 1. Versioned migration table
22+
23+
**Version 0** (old schema):
24+
| Column | Type |
25+
|---|---|
26+
| `id` | serial |
27+
| `hash` | text |
28+
| `created_at` | bigint (millis) |
29+
30+
**Version 1** (new schema):
31+
| Column | Type | |
32+
|---|---| ---|
33+
| `id` | serial |
34+
| `hash` | text |
35+
| `created_at` | bigint | (legacy) |
36+
| `name` | text |
37+
| `applied_at` | timestamp |
38+
39+
The `name` column stores the full folder name of the migration (e.g. `20250220153045_brave_wolverine`). The `applied_at` column records when the migration was actually executed. For pre-existing migrations that were backfilled during upgrade, `applied_at` is set to `NULL` to distinguish them from newly applied ones.
40+
41+
This versioning system means we can add more fields in the future (like migration state for rollbacks) without any disruption for developers
42+
43+
### 2. Matching by folder name instead of timestamps
44+
45+
All migrations are now checked against the **full folder name**: the combination of a 14-digit UTC timestamp and a name suffix (random or custom). Even if two migrations are generated within the same second, the name suffix guarantees uniqueness.
46+
47+
The detection logic is simple: build a set of `name` values from the database, filter local migrations whose name isn't in that set, and apply those. No more timestamp arithmetic, no more precision mismatches.
48+
49+
### 3. Automatic upgrade with smart backfilling
50+
51+
When `beta.16` detects a `version_0` table, it adds the new columns and backfills the `name` for each existing row using a multi-step matching strategy:
52+
53+
1. **Millis match**: truncate the stored millis to seconds and match against local migration folder timestamps
54+
2. **Hash tiebreaker**: if multiple migrations share the same second, use the SQL hash to pick the right one
55+
3. **Hash-only fallback**: if millis matching fails entirely, fall back to matching by hash alone
56+
57+
This means the upgrade handles all edge cases: normal single-developer projects, teams with closely-timed migrations, and even cases where the old journal data doesn't perfectly align with the new folder names.
58+
59+
### 4. New commutativity checks (`drizzle-kit check`)
60+
61+
When working in teams, multiple developers may generate migrations from the same base schema on different branches. These migrations can conflict in non-obvious ways, for example, two branches both adding a column to the same table, or one renaming a table that another is altering.
62+
63+
We built a new `drizzle-kit check` command that detects these non-commutative migrations. It works by:
64+
65+
- Building a DAG (directed acyclic graph) from snapshot `prevIds` to understand the branch structure
66+
- Finding fork points where branches diverged
67+
- Computing the DDL diff from the parent snapshot to each branch leaf
68+
- Checking for conflicting operations using a comprehensive footprint map that knows which DDL statement types can interfere with each other
69+
70+
This is available for PostgreSQL and MySQL today. If conflicts are found, the report tells you exactly which migrations on which branches are incompatible and what statements are conflicting.
71+
72+
The new folder structure also changed snapshot metadata from `prevId: string` to `prevIds: string[]`, enabling proper DAG representation of migration history across branches.
73+
74+
## Upgrading to beta.16
75+
76+
The upgrade is automatic. When you run `migrate` for the first time on `beta.16`:
77+
78+
1. Drizzle detects your migration table version
79+
2. If it's `version_0`, it adds the new columns and backfills `name` from your local migration files
80+
3. All future migrations are tracked by name
81+
4. No manual steps required
82+
83+
If you're also upgrading your migration **folder structure** from the old journal format, run `drizzle-kit up` first to convert to the v3 folder layout, then `migrate` will handle the rest.
84+
85+
## Upgrading to beta.16 is not fixing my problem
86+
87+
If you're still hitting migration issues after upgrading, please reach out to us directly - drop a message in [Discord](https://discord.gg/7NKUQWP9c8) or open a [GitHub issue](https://github.com/drizzle-team/drizzle-orm/issues/new/choose) with your migration folder structure and the contents of your migrations table. We'll help you sort it out.

compose/cockroach-many.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
cockroach0:
3-
image: cockroachdb/cockroach:latest
3+
image: cockroachdb/cockroach:v25.2.0
44
command: start-single-node --insecure --store=type=mem,size=2GiB
55
ports:
66
- "26260:26257"
@@ -12,7 +12,7 @@ services:
1212
retries: 60
1313

1414
cockroach1:
15-
image: cockroachdb/cockroach:latest
15+
image: cockroachdb/cockroach:v25.2.0
1616
command: start-single-node --insecure --store=type=mem,size=2GiB
1717
ports:
1818
- "26261:26257"
@@ -24,7 +24,7 @@ services:
2424
retries: 60
2525

2626
cockroach2:
27-
image: cockroachdb/cockroach:latest
27+
image: cockroachdb/cockroach:v25.2.0
2828
command: start-single-node --insecure --store=type=mem,size=2GiB
2929
ports:
3030
- "26262:26257"

compose/cockroach.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
cockroach:
3-
image: cockroachdb/cockroach:latest
3+
image: cockroachdb/cockroach:v25.2.0
44
command: start-single-node --insecure --store=type=mem,size=2GiB
55
ports:
66
- "26257:26257"

drizzle-kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-kit",
3-
"version": "1.0.0-beta.15",
3+
"version": "1.0.0-beta.16",
44
"homepage": "https://orm.drizzle.team",
55
"keywords": [
66
"drizzle",

0 commit comments

Comments
 (0)