Skip to content

CLI Reference#

Command-line interface for pgdorm migrations, drift detection, schema assimilation, and live PostgreSQL compatibility checks (python -m pgdorm or pgdorm).

For guided workflows, see The Foreman's Toolkit (CLI & CI), First Shift, and Schema Assimilation.

Global Options & Configuration#

Global options precede the subcommand:

pgdorm [--app MODULE:ATTR] [--config PATH] [--dsn DSN] [--schema SCHEMA]
       [--ignore-table TABLE ...] [--pool-mode {direct,transaction}] [--json]
       <subcommand> [subcommand-options]
Flag Type / Values Default Description
--app module.path:attr [tool.pgdorm].app Dotted Python target pointing to a pg.MigrationApp (not required for doctor, audit-schema, scaffold).
--config PATH ./pyproject.toml Path to pyproject.toml containing [tool.pgdorm].
--dsn str $DATABASE_URL or [tool.pgdorm].dsn PostgreSQL connection DSN.
--schema str App schema / public Override the target PostgreSQL schema.
--ignore-table str (repeatable) [] Table name to leave untouched during introspection and diffing. Merged with app.ignore_tables and [tool.pgdorm].ignore_tables.
--pool-mode direct | transaction direct Connection pool mode. Use transaction behind PgBouncer transaction pooling.
--json flag false Emit machine-readable JSON output.

Environment Variables & pyproject.toml#

  • DATABASE_URL: Default PostgreSQL DSN when --dsn is omitted. Takes precedence over [tool.pgdorm].dsn.
  • [tool.pgdorm] in pyproject.toml:
[tool.pgdorm]
app = "myapp.db:migrations"
dsn = "postgresql://postgres@localhost:5432/myapp"
ignore_tables = ["legacy_audit"]

Exit Codes#

Code Meaning
0 Success (or clean check with no drift / migratable schema).
1 Configuration error, missing DSN/app, failed doctor probe, or MigrationError.
2 drift detected live pg_catalog divergence from the last recorded _pgdorm snapshot.
3 audit-schema found blocking issues (migratable == False).

Subcommands#

doctor#

Probe live PostgreSQL compatibility without requiring --app. Checks server version (>= 180000), wire protocol capabilities, and optional write-path features.

  • --write-probe: Also exercise pg_temp, UNNEST, and RETURNING OLD/NEW inside a rolled-back transaction.
  • --no-prepared-statements: Run probes using unnamed statements (for proxies with max_prepared_statements = 0).

status#

Show the latest applied migration in _pgdorm, whether live pg_catalog has drifted, and the count of pending schema operations.

plan#

Dry-run the schema diff between live pg_catalog and the MigrationApp target tables, printing the compiled PostgreSQL DDL steps and lock levels.

  • --name (default "sync"): Human-readable name for the planned migration.

sync#

Zero-file declarative sync: compute the diff, execute online DDL with lock-timeout guards, and record the new snapshot directly in _pgdorm.

  • --name (default "sync"): Migration name recorded in _pgdorm.
  • --allow-destructive: Permit destructive operations (DROP TABLE, DROP COLUMN, etc.).
  • --allow-breaking: Permit breaking operations (type narrowing, unsafe rewrites).
  • --allow-drift: Proceed even if live pg_catalog drifted from the last recorded snapshot.

upgrade#

Apply code-defined pg.Migration values registered on the MigrationApp in version order, verifying checksums against _pgdorm.

  • --allow-destructive: Permit destructive operations in pending migrations.
  • --allow-breaking: Permit breaking operations in pending migrations.

baseline#

Adopt an existing live schema into the _pgdorm ledger without executing DDL, recording the current catalog snapshot as the initial revision.

  • --version (default "0000_baseline"): Baseline version identifier.
  • --name (default "baseline"): Baseline migration name.

drift#

Compare live pg_catalog against the last recorded _pgdorm snapshot. Exits with code 0 when clean or 2 when drift is detected.

history#

Print the migration history stored in the _pgdorm ledger table.

render#

Render a code-defined pg.Migration Python value from the current schema diff to stdout.

  • --version (default "0001"): Migration version string.
  • --name (default "auto"): Migration name.
  • --var-name (default None): Optional Python variable name for the rendered Migration constant.

audit-schema#

Audit a live PostgreSQL schema for assimilation readiness (works without --app). Reports unmanaged views, triggers, routines, enum types, and TypeTable candidates. Exits 0 if migratable or 3 if blockers exist.

  • --no-definitions: Omit live SQL definitions for unmanaged views, triggers, and routines from JSON output.

scaffold#

Introspect a live PostgreSQL schema and generate pgdorm Table models plus an AdoptionMap (works without --app).

  • --mode (assimilate | bind, default assimilate): Generate idiomatic pgdorm models with rename/modernize mappings (assimilate) or bind to existing physical names (bind).
  • --type-table (repeatable): Mark specific lookup tables to scaffold as pg.TypeTable subclasses.
  • --modernize: Upgrade legacy column types (e.g. serial -> IDENTITY, varchar -> text) in the generated models.
  • --surrogate-pk (bigint | uuidv7, default bigint): Surrogate primary key type for tables lacking a compliant <table_name>_id key.
  • --out (PATH, default stdout): Write generated Python module to a file instead of stdout.

adopt-plan#

Validate an AdoptionMap against live pg_catalog and dry-run the phased assimilation plan with downstream object impact warnings.

  • --adoption (required): Dotted Python target module.path:attr pointing to a pgm.AdoptionMap.
  • --version (default "0001_assimilate"): Version string for the planned adoption migration.
  • --name (default "assimilate"): Name for the planned adoption migration.
  • --format (text | json | sql, default text): Output format for the plan and impact report.