toolchains/typescript/data/drizzle-migrations/SKILL.md
Migration-first database development workflow using Drizzle ORM for TypeScript/J...
npx skillsauth add bobmatnyc/claude-mpm-skills drizzle-migrationsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Migration-first database development workflow using Drizzle ORM for TypeScript/JavaScript projects.
Use this skill when:
Critical Rule: Schema changes ALWAYS start with migrations, never code-first.
❌ WRONG: Writing TypeScript schema first
// DON'T DO THIS FIRST
export const users = pgTable('users', {
id: uuid('id').primaryKey(),
email: text('email').notNull(),
});
✅ CORRECT: Write SQL migration first
-- drizzle/0001_add_users_table.sql
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT NOW()
);
Create descriptive SQL migration file:
-- drizzle/0001_create_school_calendars.sql
CREATE TABLE school_calendars (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
school_id UUID NOT NULL REFERENCES schools(id) ON DELETE CASCADE,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
academic_year TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW()
);
-- Add indexes for query performance
CREATE INDEX idx_school_calendars_school_id ON school_calendars(school_id);
CREATE INDEX idx_school_calendars_academic_year ON school_calendars(academic_year);
-- Add constraints
ALTER TABLE school_calendars
ADD CONSTRAINT check_date_range
CHECK (end_date > start_date);
Naming Convention:
0001_, 0002_, etc.create_school_calendars, add_user_rolesXXXX_descriptive_name.sqlDrizzle Kit generates TypeScript types from SQL:
# Generate TypeScript schema and snapshots
pnpm drizzle-kit generate
# Or using npm
npm run db:generate
What This Creates:
drizzle-kit push)drizzle/meta/XXXX_snapshot.jsonSnapshots enable schema drift detection:
// drizzle/meta/0001_snapshot.json (auto-generated)
{
"version": "5",
"dialect": "postgresql",
"tables": {
"school_calendars": {
"name": "school_calendars",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"school_id": {
"name": "school_id",
"type": "uuid",
"notNull": true
}
}
}
}
}
Snapshots in Version Control:
Now write TypeScript schema that mirrors SQL migration:
// src/lib/db/schema/school/calendar.ts
import { pgTable, uuid, date, text, timestamp } from 'drizzle-orm/pg-core';
import { schools } from './school';
export const schoolCalendars = pgTable('school_calendars', {
id: uuid('id').primaryKey().defaultRandom(),
schoolId: uuid('school_id')
.notNull()
.references(() => schools.id, { onDelete: 'cascade' }),
startDate: date('start_date').notNull(),
endDate: date('end_date').notNull(),
academicYear: text('academic_year').notNull(),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});
// Type inference
export type SchoolCalendar = typeof schoolCalendars.$inferSelect;
export type NewSchoolCalendar = typeof schoolCalendars.$inferInsert;
Key Points:
school_id → 'school_id'schoolIdStructure schemas for maintainability:
src/lib/db/schema/
├── index.ts # Export all schemas
├── school/
│ ├── index.ts
│ ├── district.ts
│ ├── holiday.ts
│ ├── school.ts
│ └── calendar.ts
├── providers.ts
├── cart.ts
└── users.ts
index.ts (export all):
// src/lib/db/schema/index.ts
export * from './school';
export * from './providers';
export * from './cart';
export * from './users';
school/index.ts:
// src/lib/db/schema/school/index.ts
export * from './district';
export * from './holiday';
export * from './school';
export * from './calendar';
Validate schema consistency in CI/CD:
# .github/workflows/quality.yml
name: Quality Checks
on:
pull_request:
branches: [main, develop]
push:
branches: [main]
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'pnpm'
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Check database schema drift
run: pnpm drizzle-kit check
- name: Verify migrations (dry-run)
run: pnpm drizzle-kit push --dry-run
env:
DATABASE_URL: ${{ secrets.STAGING_DATABASE_URL }}
- name: Run type checking
run: pnpm tsc --noEmit
- name: Lint code
run: pnpm lint
CI Checks Explained:
drizzle-kit check: Validates snapshots match schemadrizzle-kit push --dry-run: Tests migration without applyingBefore production, test migration on staging:
# 1. Run migration on staging
STAGING_DATABASE_URL="..." pnpm drizzle-kit push
# 2. Verify schema
pnpm drizzle-kit check
# 3. Test affected API routes
curl https://staging.example.com/api/schools/calendars
# 4. Check for data integrity issues
# Run queries to verify data looks correct
# 5. Monitor logs for errors
# Check application logs for migration-related errors
Staging Checklist:
-- drizzle/0005_add_user_phone.sql
ALTER TABLE users
ADD COLUMN phone TEXT;
-- Add index if querying by phone
CREATE INDEX idx_users_phone ON users(phone);
TypeScript:
export const users = pgTable('users', {
id: uuid('id').primaryKey(),
email: text('email').notNull(),
phone: text('phone'), // New column
});
-- drizzle/0006_create_provider_specialties.sql
CREATE TABLE provider_specialties (
provider_id UUID NOT NULL REFERENCES providers(id) ON DELETE CASCADE,
specialty_id UUID NOT NULL REFERENCES specialties(id) ON DELETE CASCADE,
PRIMARY KEY (provider_id, specialty_id)
);
CREATE INDEX idx_provider_specialties_provider ON provider_specialties(provider_id);
CREATE INDEX idx_provider_specialties_specialty ON provider_specialties(specialty_id);
TypeScript:
export const providerSpecialties = pgTable('provider_specialties', {
providerId: uuid('provider_id')
.notNull()
.references(() => providers.id, { onDelete: 'cascade' }),
specialtyId: uuid('specialty_id')
.notNull()
.references(() => specialties.id, { onDelete: 'cascade' }),
}, (table) => ({
pk: primaryKey(table.providerId, table.specialtyId),
}));
-- drizzle/0007_change_price_to_decimal.sql
ALTER TABLE services
ALTER COLUMN price TYPE DECIMAL(10, 2);
TypeScript:
import { decimal } from 'drizzle-orm/pg-core';
export const services = pgTable('services', {
id: uuid('id').primaryKey(),
name: text('name').notNull(),
price: decimal('price', { precision: 10, scale: 2 }).notNull(),
});
-- drizzle/0008_add_email_constraint.sql
ALTER TABLE users
ADD CONSTRAINT users_email_unique UNIQUE (email);
ALTER TABLE users
ADD CONSTRAINT users_email_format CHECK (email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$');
import type { Config } from 'drizzle-kit';
export default {
schema: './src/lib/db/schema/index.ts',
out: './drizzle',
driver: 'pg',
dbCredentials: {
connectionString: process.env.DATABASE_URL!,
},
} satisfies Config;
{
"scripts": {
"db:generate": "drizzle-kit generate:pg",
"db:push": "drizzle-kit push:pg",
"db:studio": "drizzle-kit studio",
"db:check": "drizzle-kit check:pg",
"db:up": "drizzle-kit up:pg"
}
}
# 1. Create migration
echo "CREATE TABLE test (...)" > drizzle/0009_test.sql
# 2. Generate TypeScript
pnpm db:generate
# 3. Push to local database
pnpm db:push
# 4. Verify schema
pnpm db:check
# 5. Test in application
pnpm dev
# Manually test affected features
# 6. Run tests
pnpm test
-- drizzle/0010_add_feature.sql (up migration)
CREATE TABLE new_feature (...);
-- drizzle/0010_add_feature_down.sql (down migration)
DROP TABLE new_feature;
Apply rollback:
# Manually run down migration
psql $DATABASE_URL -f drizzle/0010_add_feature_down.sql
drizzle-kit check in CIdrizzle-kit push in production (use proper migrations)Error: Schema drift detected
Solution:
# Check what changed
pnpm drizzle-kit check
# Regenerate snapshots
pnpm drizzle-kit generate
# Review changes and commit
git add drizzle/meta/
git commit -m "Update schema snapshots"
Error: Migration fails with data constraint violation
Solution:
-- First: Migrate data
UPDATE users SET status = 'active' WHERE status IS NULL;
-- Then: Add constraint
ALTER TABLE users
ALTER COLUMN status SET NOT NULL;
Error: TypeScript types don't match database
Solution:
# Regenerate everything
pnpm db:generate
pnpm tsc --noEmit
# If still broken, check schema files
# Ensure column names match SQL exactly
universal-data-database-migration - Universal migration patternstoolchains-typescript-data-drizzle - Drizzle ORM usage patternstoolchains-typescript-core - TypeScript best practicesuniversal-debugging-verification-before-completion - Verification workflowsdevelopment
Optimize web performance using Core Web Vitals, modern patterns (View Transitions, Speculation Rules), and framework-specific techniques
development
Best practices for documenting APIs and code interfaces, eliminating redundant documentation guidance per agent.
development
Comprehensive API design patterns covering REST, GraphQL, gRPC, versioning, authentication, and modern API best practices
development
Visual verification workflow for UI changes to accelerate code review and catch ...