db-migrate
Create database migration scripts following project patterns. Use when user mentions "migration", "add column", "alter table", "schema change", or "database update".
When & Why to Use This Skill
This Claude skill automates the generation of database migration scripts specifically for Neon PostgreSQL and Prisma environments. It streamlines the database evolution process by creating standardized migration files, updating TypeScript interfaces, and modifying SQL schema definitions, all while enforcing safety guardrails like idempotent operations.
Use Cases
- Schema Evolution: Automatically generate PostgreSQL migration scripts for adding columns, altering tables, or creating indexes based on natural language descriptions.
- Full-Stack Synchronization: Simultaneously update database schemas, TypeScript interfaces, and migration history to keep the entire codebase in sync with database changes.
- Safe Deployment Preparation: Create idempotent migration scripts using 'IF NOT EXISTS' patterns to ensure safe execution across different environments (dev, staging, production).
- Standardized Scripting: Maintain project consistency by generating migration files that follow specific naming conventions and architectural patterns automatically.
| name | db-migrate |
|---|---|
| description | Create database migration scripts following project patterns. Use when user mentions "migration", "add column", "alter table", "schema change", or "database update". |
Database Migration
Create migration scripts for Neon PostgreSQL following project conventions.
Instructions
Read current schema:
prisma/schema.sqlfor table definitionssrc/lib/db.tsfor TypeScript interfaces
Create migration script in
scripts/directory:- Filename:
<action>-<description>.mjs(e.g.,add-views-column.mjs) - Use existing pattern from
scripts/add-summary-column.mjs
- Filename:
Migration script template:
import { neon } from '@neondatabase/serverless'; import { config } from 'dotenv'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); config({ path: join(__dirname, '..', '.env.local') }); const sql = neon(process.env.DATABASE_URL); async function migrate() { console.log('Running migration...'); await sql` ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name TYPE `; console.log('Migration complete!'); } migrate().catch(console.error);Update
src/lib/db.ts:- Add new fields to relevant interface
- Update query functions if needed
Update
prisma/schema.sql:- Add new column to CREATE TABLE statement
Examples
- "Add a views column to articles"
- "Create migration for user preferences table"
- "Add index on published_at"
Guardrails
- ALWAYS use
IF NOT EXISTSorIF EXISTSfor safety - NEVER use DROP TABLE without explicit confirmation
- Test migration on development database first
- Back up data before destructive operations:
curl localhost:3000/api/admin/articles/export