Skip to content
Back to skills

Idempotent Sql Design

ASecurity

Idempotent SQL Design

  • 17 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 2, 2026
ai-agentsgosql

Security analysis

A100/100

Scanned September 2, 2026

npx -y skills add CarlosCaPe/octorato --skill idempotent-sql-design --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Idempotent Sql Design?

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

Security grade badge for Idempotent Sql Design
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/carloscape-idempotent-sql-design/badge)](https://www.skillsdirectory.com/skills/carloscape-idempotent-sql-design)

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: idempotent-sql-design
description: "Idempotent SQL Design"
metadata:
  short-description: "Idempotent SQL Design"
  original-index: 04
---

# Idempotent SQL Design

## What

Scripts that check the current state before acting, so they can be safely
re-run without causing errors or duplicate changes. Running the script once
or ten times produces the same final state.

## Why

Deployments fail. Networks drop. Operators re-run scripts "just to be sure."
An idempotent script handles all of these gracefully -- it skips work that's
already done and only applies what's missing.

## How

### Column renames
```sql
-- Check before acting
IF EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'Applicant'
      AND column_name = 'SpeialtyRequirement'  -- old name
) THEN
    ALTER TABLE public."Applicant"
        RENAME COLUMN "SpeialtyRequirement" TO "SpecialtyRequirement";
    RAISE NOTICE 'Renamed: SpeialtyRequirement -> SpecialtyRequirement';
ELSE
    RAISE NOTICE 'Skipped: SpeialtyRequirement (already renamed)';
END IF;
```

### Index creation
```sql
-- Only create if missing
IF NOT EXISTS (
    SELECT 1 FROM pg_indexes
    WHERE schemaname = 'public'
      AND indexname = 'ix_my_index'
) THEN
    CREATE INDEX ix_my_index ON public."MyTable" ("MyColumn");
END IF;
```

### FK constraints
```sql
-- Only add if missing
IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE constraint_name = 'fk_my_constraint'
) THEN
    ALTER TABLE public."MyTable"
        ADD CONSTRAINT fk_my_constraint
        FOREIGN KEY ("MyColumn") REFERENCES public."OtherTable"("Id");
END IF;
```

## Key Principle

The pattern is always:
1. **Query current state** (does the old column exist? does the index exist?)
2. **Act only if needed** (IF EXISTS / IF NOT EXISTS)
3. **Report what happened** (RAISE NOTICE with [OK] or [--] prefix)

## When to Use

- Every DDL script. Always.
- Especially critical for scripts deployed to multiple environments where
  some may already have partial changes applied

## Where We Used It

- ****: Column renames + procedure updates (check old/new state first)
- **/**: FK constraint creation (check constraint existence)
- **/**: Index creation (check index existence)

---

*Category: Reliability | 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.

Comments

Loading comments…

Newsletter

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