Split PostgreSQL DDL into Atomic Statements with PGPM

Dan Lynch

Dan Lynch

lesson header image

The imported module uses object SQL granularity: the table is written as one CREATE TABLE statement. Switch the statement shape to atomic, which separates the table, columns, and constraints into smaller statements. Leave the plan-entry granularity at its default, so the table is still one change.

bash
pgpm transform --cwd ./packages/library --granularity atomic --out ../../projections/atomic --check

sed -n '/^CREATE TABLE/,$p' packages/library/deploy/schemas/library/tables/books/table.sql
sed -n '/^CREATE TABLE/,$p' projections/atomic/library-atomic/deploy/schemas/library/tables/books/table.sql

With --cwd, the output directory is resolved from packages/library. The original module stays untouched. In the atomic copy, look for ALTER TABLE ... ADD COLUMN statements in the table's deploy script.

--check deploys both modules to scratch databases and compares their catalogs. Look for --check passed: catalogs are structurally equivalent. The SQL changed; the PostgreSQL objects did not.

The third SQL granularity, consolidated, combines statements where possible. SQL granularity doesn't decide how many entries go in pgpm.plan; that's a separate option.