managing-database-migrations

Process use when you need to work with database migrations. This skill provides schema migration management with comprehensive guidance and automation. Trigger with phrases like "create migration", "run migrations", or "manage schema versions".

1,868 stars

Best use case

managing-database-migrations is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Process use when you need to work with database migrations. This skill provides schema migration management with comprehensive guidance and automation. Trigger with phrases like "create migration", "run migrations", or "manage schema versions".

Teams using managing-database-migrations should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/managing-database-migrations/SKILL.md --create-dirs "https://raw.githubusercontent.com/jeremylongshore/claude-code-plugins-plus-skills/main/plugins/database/database-migration-manager/skills/managing-database-migrations/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/managing-database-migrations/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How managing-database-migrations Compares

Feature / Agentmanaging-database-migrationsStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Process use when you need to work with database migrations. This skill provides schema migration management with comprehensive guidance and automation. Trigger with phrases like "create migration", "run migrations", or "manage schema versions".

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

# Database Migration Manager

## Overview

Create, validate, and execute database schema migrations with full rollback support across PostgreSQL, MySQL, and MongoDB.

## Prerequisites

- Database credentials with DDL permissions (CREATE, ALTER, DROP TABLE)
- Migration framework installed and configured (Flyway, Alembic, Prisma, Knex, or raw SQL versioning)
- Version control for migration files (Git repository)
- Access to a staging database matching production schema for testing migrations
- `psql` or `mysql` CLI for executing and verifying migrations
- Current schema baseline documented or captured via `pg_dump --schema-only`

## Instructions

1. Capture the current schema state before making changes. Run `pg_dump --schema-only -f schema_before.sql` (PostgreSQL) or `mysqldump --no-data > schema_before.sql` (MySQL) to create a reference point.

2. Define the desired schema change clearly: specify table name, column additions/removals/modifications, constraint changes, and index updates. Document whether the change is additive (safe) or destructive (requires data migration).

3. Generate a versioned migration file following the framework's naming convention:
   - Flyway: `V20240115_001__add_status_column_to_orders.sql`
   - Alembic: `alembic revision --autogenerate -m "add status column to orders"`
   - Prisma: Edit `schema.prisma` then `npx prisma migrate dev --name add_status_to_orders`
   - Knex: `npx knex migrate:make add_status_to_orders`

4. Write the UP migration (forward change) with these safety practices:
   - Add new columns as nullable first, then backfill, then set NOT NULL
   - Use `IF NOT EXISTS` for CREATE operations to make migrations idempotent
   - Add explicit transaction wrapping: `BEGIN; ... COMMIT;`
   - Include comments explaining the business reason for each change

5. Write the DOWN migration (rollback) that exactly reverses the UP migration. For column additions, the DOWN drops the column. For table renames, the DOWN renames back. For data transformations, the DOWN must restore original data (store it in a backup column or table if needed).

6. Validate the migration on staging by running the full migration sequence:
   - Apply the UP migration and verify schema matches expectations
   - Run the application test suite against the migrated schema
   - Apply the DOWN migration and verify the schema returns to its original state
   - Re-apply the UP migration to confirm idempotency

7. For zero-downtime migrations on production, follow the expand-contract pattern:
   - Phase 1 (expand): Add new column/table without removing old ones. Deploy application code that writes to both old and new.
   - Phase 2 (migrate): Backfill new column/table from old data in batches.
   - Phase 3 (contract): Deploy application code that reads only from new. Drop old column/table in a future migration.

8. Handle large table migrations (>10M rows) with online DDL tools: `pg_repack` for PostgreSQL, `pt-online-schema-change` for MySQL, or `gh-ost` for MySQL. These tools create a shadow table, replicate changes, then swap atomically.

9. Update the migration history table and verify the migration version matches expectations. Run `flyway info` or `alembic current` to confirm.

10. Document the migration in a changelog with: migration version, description, tables affected, estimated execution time, rollback procedure, and any required application deployments.

## Output

- **Migration files** (UP and DOWN) in the target framework's format
- **Pre-flight validation script** checking prerequisites before migration execution
- **Data backfill scripts** for non-nullable column additions on existing tables
- **Rollback runbook** with step-by-step instructions for reverting in production
- **Migration changelog entry** documenting the change for team reference

## Error Handling

| Error | Cause | Solution |
|-------|-------|---------|
| Migration fails with lock timeout | Long-running queries blocking DDL locks on the target table | Set `lock_timeout = '5s'` to fail fast; retry during low-traffic period; use `pg_repack` for lock-free operations |
| Column cannot be dropped because of dependent views | Views or materialized views reference the column being removed | Drop or recreate dependent views first; use `CASCADE` only after verifying all dependents are acceptable to drop |
| NOT NULL constraint violation during migration | Existing rows have NULL values in a column being made NOT NULL | Add a backfill step: `UPDATE table SET column = default_value WHERE column IS NULL` before adding NOT NULL |
| Migration version conflict | Two developers created migrations with the same version number | Use timestamp-based versioning; resolve by renaming one migration and re-running |
| Rollback fails because data was inserted after UP migration | New data in added columns has no place to go in the old schema | Design rollback to handle new data (backup column, log discarded data); accept that some rollbacks are data-lossy |

