Database guard pattern

From CarlosCaPe/octorato · View interactive version

Database guard pattern is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Database Guard Pattern

How to use this skill

Skill file

Database Guard Pattern

What

A safety check at the top of every DDL script that verifies the current

database name matches an expected pattern. If the script connects to the wrong

database, it aborts immediately.

Why

One misclick in a connection string can point a destructive script at the wrong

database. A database guard is the last line of defense. It costs one line of SQL

and prevents catastrophic mistakes.

How

DO $$
DECLARE
    v_db text := current_database();
BEGIN
    IF v_db NOT ILIKE 'myapp%' THEN
        RAISE NOTICE 'SAFETY: This script only runs on myapp databases';
        RAISE NOTICE '  Current database: %', v_db;
        RAISE EXCEPTION 'SAFETY: Wrong database -- aborting all phases';
    END IF;
END $$;

Pattern variations:

When to Use

Where We Used It

Gotchas

the current DO block, while RAISE EXCEPTION aborts execution


Category: Safety | 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.