Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/app/drizzle-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ You need a way to tell Drizzle: "these migrations are already reflected in the d
`markMigrationsApplied` populates Drizzle's internal `__drizzle_migrations` tracking table with records for all existing migration files, so that `drizzle-kit migrate` treats them as already applied. This establishes a baseline — from this point forward, only new migrations will be executed.

The function:
- Reads the migration journal (`meta/_journal.json`) and SQL files from your migrations folder
- Reads migration files from your migrations folder — supports both the legacy journal format (`meta/_journal.json` with flat SQL files, from drizzle-kit 0.x) and the new folder-per-migration format (`<timestamp>_<name>/migration.sql`, from drizzle-kit 1.0.0-beta)
- Computes the SHA-256 hash for each migration (matching Drizzle's internal algorithm)
- Inserts tracking records into the `__drizzle_migrations` table
- Is **idempotent** — safe to run multiple times; already-tracked migrations are skipped
- Supports **PostgreSQL**, **MySQL**, and **CockroachDB**, with auto-detection from the journal
- Supports **PostgreSQL**, **MySQL**, and **CockroachDB**, with auto-detection from the journal or snapshot files

#### CLI

Expand Down Expand Up @@ -115,17 +115,19 @@ await sql.end()

| Option | Type | Default | Description |
|---|---|---|---|
| `migrationsFolder` | `string` | *(required)* | Path to the Drizzle migrations folder (containing `meta/_journal.json`) |
| `migrationsFolder` | `string` | *(required)* | Path to the Drizzle migrations folder. Supports both legacy format (with `meta/_journal.json`) and new folder-per-migration format (drizzle-kit 1.0.0-beta) |
| `executor` | `SqlExecutor` | *(required)* | Object with `run(sql)` and `all(sql)` methods for executing raw SQL |
| `dialect` | `'postgresql' \| 'mysql' \| 'cockroachdb'` | *(auto-detected)* | Database dialect. Auto-detected from the journal's `dialect` field if omitted |
| `dialect` | `'postgresql' \| 'mysql' \| 'cockroachdb'` | *(auto-detected)* | Database dialect. Auto-detected from the journal or snapshot files if omitted |
| `migrationsTable` | `string` | `'__drizzle_migrations'` | Name of the migrations tracking table |
| `migrationsSchema` | `string` | `'drizzle'` | Schema for the migrations table (PostgreSQL and CockroachDB only) |

#### Helper functions

`readMigrationJournal(migrationsFolder)` — reads and parses `meta/_journal.json`.
`detectMigrationFormat(migrationsFolder)` — returns `'journal'` (legacy format with `meta/_journal.json`) or `'folder'` (new folder-per-migration format).

`readMigrationEntries(migrationsFolder)` — reads all migration entries with their computed SHA-256 hashes.
`readMigrationJournal(migrationsFolder)` — reads and parses `meta/_journal.json` (legacy format only).

`readMigrationEntries(migrationsFolder)` — reads all migration entries with their computed SHA-256 hashes. Automatically detects and handles both legacy and new formats.

`computeMigrationHash(sqlContent)` — computes the SHA-256 hash of a migration SQL string, matching Drizzle's internal algorithm.

Expand Down
6 changes: 6 additions & 0 deletions packages/app/drizzle-utils/docs/migrating-from-prisma.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ npx drizzle-kit generate

This produces SQL migration files that describe your full schema (`CREATE TABLE`, etc.). These describe the *target state*, which your database already has — they must NOT be executed directly.

The output format depends on your drizzle-kit version:
- **drizzle-kit 0.x (stable)**: flat SQL files with a `meta/_journal.json` index
- **drizzle-kit 1.0.0-beta**: folder-per-migration (`<timestamp>_<name>/migration.sql`)

Both formats are fully supported by `markMigrationsApplied`.

Review the generated SQL to verify it matches your existing database. If there are differences, fix your Drizzle schema and regenerate.

## Step 5: Mark migrations as applied (the baseline)
Expand Down
3 changes: 2 additions & 1 deletion packages/app/drizzle-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"@vitest/coverage-v8": "^4.0.15",
"cli-testlab": "^6.0.1",
"cross-env": "^10.0.0",
"drizzle-orm": "1.0.0-beta.19",
"drizzle-kit": "1.0.0-beta.20",
"drizzle-orm": "1.0.0-beta.20",
"mysql2": "^3.14.0",
"postgres": "^3.4.8",
"rimraf": "^6.1.3",
Expand Down
2 changes: 2 additions & 0 deletions packages/app/drizzle-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ export { type BulkUpdateEntry, drizzleFullBulkUpdate } from './drizzleFullBulkUp
export {
computeMigrationHash,
type Dialect,
detectMigrationFormat,
type MarkMigrationsAppliedOptions,
type MarkMigrationsAppliedResult,
type MigrationEntry,
type MigrationFormat,
type MigrationJournal,
type MigrationJournalEntry,
markMigrationsApplied,
Expand Down
Loading
Loading