db-apply-migration

Applies SQL migrations to PostgreSQL with advisory locks and pre/post schema snapshots.

3|Updated Feb 7, 2026
One-click install
npx skills add https://github.com/gabrielnsmnto/kord-aios --skill db-apply-migration-gabrielnsmnto
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: db-apply-migration
Source: https://github.com/gabrielnsmnto/kord-aios/tree/main/src/features/builtin-skills/kord-aios/database/db-apply-migration
Command: npx skills add https://github.com/gabrielnsmnto/kord-aios --skill db-apply-migration-gabrielnsmnto

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Applying database migrations manually risks concurrent execution conflicts, partial application, and no recovery path when something goes wrong. This Skill wraps migration execution in advisory locks, automatic snapshots, and transaction safety so schema changes are controlled and reversible. ## Core Features & Use Cases - Advisory Lock Protection: Acquires a PostgreSQL advisory lock via pg_try_advisory_lock to prevent concurrent migrations from corrupting the schema. - Automatic Snapshots: Creates schema-only pg_dump snapshots before and after the migration, plus an optional diff patch for review. - Transaction-Safe Execution: Runs migrations with ON_ERROR_STOP so failures roll back automatically without partial application. - Use Case: You have a new SQL migration file for your Supabase database. Run this Skill to apply it safely, get before/after snapshots stored in supabase/snapshots, and receive a rollback command if anything needs to be undone. ## Quick Start Apply the migration file at supabase/migrations/20240101_add_users.sql to my database with snapshots and locking enabled.

Frequently Asked Questions about db-apply-migration

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I safely apply a SQL migration to PostgreSQL?▼

Use psql with ON_ERROR_STOP=1 to run the migration file in a transaction so failures roll back automatically. Acquire an advisory lock with pg_try_advisory_lock first to prevent concurrent migrations, and take a pg_dump schema snapshot beforehand for rollback.

How to prevent concurrent database migrations in PostgreSQL?▼

Use PostgreSQL advisory locks via pg_try_advisory_lock with a hashed key like hashtext('dbsage:migrate'). If the lock is already held, the command returns false and the migration aborts instead of conflicting with the running one.

Does this work with Supabase databases?▼

Yes, it connects through the SUPABASE_DB_URL environment variable using standard psql and pg_dump. Snapshots are stored in the supabase/snapshots directory and rollback scripts in supabase/rollback.

What happens if a migration fails mid-execution?▼

PostgreSQL rolls back the transaction automatically because ON_ERROR_STOP is enabled, leaving the database unchanged. The advisory lock releases on disconnect, and the pre-migration snapshot remains available for inspection.

How do I roll back an applied migration?▼

Run the rollback command pointing to the pre-migration snapshot file, for example rollback supabase/snapshots/{TS}_before.sql. You can also create a manual rollback script in supabase/rollback/{TS}_rollback.sql.