Skip to content
Agentmart
Submit a tool

database-migration

Database migration best practices — schema changes, zero-downtime migrations, rollback strategies, and data migration patterns.

Skill by Claude-Code-Community-Ireland

IndexedMIT★ 11
Write a review

Install

Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.

npx agentmarket install skill/claude-code-community-ireland-database-migration

Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.

Get the weekly diff

The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.

We use your email only for this newsletter. Privacy

About

Database migration best practices — schema changes, zero-downtime migrations, rollback strategies, and data migration patterns. Reference when modifying database schemas.

Use cases: backend, data

No reviews yet. Installed it? Reviews come from verified installers only. How reviews work

Permissions

What this skill can do and what it asks for, from its SKILL.md. Checked Oct 25, 2026.

Tools
No tools found
Secrets
None asked for
Install scripts
None found

Network hosts it talks to 1

  • codeload.github.com · found in install command

Files it touches

  • ~/.claude/skills/database-migration

What's inside

Skill name
database-migration
Description
Database migration best practices — schema changes, zero-downtime migrations, rollback strategies, and data migration patterns. Reference when modifying database schemas.
License
MIT
Instructions
346 lines in SKILL.md · view on GitHub

Files (1)

  • SKILL.md

SKILL.md (first lines)

# Database Migration

## Migration File Conventions

Every schema change must be captured in a versioned migration file. Follow these conventions:

- **Timestamped naming**: Use `YYYYMMDDHHMMSS_descriptive_name` (e.g., `20240315143022_add_status_to_orders.sql`).
- **Sequential ordering**: Migrations run in order. Never insert a migration before one that has already been applied.
- **One concern per file**: Each migration handles a single logical change. Do not combine unrelated schema changes.
- **Descriptive names**: The filename should describe the change, not the ticket number. Use verbs like `add`, `remove`, `create`, `drop`, `rename`, `alter`.
- **Idempotent when possible**: Use `IF NOT EXISTS` / `IF EXISTS` guards to make re-runs safe.

```
migrations/
  20240301100000_create_users_table.sql
  20240305120000_add_email_index_to_users.sql
  20240310090000_create_orders_table.sql
  20240315143022_add_status_to_orders.sql
```

## Forward and Rollback Pairs

Every migration must have a corresponding rollback. If you cannot write a safe rollback, document why and flag it for review.

```sql
-- Migration: 20240315143022_add_status_to_orders.sql

-- UP
ALTER TABLE orders ADD COLUMN status VARCHAR(50) DEFAULT 'pending';
CREATE INDEX idx_orders_status ON orders (status);

-- DOWN
DROP INDEX IF EXISTS idx_orders_status;
ALTER TABLE orders DROP COLUMN IF EXISTS status;
```

### Rollback Rules

| Scenario | Rollback Strategy |
|----------|-------------------|
| Add column | Drop column |
| Add index | Drop index |
| Create table | Drop table |
| Add constraint | Drop constraint |
| Rename column | Rename back |
| Drop column | Cannot auto-rollback — must restore from backup or use prior migration to re-add |
| Data backfill | Reverse backfill or accept data state |

## Zero-Downtime Schema Changes

When your application cannot tolerate downtime during deploys, follow the expand-and-contract pattern. Never make breaking schema changes in a single step.

### Step-by-Step: Renaming a Column

Renaming a column directly (`ALTER TABLE ... RENAME COLUMN`) breaks running application instances that reference the old name. Instead:

1. **Deploy 1 — Expand**: Add the new column alongside the old one.
   ```sql
   ALTER TABLE users ADD COLUMN full_name VARCHAR(255);
   ```

2. **Deploy 2 — Dual Write**: Update application code to write to both columns.
   ```javascript
   // Write to both columns
   await db.query(
     'UPDATE users SET name = $1, full_name = $1 WHERE id = $2',
     [name, userId]
   );
   ```

3. **Deploy 3 — Backfill**: Copy existing data from old column to new column.
   ```sql
   UPDATE users SET full_name = name WHERE full_name IS NULL;
   ```

4. **Deploy 4 — Cut Over**: Update application to read from and write to new column only.
   ```javascript
   // Read from new column
   const user = await db.query('SELECT full_name FROM users WHERE id = $1', [userId]);
   ```

5. **Deploy 5 — Contract**: Drop the old column after a safe observation period.
   ```sql
   ALTER TABLE users DROP COLUMN name;
   ```

### Step-by-Step: Adding a NOT NULL Constraint

Adding a `NOT NULL` constraint on an existing column with null values will fail or lock the table. Safe approach:

1. Add a check constraint as `NOT VALID` (PostgreSQL) to avoid scanning existing rows:
   ```sql
   ALTER TABLE orders ADD CONSTRAINT orders_status_not_null
     CHECK (status IS NOT NULL) NOT VALID;
   ```

2. Backfill any null values:
   ```sql
   UPDATE orders SET status = 'unknown' WHERE status IS NULL;
   ```

3. Validate the constraint (acquires lighter lock):
   ```sql
   ALTER TABLE orders VALIDATE CONSTRAINT orders_status_not_null;
   ```

4. Optionally convert to a proper `NOT NULL` column constraint:
   ```sql
   ALTER TABLE orders ALTER COLUMN status SET NOT NULL;
   ALTER TABLE orders DROP CONSTRAINT orders_status_not_null;
   ```

## Dangerous Operations

| Operation | Risk | Safe Alternative |
|-----------|------|-----------------|
| `RENAME COLUMN` | Breaks queries referencing old name | Expand-and-contract pattern (add new, migrate, drop old) |
| `CHANGE COLUMN TYPE` | May require full table rewrite, long lock | Add new column with new type, backfill, swap |
| `DROP COLUMN` | Irreversible data loss | Verify no code references, back up data, then drop |

From Claude-Code-Community-Ireland/claude-code-resources, MIT.

More from Claude-Code-Community-Ireland/claude-code-resources

Signals

Stars
11
Forks
4
Open issues
4
Last commit (this folder)
Jan 29, 2026 (9 months ago)

From GitHub (fetched Oct 24, 2026).

Details

License
MIT
First seen
Oct 24, 2026
Last checked
Oct 24, 2026

More from Claude-Code-Community-Ireland

Cite this page

database-migration skill. Agent Mart. https://agentmart.dev/skills/claude-code-community-ireland-database-migration (last updated 2026-10-24)