New composite types are created with unquoted attribute names (mixed-case attributes become lower case)
Summary
When pgschema creates a composite type, it writes the attribute names without quotes. An attribute declared as "organizationId" is therefore created as organizationid. The next plan wants to drop and re-add the attributes, so the plan never converges.
Adding the same attribute to an existing type works, because ALTER TYPE ... ADD ATTRIBUTE quotes the name. pgschema dump also writes the names unquoted, so a dumped file does not round-trip either.
Versions
- pgschema 1.14.1, 1.15.0 and 2.0.0 (the official
linux-arm64release binaries; all three behave the same) - PostgreSQL 16.11
Reproduction
schema.sql:
CREATE TYPE customer_response AS (status text, "organizationId" integer, "firstName" text);
Against an empty database:
pgschema plan --host 127.0.0.1 --port 5432 --user postgres --db repro --schema public --file schema.sql
pgschema apply --host 127.0.0.1 --port 5432 --user postgres --db repro --schema public --file schema.sql --auto-approve
psql -d repro -Atc "select string_agg(attname, ', ' order by attnum) from pg_attribute
where attrelid = 'customer_response'::regclass and attnum > 0"
pgschema plan --host 127.0.0.1 --port 5432 --user postgres --db repro --schema public --file schema.sql
Actual
The first plan, with the attribute names unquoted:
CREATE TYPE customer_response AS (status text, organizationId integer, firstName text);
The attributes in the database: status, organizationid, firstname.
The second plan (Plan: 4 to modify.):
ALTER TYPE customer_response DROP ATTRIBUTE organizationid;
ALTER TYPE customer_response DROP ATTRIBUTE firstname;
ALTER TYPE customer_response ADD ATTRIBUTE "organizationId" integer;
ALTER TYPE customer_response ADD ATTRIBUTE "firstName" text;
Applying that plan fixes the names, but only after a second run. With a non-empty type that is a destructive change that should not be needed.
Expected
CREATE TYPE customer_response AS (status text, "organizationId" integer, "firstName" text);
After the apply, the second plan should report No changes detected.
For comparison: modifying an existing type works
Start from CREATE TYPE customer_response AS (status text); and plan the same schema.sql:
ALTER TYPE customer_response ADD ATTRIBUTE "organizationId" integer;
ALTER TYPE customer_response ADD ATTRIBUTE "firstName" text;
The attributes in the database are then status, organizationId, firstName, and the second plan reports No changes detected.
Dump
On that database (correct names organizationId, firstName), pgschema dump prints:
CREATE TYPE customer_response AS (status text, organizationId integer, firstName text);
Planning this dumped file against the same database would rename the attributes to lower case.
Cause
In internal/diff/type.go (v2.0.0), line 375, generateTypeSQL writes the attribute name as is:
attributes = append(attributes, fmt.Sprintf("%s %s", attr.Name, dataType))
The ADD ATTRIBUTE, ALTER ATTRIBUTE and DROP ATTRIBUTE statements in the same file use ir.QuoteIdentifier(attr.Name) (lines 231, 261 and 263), which is why modifying a type works.
Suggested fix
attributes = append(attributes, fmt.Sprintf("%s %s", ir.QuoteIdentifier(attr.Name), dataType))
Assumption: the dump output comes from the same function, since it shows the same unquoted form. If so, this also fixes the dump.
A test case like testdata/diff/create_type/issue_625_composite_attributes with a mixed-case attribute in a new type, and one for the dump, would cover it.
Workaround we use
We create new types in two releases:
- A release that creates each type with only its lower-case attributes (
AS ()when it has none). - A release that adds the mixed-case attributes, which
ADD ATTRIBUTEquotes correctly.
The files list lower-case attributes first, so the final attribute order matches.
Related: #625 (composite attribute changes, which added the quoted ALTER TYPE statements).