Works with the AI Agents you already use

    Safe DB Migrations: Zero-Downtime Schema Engine

    by Tushar

    1

    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

    /mo

    Subscription · cancel anytime

    Secure checkout via Stripe

    • Always the latest version
    • Delivered live to your agent via MCP
    • Cancel anytime, access ends at period end
    Security scanned
    Safe DB Migrations: Zero-Downtime Schema Engine

    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.

    MetricNaive MigrationSafe Zero-Downtime Migration
    Table Lock TypeAccessExclusiveLock (Blocks Reads & Writes)ShareUpdateExclusiveLock (Reads & Writes Continue)
    Max Lock Wait TimeUnlimited (Causes Connection Starvation)3 seconds (SET lock_timeout = '3s')
    Downtime Window45 - 120 seconds0.00 seconds
    ReversibilityDangerous manual surgeryAutomated 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

    1. Deploy Phase 1: Run the migration script above against your staging replica.
    2. In your application layer, configure your ORM to dual-write or supply defaults for phone_number.
    3. If backfilling historical records, run the throttled batch update utility (scripts/audit_migration.py --backfill) during off-peak hours.
    $5.95/mo

    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 VALID constraint 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.py to 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. 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. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 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
    PythonNode.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

    Terminal / Shell
    Read Files
    Write Files
    Listed13 days ago

    What's inside

    Frequently Asked Questions

    Yes! It provides step-by-step guidance on overriding Prisma's default blocking migration DDL with `--create-only` and running concurrent indexing in Drizzle.

    This skill supports PostgreSQL, MySQL, and SQLite. It is compatible with popular ORMs including Prisma, Drizzle, TypeORM, Django, and Alembic, providing either raw SQL or framework-specific migration code.

    Yes, for complex operations like column renames, the skill provides a structured "Expand and Contract" plan. This includes the necessary dual-write logic and multi-step migration scripts to ensure your application stays functional throughout the transition.

    You receive the core AI skill protocols for generating safe SQL, a specialized Python-based audit script to lint your migrations for risks, and a library of non-locking migration patterns for various database engines.

    General-purpose LLMs often suggest simple but dangerous commands (like a standard 'CREATE INDEX') that lock tables. This skill specifically encodes deep database engine behaviors, such as enforcing lock timeouts and ensuring concurrent operations run outside of transaction blocks.

    Due to engine-level requirements, PostgreSQL concurrent operations cannot be run inside a transaction block. You will need to ensure your migration runner (like Prisma or Alembic) is configured to allow non-transactional migrations for those specific files.

    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.