- Home
- Skills
- Data & Databases
- Safe DB Migrations: Zero-Downtime Schema Engine
Works with the AI Agents you already use
Safe DB Migrations: Zero-Downtime Schema Engine
by Tushar
Zero-downtime, non-locking database migrations and schema evolution for PostgreSQL, MySQL, and SQLite. Prevents production table locks, outage timeouts, and data loss.
$5.95
/moSubscription · cancel anytime
Secure checkout via Stripe
- Always the latest version
- Delivered live to your agent via MCP
- Cancel anytime, access ends at period end
Safe DB Migrations: Zero-Downtime Schema Engine
Example session with this skill installed
I need to add a new required 'phone_number' column and an index on 'organization_id' to our 'users' table in PostgreSQL. The table has 4.2 million active rows. Write the migration script and tell me how to run it safely without downtime.
- Read your context and instructions
- Compiled the safe db migrations
Summary
Generated a 100% zero-downtime migration strategy for PostgreSQL with 4.2M rows. The migration enforces strict lock timeouts, non-blocking concurrent indexing, and a two-phase NOT VALID constraint to avoid taking an AccessExclusiveLock on the users table.
| Metric | Naive Migration | Safe Zero-Downtime Migration |
|---|---|---|
| Table Lock Type | AccessExclusiveLock (Blocks Reads & Writes) | ShareUpdateExclusiveLock (Reads & Writes Continue) |
| Max Lock Wait Time | Unlimited (Causes Connection Starvation) | 3 seconds (SET lock_timeout = '3s') |
| Downtime Window | 45 - 120 seconds | 0.00 seconds |
| Reversibility | Dangerous manual surgery | Automated idempotent rollback script |
1. Zero-Downtime Migration Script (20261122_safe_users_update.sql)
-- Step 1: Prevent lock queue starvation
SET lock_timeout = '3s';
SET statement_timeout = '10s';
-- Step 2: Add column as nullable first (instant metadata update, no rewrite)
ALTER TABLE users
ADD COLUMN IF NOT EXISTS phone_number VARCHAR(32);
-- Step 3: Add NOT NULL check constraint without scanning existing rows
ALTER TABLE users
ADD CONSTRAINT chk_users_phone_number_not_null
CHECK (phone_number IS NOT NULL) NOT VALID;
-- Step 4: Validate constraint asynchronously without blocking concurrent writes
ALTER TABLE users
VALIDATE CONSTRAINT chk_users_phone_number_not_null;
-- Step 5: Create index concurrently (MUST run outside a multi-statement transaction)
COMMIT; -- End any open transaction block
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_org_id
ON users(organization_id);
2. Automated Safety Verification
$ python scripts/audit_migration.py 20261122_safe_users_update.sql
============================================================
SAFE-DB-MIGRATIONS AUDIT REPORT
============================================================
File: 20261122_safe_users_update.sql
Dialect: postgres
Status: PASSED
Risk Level: SAFE (Score: 0)
Findings: 0
------------------------------------------------------------
No unsafe migration anti-patterns detected! Clean zero-downtime DDL.
============================================================
3. Reversible Rollback Script (20261122_safe_users_update_down.sql)
SET lock_timeout = '3s';
DROP INDEX CONCURRENTLY IF EXISTS idx_users_org_id;
ALTER TABLE users DROP CONSTRAINT IF EXISTS chk_users_phone_number_not_null;
ALTER TABLE users DROP COLUMN IF EXISTS phone_number;
Next steps
- Deploy Phase 1: Run the migration script above against your staging replica.
- In your application layer, configure your ORM to dual-write or supply defaults for
phone_number. - If backfilling historical records, run the throttled batch update utility (
scripts/audit_migration.py --backfill) during off-peak hours.
Subscription · always the latest version
- Secure checkout via Stripe
- Cancel anytime
- Delivered live via MCP
About this skill
Never Let an AI Agent Crash Your Production Database Again
Every developer has experienced the nightmare: an AI coding agent generates a simple database migration like CREATE INDEX or ALTER TABLE ADD COLUMN NOT NULL, you run it in production, and seconds later your database connection pool explodes, queries freeze, and your application goes completely offline.
This happens because standard migrations acquire AccessExclusiveLock table locks that block all incoming read and write transactions.
Safe DB Migrations transforms your AI agent (Claude Code, Cursor, Copilot, Antigravity, or local LLMs) into a senior database site-reliability engineer that guarantees 100% zero-downtime, non-locking migrations.
What This Skill Solves
- Eliminates Production Table Locks: Replaces blocking queries with non-blocking equivalents (e.g.
CREATE INDEX CONCURRENTLY, lock timeouts). - Prevents Outage Cascades: Mandates
SET lock_timeout = '3s'and statement timeouts so migrations fail safely instead of starving database connections. - Automates Expand-and-Contract: Provides phased rollout recipes for column renames, type changes, and column drops without breaking running app instances.
- Guarantees Safe Foreign Keys & NOT NULL: Implements two-phase
NOT VALIDconstraint creation and validation without table rewrites. - Throttles Large Backfills: Batches updates in 5,000-row chunks with sleep yields to prevent WAL bloat and replication lag.
- Includes Automated Linter CLI: Bundles
scripts/audit_migration.pyto scan SQL files for 8 dangerous DDL anti-patterns before applying changes.
Supported Databases & ORMs
- Engines: PostgreSQL 11+, MySQL 8.0+, SQLite
- ORMs: Prisma, Drizzle ORM, TypeORM, Django ORM, Alembic (SQLAlchemy), Supabase, and Raw SQL
Concurrent index operations in Postgres cannot run inside transaction blocks. Requires Python to run the migration audit linter. Backfill logic assumes a primary key for batching.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Free to use
No account required to browse
Trust & safety
Security scan
Security scan passed 13 days ago
- Software
Python
Node.jsprisma
Runs 100% locally inside your AI coding agent. No database credentials or internet access required. The linter parses raw SQL and ORM files offline.
Permissions
What's inside
Frequently Asked Questions
New in Data & Databases
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Data & Databases.