migrations
GitHub用于处理 Drizzle 数据库迁移的生成、审查、编辑及冲突解决。涵盖 schema 变更后的迁移文件生成、SQL 适配修改,以及合并冲突时的重置与重新应用流程。
Trigger Scenarios
Install
npx skills add theopenco/llmgateway --skill migrations -g -y
SKILL.md
Frontmatter
{
"name": "migrations",
"description": "Generate, review, edit, apply, or resolve conflicts for Drizzle database migrations in this repo. Use when changing packages\/db\/src\/schema.ts, running pnpm migrations or pnpm migrate, touching packages\/db\/migrations, reviewing migration diffs, or handling migration merge conflicts."
}
Migrations
Use this workflow for database schema changes and migration conflicts.
Generate migrations
- Run all commands from the repository root.
- Make schema changes in
packages/db/src/schema.ts. - Generate Drizzle migration artifacts with
pnpm migrations. - Review the generated diff under
packages/db/migrations/. - Drizzle may generate:
packages/db/migrations/<timestamp>_<name>.sqlpackages/db/migrations/meta/<timestamp>_snapshot.jsonpackages/db/migrations/meta/_journal.json
Editing generated migrations
- Do not write a migration by hand from scratch. Generate it first with
pnpm migrations. - If the generated migration needs adaptation, edit only the generated
.sqlfile. - Never manually edit any
*_snapshot.jsonfile. - Never manually edit
packages/db/migrations/meta/_journal.json. - If the TypeScript schema is wrong, fix
packages/db/src/schema.tsand regenerate instead of patching snapshot or journal metadata. - Use snake_case column names in SQL because Drizzle maps camelCase TypeScript fields to snake_case database columns.
Appropriate .sql-only edits include adding safe data backfills, adjusting a generated type cast, adding a USING clause, splitting statements for safer execution, or preserving data during a rename. Keep the generated snapshot and journal exactly as Drizzle wrote them.
Conflict resolution
Never resolve merge conflicts in migration SQL, snapshot JSON, or journal files manually.
When merging with main and migration conflicts appear:
- The reset in step 1 rewrites
packages/db/migrations/fromorigin/main, and it acts on tracked files only. Two consequences:
- An untracked
.sql(one you just generated but have not committed) survives the reset and then collides with what step 2 regenerates. It is also invisible togit diff, so the capture below would miss it. - Any hand-adaptation of a generated
.sql— aUSINGclause, a data backfill, a rename that preserves data — is reverted, andpnpm migrationsemits vanilla SQL from the schema diff, so it will not come back on its own.
Commit (or delete) everything under the directory first, so nothing is untracked and the capture sees all of it:
git status --porcelain packages/db/migrations/ # must print nothing before continuing
git diff origin/main -- packages/db/migrations/ > /tmp/migration-adaptations.patch
Keep that patch as your reference and re-apply the adaptations by hand in step 3. Do not use git stash for this — lint-staged inserts its own backup stashes at position 0 in this repo, so a bare git stash pop can restore the wrong entry.
- Reset migrations to
origin/main:
git restore --source=origin/main packages/db/migrations/
- Re-run generation from the repository root:
pnpm migrations
- Review the regenerated SQL, then re-apply any adaptations you captured in step 0. Adapt only the generated
.sqlfile.
Validation
- Inspect
git diff packages/db/src/schema.ts packages/db/migrations/. - Confirm any snapshot JSON and journal changes came from
pnpm migrations, not manual edits. - Run
pnpm formatafter changes. - Run
pnpm buildafter schema or migration changes.
Version History
- 8fdff5b Current 2026-08-16 07:59
- 8c03315 2026-07-24 16:53


