Loading...
Loading...
Safe database schema migrations using the expand-and-contract pattern with Prisma ORM. Use when renaming columns/tables, changing column types, adding non-nullable columns, or any schema change requiring zero-downtime deployment.
npx skill4agent add kbravh/skills prisma-expand-contractStringEnumEXPAND MIGRATE CONTRACT
Add new structure → Copy data, → Remove old structure
update code
Deploy 1 Deploy 2 Deploy 3model User {
displayName String @map("user_name") // Prisma: displayName, DB: user_name
}model Account {
id String @id
@@map("users") // Prisma: Account, DB: users
}@map@@mapmodel User {
createdAt DateTime @default(now()) // Safe to add directly
isActive Boolean @default(true) // Safe to add directly
}| Scenario | Approach |
|---|---|
| Rename column | Add new → backfill → make required → remove old |
| Change type (String→Enum) | Add enum column → backfill mapping → switch reads → remove string |
| Add non-nullable column | Add nullable → backfill → make required |
| Rename table | Create new table → copy data → migrate code → drop old |
| Split table | Add related table → copy data → update code → remove old fields |
// DON'T: Breaks running instances immediately
model User {
displayName String // Was: userName
}// DON'T: Old code fails during deployment
model User {
// Removed: userName
displayName String // Added
}# DON'T: Can cause data loss
npx prisma migrate dev
# DO: Use migrate deploy
npx prisma migrate deploy// DON'T: Migration fails if nulls exist
model User {
email String // Changed from String?
}npx prisma migrate dev --name name # Development: create + apply
npx prisma migrate deploy # Production: apply pending
npx prisma migrate status # View migration status