원클릭으로
db-apply-migration
Safely apply a migration with pre/post snapshots and exclusive lock
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Safely apply a migration with pre/post snapshots and exclusive lock
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
Execute story development with selectable automation modes to accommodate different developer preferences, skill levels, and story complexity.
Set up a Kord-aligned documentation baseline (no legacy frameworks)
Create Next Story Task methodology and workflow
Validate Next Story Task methodology and workflow
advanced-elicitation methodology and workflow
No checklists needed - this task facilitates brainstorming sessions, validation is through user interaction methodology and workflow
| name | db-apply-migration |
| description | Safely apply a migration with pre/post snapshots and exclusive lock |
| agent | architect |
| subtask | false |
Safely apply a migration with pre/post snapshots and exclusive lock
Parameter:* mode (optional, default: interactive)
Purpose:* Definitive pass/fail criteria for task completion
Checklist:*
acceptance-criteria:
- [ ] Data persisted correctly; constraints respected; no orphaned data
type: acceptance-criterion
blocker: true
validation: |
Assert data persisted correctly; constraints respected; no orphaned data
error_message: "Acceptance criterion not met: Data persisted correctly; constraints respected; no orphaned data"
Strategy:* retry
Common Errors:*
Error:* Connection Failed
Error:* Query Syntax Error
Error:* Transaction Rollback
path (string): Path to SQL migration fileAsk user to confirm:
{path}$SUPABASE_DB_URL (redacted)CRITICAL*: If user says dry-run not done, stop and recommend: dry-run {path}
Ensure no concurrent migrations:
psql "$SUPABASE_DB_URL" -v ON_ERROR_STOP=1 -c \
"SELECT pg_try_advisory_lock(hashtext('dbsage:migrate')) AS got" \
| grep -q t || { echo "❌ Another migration is running"; exit 1; }
echo "✓ Migration lock acquired"
Create schema-only snapshot before changes:
TS=$(date +%Y%m%d%H%M%S)
mkdir -p supabase/snapshots supabase/rollback
pg_dump "$SUPABASE_DB_URL" --schema-only --clean --if-exists \
> "supabase/snapshots/${TS}_before.sql"
echo "✓ Pre-migration snapshot: supabase/snapshots/${TS}_before.sql"
echo $TS > /tmp/dbsage_migration_ts
Run migration in transaction:
echo "Applying migration..."
psql "$SUPABASE_DB_URL" -v ON_ERROR_STOP=1 -f {path}
if [ $? -eq 0 ]; then
echo "✓ Migration applied successfully"
else
echo "❌ Migration failed - rolling back..."
# Advisory lock will be released on disconnect
exit 1
fi
Create snapshot after changes:
TS=$(cat /tmp/dbsage_migration_ts)
pg_dump "$SUPABASE_DB_URL" --schema-only --clean --if-exists \
> "supabase/snapshots/${TS}_after.sql"
echo "✓ Post-migration snapshot: supabase/snapshots/${TS}_after.sql"
diff -u "supabase/snapshots/${TS}_before.sql" \
"supabase/snapshots/${TS}_after.sql" \
> "supabase/snapshots/${TS}_diff.patch" || true
echo "✓ Diff saved: supabase/snapshots/${TS}_diff.patch"
psql "$SUPABASE_DB_URL" -v ON_ERROR_STOP=1 -c \
"SELECT pg_advisory_unlock(hashtext('dbsage:migrate'));"
echo "✓ Migration lock released"
Present options to user:
1. Run smoke tests - smoke-test
2. Check RLS coverage - rls-audit
3. Verify query performance - analyze-hotpaths
4. Done for now
✅ Migration Applied Successfully
Timestamp: {TS}
Migration: {path}
Snapshots:
- Before: supabase/snapshots/{TS}_before.sql
- After: supabase/snapshots/{TS}_after.sql
- Diff: supabase/snapshots/{TS}_diff.patch
Next steps:
smoke-test - Validate migration
rls-audit - Check security
rollback {TS} - Undo if needed
If migration needs to be undone:
rollback supabase/snapshots/{TS}_before.sql
Or create manual rollback script in supabase/rollback/{TS}_rollback.sql
❌ Another migration is running
Wait for completion or check for stuck locks:
SELECT * FROM pg_locks WHERE locktype = 'advisory';
✅ Advisory lock prevents concurrent migrations
✅ Pre/post snapshots for comparison
✅ ON_ERROR_STOP prevents partial application
✅ Transaction-wrapped execution
✅ Automatic diff generation
✅ Rollback instructions provided