Atomic 3phase ddl scripts

From CarlosCaPe/octorato · View interactive version

Atomic 3phase ddl scripts is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Atomic 3-Phase DDL Scripts

How to use this skill

Skill file

Atomic 3-Phase DDL Scripts

What

A script architecture that separates database modifications into three

distinct phases within a single SQL file:

| Phase | Purpose | Behavior on Failure |

|-------|---------|---------------------|

| Phase 1 | Pre-check, gap analysis, dry-run gate | RAISE EXCEPTION stops all |

| Phase 2 | Execute changes (single DO block) | Rolls back atomically |

| Phase 3 | Post-check, verify final state | Reports pass/fail counts |

Why

Separating concerns makes scripts auditable, safe, and debuggable.

Phase 1 tells you what will happen before anything changes. Phase 2 does the

work atomically. Phase 3 proves it worked.

How

-- Phase 1: Pre-check
DO $$
BEGIN
    -- gap analysis: what needs doing?
    -- dry-run gate: RAISE EXCEPTION if dry_run = true
END $$;

-- Phase 2: Execute (atomic DO block)
DO $$
BEGIN
    -- all modifications inside ONE block
    -- if any fails, everything rolls back
END $$;

-- Phase 3: Post-check
DO $$
BEGIN
    -- verify every expected outcome
    -- report confirmed/failed counts
END $$;

The key insight is that Phase 2 is a single DO block. If any statement inside

it fails, PostgreSQL rolls back the entire block -- you never get a half-applied

state.

When to Use

Where We Used It

Gotchas

its own phase outside a DO block

and our Node.js runner). If wrapped in explicit BEGIN/COMMIT, multiple DO

blocks share one transaction -- avoid this for 3-Phase scripts

prevents subsequent statements from running (PG 16 docs:

plpgsql-errors-and-messages.html)


Category: Architecture | Origin:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.