Create reviewable up/down database migrations with your ORM's own tool or the bundled diff.ts (PostgreSQL/SQLite), with data-loss and blocking-change warnings.
---
name: db-migrate
description: Creates a reviewable database migration (up and down) for a schema change, using the project's own ORM migration tool when it has one (Prisma, Drizzle, TypeORM, Sequelize, Django, Alembic) and the bundled diff.ts for raw SQL on PostgreSQL or SQLite. Use when the user changes a model or schema and asks for a migration, ALTER TABLE statements or a rollback plan.
compatibility: The bundled diff.ts needs Node.js 22.18+ or 23.6+ (or npx tsx on older Node). ORM paths need that ORM's CLI installed in the project.
metadata:
version: "1.1.0"
---
# Database Migration Generator
Produces a migration the user can review before anything touches a database.
This skill never applies migrations to shared, staging or production databases.
## Files in this skill
- `SKILL.md` (this file)
- `diff.ts` — compares two JSON schema snapshots and prints SQL with `-- UP`, `-- DOWN` and
`-- WARNINGS` sections. Supports `postgres` and `sqlite`. Exit codes: `0` ok, `2` ok but a
BLOCKING warning was raised, `1` invalid input, `64` usage error. It does not connect to a
database and does not detect renames (a rename shows up as drop + add, with a DATA LOSS warning).
## Workflow
1. **Identify the migration tool already in the repo** and use it rather than hand-written SQL:
| Project uses | Generate (review before applying) |
|---|---|
| Prisma | `npx prisma migrate dev --create-only --name <name>` (writes SQL without applying), or `npx prisma migrate diff` |
| Drizzle | `npx drizzle-kit generate` |
| TypeORM | `npx typeorm migration:generate -d <path/to/data-source> <path/MigrationName>` (TypeScript data sources need a TS loader, for example `npx typeorm-ts-node-commonjs ...`) |
| Sequelize | `npx sequelize-cli migration:generate --name <name>` creates an empty skeleton; write `up`/`down` yourself |
| Django | `python manage.py makemigrations <app>`, then `python manage.py sqlmigrate <app> <number>` to read the SQL |
| Alembic | `alembic revision --autogenerate -m "<message>"` (autogenerate does not detect every change; read the file) |
| Raw SQL / none | Use `diff.ts` (step 3) |
2. **Collect inputs:** the before and after schema (model files, `schema.prisma`, DDL, or a
description), the database engine and version, approximate table sizes if known, and
whether any column or table is being **renamed** (generators cannot tell rename from
drop + add).
3. **Raw SQL path:** write `before.json` and `after.json` in the snapshot shape documented at
the top of `diff.ts`, then run:
```bash
node <skill-dir>/diff.ts before.json after.json --name add_user_profile > migration.sql
```
4. **Review the generated migration** (ORM or diff.ts) for these risks and fix them before
handing it over:
- Renames generated as drop + add → replace with `RENAME COLUMN` / `RENAME TO`.
- `NOT NULL` column added without a default to a non-empty table → add a default, or split
into add nullable → backfill → set NOT NULL.
- Type changes that can truncate or fail (PostgreSQL may need `USING`).
- Dropped columns/tables → confirm with the user; the DOWN step cannot restore data.
- Index builds on large PostgreSQL tables → consider `CREATE INDEX CONCURRENTLY` (cannot run
inside a transaction; many ORMs wrap migrations in one).
- SQLite: `DROP COLUMN` fails for primary-key, UNIQUE or indexed columns (drop the index
first); `ALTER COLUMN ... SET/DROP NOT NULL` needs SQLite 3.53.0+; other changes need a
table rebuild.
5. **Test locally only:** apply UP then DOWN against a disposable local or containerised
database seeded with representative rows, and run the app's tests. Never run against a
shared database unless the user explicitly asks and names it.
6. Present the migration file, the warnings, and a short rollout note (backup, order of
deploy vs migration for backward-compatible changes).
## Output format
```sql
-- Migration: <name>
-- WARNINGS
-- <each risk, one line>
-- UP
<statements>
-- DOWN
<statements in reverse order>
```
## Limitations
- `diff.ts` handles tables, columns (type, nullability, default, primary key on create) and
indexes. Foreign keys, CHECK constraints, enums, views and triggers must be written by hand.
- DOWN migrations restore structure, not deleted data. Take a backup before destructive changes.
- MySQL is not supported by `diff.ts`; use the ORM tool or write the SQL by hand.
## Example
Request: "Add nullable `bio` (text) and `avatar_url` (varchar 255) to `users`, add a unique
index on `email`, and drop the unused `legacy_flag` column. SQLite."
Expected migration excerpt (from `diff.ts`):
```sql
-- WARNINGS
-- DATA LOSS: column users.legacy_flag is dropped; DOWN recreates it empty. If it was renamed, use RENAME COLUMN instead.
-- UP
DROP INDEX "idx_users_legacy";
ALTER TABLE "users" ADD COLUMN "bio" TEXT;
ALTER TABLE "users" ADD COLUMN "avatar_url" VARCHAR(255);
ALTER TABLE "users" DROP COLUMN "legacy_flag";
CREATE UNIQUE INDEX "idx_users_email" ON "users" ("email");
-- DOWN
DROP INDEX "idx_users_email";
ALTER TABLE "users" ADD COLUMN "legacy_flag" INTEGER;
ALTER TABLE "users" DROP COLUMN "avatar_url";
ALTER TABLE "users" DROP COLUMN "bio";
CREATE INDEX "idx_users_legacy" ON "users" ("legacy_flag");
```
Before handing it over, confirm with the user that `legacy_flag` data can be discarded.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A SQLite-backed app needs two new profile columns and a unique email index, and an unused column should be removed. The project has no ORM migration generator.
Add nullable bio (TEXT) and avatar_url (VARCHAR(255)) to users, add a unique index on email, and drop the unused legacy_flag column (it currently has an index idx_users_legacy). Database: SQLite. Give me the migration with a rollback.
Snapshot files before.json/after.json and the output of `node diff.ts before.json after.json --name add_user_profile`:
-- WARNINGS
-- DATA LOSS: column users.legacy_flag is dropped; DOWN recreates it empty. ...
-- UP
DROP INDEX "idx_users_legacy";
ALTER TABLE "users" ADD COLUMN "bio" TEXT;
ALTER TABLE "users" ADD COLUMN "avatar_url" VARCHAR(255);
ALTER TABLE "users" DROP COLUMN "legacy_flag";
CREATE UNIQUE INDEX "idx_users_email" ON "users" ("email");
-- DOWN (reverse order, ending with CREATE INDEX "idx_users_legacy")
plus a request to confirm that legacy_flag data can be discarded, and a note to test UP then DOWN on a disposable local copy first.AI Agent Skills are reusable workflow packages that extend your AI coding assistant with multi-step capabilities. Install db-migrate by following these steps.
SKILL.md inside, in the location documented by your client. Do not assume every AI application reads the same directory.Requires an Agent Skills-compatible client; supported locations and activation vary by version. Read the SKILL.md setup guide. MCP connects tools and data and does not, by itself, install a skill.