Skip to content

SQL · correctness

idempotent-ddl

sql:idempotent-ddl

DDL without IF [NOT] EXISTS — migrations must be safe to re-run.

Code
SARJ102
Default
error
Fix
none
Languages
sql

Why

A partially applied migration may be retried, so unconditional object creation or removal can fail before recovery completes.

Fix

Use the supported IF NOT EXISTS or IF EXISTS form for the DDL statement.

Before / after

Executed by this rule’s unit tests.

Before

Table creation that fails on replay

migrations/001_orders.sql · focus
CREATE TABLE orders (id BIGINT PRIMARY KEY);

After

Replay-safe table creation

migrations/001_orders.sql · focus
CREATE TABLE IF NOT EXISTS orders (id BIGINT PRIMARY KEY);

Limits

  • Dialect-specific DDL forms are checked only where the guard syntax is supported.