## Examples

**Adding a status enum column to an orders table**: Generate a migration that: (1) creates the enum type `CREATE TYPE order_status AS ENUM ('pending', 'shipped', 'delivered')`, (2) adds column `ALTER TABLE orders ADD COLUMN status order_status`, (3) backfills `UPDATE orders SET status = 'delivered' WHERE shipped_at IS NOT NULL`, (4) sets NOT NULL `ALTER TABLE orders ALTER COLUMN status SET NOT NULL`. Rollback drops the column and enum type.

**Splitting a monolith users table into users and profiles**: Expand phase adds `profiles` table and triggers to sync data. Migrate phase copies existing profile data in batches of 10,000 rows. Contract phase drops profile columns from users table after application code is updated.

**Renaming a column without downtime**: Create a migration that adds the new column, adds a trigger to sync writes between old and new columns, backfills existing data, deploys application code using the new column name, then drops the old column and trigger in a follow-up migration.

## Resources

- Flyway documentation: https://documentation.red-gate.com/fd
- Alembic tutorial: https://alembic.sqlalchemy.org/en/latest/tutorial.html
- Prisma Migrate: https://www.prisma.io/docs/orm/prisma-migrate
- pg_repack (lock-free table rewrite): https://reorg.github.io/pg_repack/
- gh-ost (GitHub online schema change): https://github.com/github/gh-ost

Related Skills

managing-test-environments

1868
from jeremylongshore/claude-code-plugins-plus-skills

Test provision and manage isolated test environments with configuration and data. Use when performing specialized testing. Trigger with phrases like "manage test environment", "provision test env", or "setup test infrastructure".

managing-snapshot-tests

1868
from jeremylongshore/claude-code-plugins-plus-skills

Create and validate component snapshots for UI regression testing. Use when performing specialized testing. Trigger with phrases like "update snapshots", "test UI snapshots", or "validate component snapshots".

managing-database-tests

1868
from jeremylongshore/claude-code-plugins-plus-skills

Test database testing including fixtures, transactions, and rollback management. Use when performing specialized testing. Trigger with phrases like "test the database", "run database tests", or "validate data integrity".

managing-ssltls-certificates

1868
from jeremylongshore/claude-code-plugins-plus-skills

Execute this skill enables AI assistant to manage and monitor ssl/tls certificates using the ssl-certificate-manager plugin. it is activated when the user requests actions related to ssl certificates, such as checking certificate expiry, renewing certificates, ... Use when appropriate context detected. Trigger with relevant phrases based on skill purpose.

managing-autonomous-development

1868
from jeremylongshore/claude-code-plugins-plus-skills

Execute enables AI assistant to manage sugar's autonomous development workflows. it allows AI assistant to create tasks, view the status of the system, review pending tasks, and start autonomous execution mode. use this skill when the user asks to create a new develo... Use when appropriate context detected. Trigger with relevant phrases based on skill purpose.

managing-network-policies

1868
from jeremylongshore/claude-code-plugins-plus-skills

Execute use when managing Kubernetes network policies and firewall rules. Trigger with phrases like "create network policy", "configure firewall rules", "restrict pod communication", or "setup ingress/egress rules". Generates Kubernetes NetworkPolicy manifests following least privilege and zero-trust principles.

managing-environment-configurations

1868
from jeremylongshore/claude-code-plugins-plus-skills

Implement environment and configuration management with comprehensive guidance and automation. Use when you need to work with environment configuration. Trigger with phrases like "manage environments", "configure environments", or "sync configurations".

managing-deployment-rollbacks

1868
from jeremylongshore/claude-code-plugins-plus-skills

Deploy use when you need to work with deployment and CI/CD. This skill provides deployment automation and orchestration with comprehensive guidance and automation. Trigger with phrases like "deploy application", "create pipeline", or "automate deployment".

managing-container-registries

1868
from jeremylongshore/claude-code-plugins-plus-skills

Execute use when you need to work with containerization. This skill provides container management and orchestration with comprehensive guidance and automation. Trigger with phrases like "containerize app", "manage containers", or "orchestrate deployment".

monitoring-database-transactions

1868
from jeremylongshore/claude-code-plugins-plus-skills

Monitor use when you need to work with monitoring and observability. This skill provides health monitoring and alerting with comprehensive guidance and automation. Trigger with phrases like "monitor system health", "set up alerts", or "track metrics".

managing-database-sharding

1868
from jeremylongshore/claude-code-plugins-plus-skills

Process use when you need to work with database sharding. This skill provides horizontal sharding strategies with comprehensive guidance and automation. Trigger with phrases like "implement sharding", "shard database", or "distribute data".

scanning-database-security

1868
from jeremylongshore/claude-code-plugins-plus-skills

Process use when you need to work with security and compliance. This skill provides security scanning and vulnerability detection with comprehensive guidance and automation. Trigger with phrases like "scan for vulnerabilities", "implement security controls", or "audit security".