Database Migration & Versioning Expert
English | Bahasa Indonesia
English
Description
A specialized skill focused strictly on the lifecycle of database schemas in production environments. While database-orm-expert handles queries and types, this skill covers the DevOps aspect of databases: zero-downtime schema migrations, backward-compatible release cycles, stateful data backfills, rollback mechanisms, and schema versioning strategies.
Trigger Conditions
- When modifying an existing production database schema.
- When planning a deployment that involves database changes (blue-green, canary).
- When resolving merge conflicts in migration files (e.g., Prisma, Drizzle, Alembic).
- When designing backfill scripts for massive data migrations.
Core Architectural Guidelines
1. Zero-Downtime Migration Pattern (Expand and Contract)
Never make breaking changes in a single deployment. Use the "Expand and Contract" pattern (Parallel Change):
- Phase 1 (Expand): Add the new schema element (column, table) without removing the old one. Deploy the database change.
- Phase 2 (Migrate): Update application code to write to both old and new elements, and read from the new element (with fallback). Deploy code.
- Phase 3 (Backfill): Run a background script to backfill data from the old element to the new element for older records.
- Phase 4 (Contract): Remove the old application code that writes to the old element. Deploy code.
- Phase 5 (Cleanup): Drop the old schema element from the database.
2. Backward Compatibility Rules
- Never
DROP or RENAME a column/table in active use. Create a new one, migrate data, then drop the old one later.
- Avoid changing constraints on existing data without carefully verifying that all data complies.
- Add
DEFAULT values to new NOT NULL columns, or make them nullable first, backfill, then enforce NOT NULL.
3. Migration Mechanics
- Always use version-controlled, immutable migration scripts (e.g.,
20260814_add_user_status.sql).
- Never modify an already-applied migration file. If a mistake was made, create a new forward-migration to fix it.
- Idempotency: Write scripts that can be safely run multiple times (e.g.,
CREATE TABLE IF NOT EXISTS, ADD COLUMN IF NOT EXISTS).
4. Safe Data Backfilling
For large tables (millions of rows), running UPDATE table SET new_col = old_col will lock the table and cause downtime.
- Chunking: Perform updates in batches using
LIMIT and sleep intervals to avoid locking the database.
- Background Jobs: Use dedicated queues (like BullMQ or Inngest) to orchestrate massive backfills.
Orchestration & Integration
- Enhances
database-orm-expert with production-grade migration strategies.
- Complements
ci-cd-devops-architect for automated migration deployment steps.
- Integrates with
supabase-migration for Supabase-specific PostgreSQL migration workflows.
Bahasa Indonesia
Deskripsi
Skill khusus yang berfokus ketat pada siklus hidup skema database di lingkungan produksi. Sementara database-orm-expert menangani query dan tipe, skill ini mencakup aspek DevOps database: migrasi skema tanpa downtime (zero-downtime), rilis backward-compatible, skrip backfill data stateful, mekanisme rollback, dan strategi pembuatan versi skema.
Kondisi Pemicu
- Saat memodifikasi skema database produksi yang sudah ada.
- Saat merencanakan deployment yang melibatkan perubahan database (blue-green, canary).
- Saat merancang skrip backfill untuk tabel berukuran besar (jutaan baris).
Panduan Arsitektur Inti
1. Pola Zero-Downtime (Expand and Contract)
Jangan pernah melakukan perubahan yang merusak (breaking change) dalam satu deployment.
- Tambahkan kolom baru tanpa menghapus yang lama.
- Ubah aplikasi untuk menulis ke keduanya dan membaca dari yang baru.
- Lakukan migrasi data lama (backfill).
- Hapus penggunaan kolom lama dari kode aplikasi.
- Hapus kolom lama dari database di migrasi berikutnya.
2. Aturan Kompatibilitas Mundur (Backward Compatibility)
- Jangan pernah melakukan
DROP atau RENAME pada kolom yang sedang aktif digunakan. Buat yang baru, pindahkan data, baru hapus yang lama di siklus rilis berikutnya.
- Kolom
NOT NULL baru harus selalu memiliki nilai DEFAULT, atau jadikan nullable terlebih dahulu sebelum memaksakan constraint.
3. Keamanan Skrip Migrasi
- Gunakan file migrasi yang immutable (tidak boleh diubah setelah di-deploy). Jika ada bug, buat file migrasi baru untuk memperbaikinya (forward-fix).
- Skrip harus sebisa mungkin bersifat idempoten.
- Untuk tabel besar, lakukan backfill data secara bertahap (chunking/batching) agar tidak terjadi table lock yang menyebabkan downtime aplikasi.
Integrasi Orkestrasi
- Memperkuat
database-orm-expert dengan strategi deployment yang aman.
- Melengkapi
ci-cd-devops-architect dalam alur CI/CD untuk otomatisasi migrasi.
- Terintegrasi dengan
supabase-migration untuk ekosistem spesifik Supabase.
1---2name: database-migration-versioning-expert3description: Expert guide for database migrations: schema versioning, zero-downtime migrations, backward-compatible changes, data backfill, and rollback strategies / Panduan ahli migrasi database.4---56# Database Migration & Versioning Expert78[English](#english) | [Bahasa Indonesia](#bahasa-indonesia)910---1112<a name="english"></a>13## English1415### Description16A specialized skill focused strictly on the lifecycle of database schemas in production environments. While `database-orm-expert` handles queries and types, this skill covers the DevOps aspect of databases: zero-downtime schema migrations, backward-compatible release cycles, stateful data backfills, rollback mechanisms, and schema versioning strategies.1718### Trigger Conditions19- When modifying an existing production database schema.20- When planning a deployment that involves database changes (blue-green, canary).21- When resolving merge conflicts in migration files (e.g., Prisma, Drizzle, Alembic).22- When designing backfill scripts for massive data migrations.2324### Core Architectural Guidelines2526#### 1. Zero-Downtime Migration Pattern (Expand and Contract)27Never make breaking changes in a single deployment. Use the "Expand and Contract" pattern (Parallel Change):28- **Phase 1 (Expand)**: Add the new schema element (column, table) without removing the old one. Deploy the database change.29- **Phase 2 (Migrate)**: Update application code to write to *both* old and new elements, and read from the new element (with fallback). Deploy code.30- **Phase 3 (Backfill)**: Run a background script to backfill data from the old element to the new element for older records.31- **Phase 4 (Contract)**: Remove the old application code that writes to the old element. Deploy code.32- **Phase 5 (Cleanup)**: Drop the old schema element from the database.3334#### 2. Backward Compatibility Rules35- **Never `DROP` or `RENAME`** a column/table in active use. Create a new one, migrate data, then drop the old one later.36- **Avoid changing constraints** on existing data without carefully verifying that all data complies.37- **Add `DEFAULT` values** to new `NOT NULL` columns, or make them nullable first, backfill, then enforce `NOT NULL`.3839#### 3. Migration Mechanics40- Always use version-controlled, immutable migration scripts (e.g., `20260814_add_user_status.sql`).41- Never modify an already-applied migration file. If a mistake was made, create a new forward-migration to fix it.42- **Idempotency**: Write scripts that can be safely run multiple times (e.g., `CREATE TABLE IF NOT EXISTS`, `ADD COLUMN IF NOT EXISTS`).4344#### 4. Safe Data Backfilling45For large tables (millions of rows), running `UPDATE table SET new_col = old_col` will lock the table and cause downtime.46- **Chunking**: Perform updates in batches using `LIMIT` and sleep intervals to avoid locking the database.47- **Background Jobs**: Use dedicated queues (like BullMQ or Inngest) to orchestrate massive backfills.4849## Orchestration & Integration50- Enhances `database-orm-expert` with production-grade migration strategies.51- Complements `ci-cd-devops-architect` for automated migration deployment steps.52- Integrates with `supabase-migration` for Supabase-specific PostgreSQL migration workflows.5354---5556<a name="bahasa-indonesia"></a>57## Bahasa Indonesia5859### Deskripsi60Skill khusus yang berfokus ketat pada siklus hidup skema database di lingkungan produksi. Sementara `database-orm-expert` menangani query dan tipe, skill ini mencakup aspek DevOps database: migrasi skema tanpa downtime (zero-downtime), rilis backward-compatible, skrip backfill data stateful, mekanisme rollback, dan strategi pembuatan versi skema.6162### Kondisi Pemicu63- Saat memodifikasi skema database produksi yang sudah ada.64- Saat merencanakan deployment yang melibatkan perubahan database (blue-green, canary).65- Saat merancang skrip backfill untuk tabel berukuran besar (jutaan baris).6667### Panduan Arsitektur Inti6869#### 1. Pola Zero-Downtime (Expand and Contract)70Jangan pernah melakukan perubahan yang merusak (breaking change) dalam satu deployment.71- Tambahkan kolom baru tanpa menghapus yang lama.72- Ubah aplikasi untuk menulis ke keduanya dan membaca dari yang baru.73- Lakukan migrasi data lama (backfill).74- Hapus penggunaan kolom lama dari kode aplikasi.75- Hapus kolom lama dari database di migrasi berikutnya.7677#### 2. Aturan Kompatibilitas Mundur (Backward Compatibility)78- **Jangan pernah melakukan `DROP` atau `RENAME`** pada kolom yang sedang aktif digunakan. Buat yang baru, pindahkan data, baru hapus yang lama di siklus rilis berikutnya.79- Kolom `NOT NULL` baru harus selalu memiliki nilai `DEFAULT`, atau jadikan nullable terlebih dahulu sebelum memaksakan constraint.8081#### 3. Keamanan Skrip Migrasi82- Gunakan file migrasi yang immutable (tidak boleh diubah setelah di-deploy). Jika ada bug, buat file migrasi baru untuk memperbaikinya (forward-fix).83- Skrip harus sebisa mungkin bersifat idempoten.84- Untuk tabel besar, lakukan backfill data secara bertahap (chunking/batching) agar tidak terjadi table lock yang menyebabkan downtime aplikasi.8586## Integrasi Orkestrasi87- Memperkuat `database-orm-expert` dengan strategi deployment yang aman.88- Melengkapi `ci-cd-devops-architect` dalam alur CI/CD untuk otomatisasi migrasi.89- Terintegrasi dengan `supabase-migration` untuk ekosistem spesifik Supabase.