pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Existing composite gains a table-row attribute before pgschema creates the required table

Open pettijohn opened this issue 3 weeks ago • 0 comments

Version

pgschema 1.14.1

Summary

When an existing composite type gains an attribute that uses a new table’s row type, pgschema emits ALTER TYPE before CREATE TABLE.

The apply fails because the required row type does not exist.

This differs from fresh creation: the composite type already exists before the apply.

Reproduction

Use an empty test schema named repro and a configured planning database.

Create before.sql:

CREATE TYPE replay_result AS ();

Create after.sql:

CREATE TABLE lot (
    id integer PRIMARY KEY
);

CREATE TYPE replay_result AS (
    lots lot[]
);

Apply the initial state:

pgschema apply \
  --db "$TARGET_DB" \
  --plan-db "$PLAN_DB" \
  --schema repro \
  --file before.sql \
  --auto-approve

Then apply the desired state:

pgschema apply \
  --db "$TARGET_DB" \
  --plan-db "$PLAN_DB" \
  --schema repro \
  --file after.sql \
  --auto-approve

Actual result

Pgschema places the attribute change before table creation:

ALTER TYPE replay_result ADD ATTRIBUTE lots lot[];

CREATE TABLE IF NOT EXISTS lot (
    id integer,
    CONSTRAINT lot_pkey PRIMARY KEY (id)
);

Application fails:

ERROR: type "lot[]" does not exist (SQLSTATE 42704)

Expected result

Create lot before adding the lots attribute to replay_result.

Related fixes

Version 1.14.1 fixes #625 and #626. This case combines an attribute change on an existing composite with a new table-row dependency.

pettijohn avatar Nov 08 '26 21:11 pettijohn