Analyze SQL

Analyze raw .sql migration files directly.

Basic Usage

bash
pgfence analyze migrations/*.sql

Multiple Files

bash
pgfence analyze migrations/001_add_users.sql migrations/002_add_posts.sql

Glob Patterns

bash
pgfence analyze "migrations/**/*.sql"

Scoped Auto-Fix

bash
pgfence analyze --fix migrations/*.sql

--fix is an explicit allowlist, not a general rewrite engine. It can add CONCURRENTLY to supported CREATE INDEX and DROP INDEX statements and prepend missing lock_timeout, statement_timeout, and idle_in_transaction_session_timeout settings.

  • Only files that resolve to the raw sql format are edited.
  • Index fixes are refused inside an explicit transaction block because PostgreSQL rejects concurrent index operations there.
  • With --ci, pgfence reads the edited files again before it decides the exit code.
  • Every skipped fix includes a reason.

Scaffold Multi-Step Recipes

bash
pgfence analyze --fix --split migrations/*.sql

--split adds sibling migration files for three supported shapes: a plain ADD COLUMN ... NOT NULL, a named foreign key without NOT VALID, and a plain unique constraint without USING INDEX. It never edits the original migration. Generated backfill files are fully commented out and clearly marked for manual, out-of-band execution.

When Nothing Is Analyzed

Since 0.8.0, analyze exits with code 2 when the entire run finds zero SQL statements. Empty files, comment-only files, and unsupported migration wrappers cannot report a successful 100% coverage result. A file with no statements inside a larger run is still reported, while the run continues to evaluate the statements found in other files.