Skip to content
Back to skills

Data Migrations

ASecurity

Postgres DB migrations: DDL/DML, zero-downtime expand-contract, rollbacks, locks, CONCURRENTLY, batching, multi-tenant.

  • 3 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added November 14, 2026
databasesgobashsqlnodedatabase

Security analysis

A100/100

Pro scans all 5 files and shows the line behind each finding

Scanned November 14, 2026

npx -y skills add SebastianLuser/claude-code-arcane --skill data-migrations --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Data Migrations?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Data Migrations
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/sebastianluser-data-migrations/badge)](https://www.skillsdirectory.com/skills/sebastianluser-data-migrations)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: data-migrations
description: "Postgres DB migrations: DDL/DML, zero-downtime expand-contract, rollbacks, locks, CONCURRENTLY, batching, multi-tenant."
category: "database"
argument-hint: "[create <name>|run|rollback]"
user-invocable: true
allowed-tools: Read, Glob, Grep, Bash, Write, Edit, Task
---
# Data Migrations — Educabot

## Route

| User intent | Mode | What it does |
|----------------------|------|----------|
| "new migration", "add column", "create table", "rename" | CREATE | Generates `.up.sql` + `.down.sql` files with correct naming |
| "run migrations", "apply", "migrate up", "deploy migration" | RUN | Validates staging → runs → confirms state |
| "revert", "rollback", "undo migration", "migrate down" | ROLLBACK | Verifies `.down.sql`, assesses risk, runs with a prior backup |
| "which migrations exist", "status", "pending", "applied" | STATUS | `migrate version` + list of pending vs applied |

**Rule:** if the intent combines CREATE + RUN, do them in that order. If it is a destructive ROLLBACK, ask for explicit confirmation.

---

DB migrations without breaking production. Stack: **Postgres** with **golang-migrate** (Go) or **Prisma Migrate** (TS).

## When to use / NOT use

| Use | Do NOT use |
|------|---------|
| Schema: CREATE/ALTER TABLE, ADD COLUMN, indexes | Test data seeds (use fixtures) |
| Data: backfills, transformations, normalizations | Trivial local-dev lookup tables |
| Rename/drop columns or tables in prod | One-shot manual cleanup scripts (use a job) |
| ALTER TYPE, split/merge tables, multi-tenant | App config changes (env/config) |

## Tools

| Stack | Tool | Files |
|-------|------|----------|
| **Go** (default) | `golang-migrate/migrate` | `migrations/NNNNNNNNNNNNNN_name.up.sql` + `.down.sql` |
| **TS (Prisma)** | Prisma Migrate | If it uses Prisma as the ORM |
| **TS (plain pg)** | `node-pg-migrate` | If it uses pg without an ORM |
| **TS (Drizzle)** | `drizzle-kit` | If the stack is Drizzle |

Align the tool with the chosen ORM. Do not mix two systems in the same repo.

## Naming

Format: `YYYYMMDDHHMMSS_description.up.sql` + `.down.sql`. UTC timestamp (not sequential). Description in snake_case with a verb (`add_`, `create_`, `drop_`, `alter_`, `backfill_`). One migration = one atomic logical change. Never edit a migration already merged to main.

## Zero-downtime: expand-contract

Every breaking change is split across multiple releases (expand → backfill → dual-write → migrate reads → contract).

> → Read references/expand-contract.md for detailed phases and multi-step breaking changes

## Indexes

- **Always** `CREATE INDEX CONCURRENTLY` on tables with data (does not block writes)
- It cannot run inside a transaction — use `-- +migrate NoTransaction` (golang-migrate) or `--create-only` + edit the SQL (Prisma)
- If it fails: it is left INVALID → DROP INDEX → retry

## Locks and timeouts

`ALTER TABLE` takes an ACCESS EXCLUSIVE LOCK. Safe pattern: `SET lock_timeout = '5s'; SET statement_timeout = '30s';` before the ALTER. If it cannot get the lock → fail fast → retry with backoff. `ADD COLUMN` with a non-volatile DEFAULT is instant since PG 11. `ADD COLUMN NOT NULL DEFAULT <volatile>` rewrites the table — avoid it.

## Batching (large data migrations)

Keyset pagination (`id > last_id`), 10k batches, checkpoints, idempotent, separate job.

> → Read references/batching.md for the full batching rules

## Rollback

- Additive DDL (ADD/CREATE) → reversible with DROP
- Destructive DDL (DROP) → one-way, data lost
- Lossy data migration → one-way
- **Always** write `.down.sql` (even if it is a documented NO-OP)
- Critical one-way migrations: a prior backup is mandatory

## Staging and CI

- Staging = anonymized subset of prod. Run every migration there before prod. Measure the time.
- CI (mandatory on PRs): `up` from clean → `down` → `up` again (validates reversibility + idempotency). SQL lint if configured.

## Multi-tenant

| Pattern | How | When |
|--------|------|--------|
| **Shared schema** (Educabot default) | One global migration, backfills cover every tenant_id | Standard SaaS |
| **Schema-per-tenant** | The runner iterates tenants, tracks partial failure | Medium isolation |
| **Database-per-tenant** | Maximum isolation | Enterprise compliance only |

## Anti-patterns

> → Read references/anti-patterns.md for the full list (12 items)

## Delegation

- `/db-diagram` — post-migration ER diagram
- `/deploy-check` — deploy checklist with a migration
- `/audit-dev` — full review of accumulated migrations
- `/scaffold-go` — golang-migrate setup in a new project

## Checklist

> → Read references/checklist.md for the full checklist (12 items)

- [ ] Expand-contract pattern used for all breaking schema changes
- [ ] Both `.up.sql` and `.down.sql` files exist for every migration
- [ ] `lock_timeout` and `statement_timeout` configured before ALTER statements
- [ ] Migration tested and timed in staging with production-like data
- [ ] Rollback verified by running `.down.sql` then `.up.sql` again
- [ ] Indexes created with `CONCURRENTLY` outside transactions on populated tables

Files in this skill

  • SKILL.md5 KB
  • references/anti-patterns.md600 B
  • references/batching.md357 B
  • references/checklist.md612 B
  • references/expand-contract.md1010 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in databases →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.