Skip to content
Back to skills

Database Guard Pattern

ASecurity

Database Guard Pattern

  • 18 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 2, 2026
ai-agentsgosqldatabase

Works with

  • cli

Security analysis

A100/100

Scanned September 2, 2026

npx -y skills add CarlosCaPe/octorato --skill database-guard-pattern --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Database Guard Pattern?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Database Guard Pattern
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/carloscape-database-guard-pattern/badge)](https://www.skillsdirectory.com/skills/carloscape-database-guard-pattern)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: database-guard-pattern
description: "Database Guard Pattern"
metadata:
  short-description: "Database Guard Pattern"
  original-index: 03
---

# 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

```sql
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:
- Exact match: `v_db <> 'MyDatabase'`
- Prefix match: `v_db NOT ILIKE 'MyApp%'` (covers `_DEV`, `_QA`, `_PROD`)
- Suffix match: `v_db NOT ILIKE '%_appointments'`

## When to Use

- **Every** DDL script. No exceptions.
- Especially important for scripts that run across multiple environments

## Where We Used It

- **Project A**: `myapp%` guard
- **Project B**: `scheduler%` guard
- **Project C**: `analytics%` guard

## Gotchas

- Use `ILIKE` (case-insensitive) because database names can vary in casing
- Place the guard BEFORE any gap analysis -- fail fast
- The guard must use `RAISE EXCEPTION`, not `RETURN` -- `RETURN` only exits
  the current DO block, while `RAISE EXCEPTION` aborts execution

---

*Category: Safety | Origin: *

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.