Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions ir/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func normalizePostgreSQLType(input string) string {
"pg_catalog.timetz": "timetz",
"pg_catalog.interval": "interval",

// Array types (internal PostgreSQL array notation)
// Array types (internal PostgreSQL array notation with underscore prefix)
"_text": "text[]",
"_int2": "smallint[]",
"_int4": "integer[]",
Expand All @@ -733,6 +733,31 @@ func normalizePostgreSQLType(input string) string {
"_timestamptz": "timestamptz[]",
"_interval": "interval[]",

// Array types (basetype[] format from SQL query)
"int2[]": "smallint[]",
"int4[]": "integer[]",
"int8[]": "bigint[]",
"float4[]": "real[]",
"float8[]": "double precision[]",
"bool[]": "boolean[]",
"varchar[]": "varchar[]",
"bpchar[]": "character[]",
"numeric[]": "numeric[]",
"uuid[]": "uuid[]",
"json[]": "json[]",
"jsonb[]": "jsonb[]",
"bytea[]": "bytea[]",
"inet[]": "inet[]",
"cidr[]": "cidr[]",
"macaddr[]": "macaddr[]",
"macaddr8[]": "macaddr8[]",
"date[]": "date[]",
"time[]": "time[]",
"timetz[]": "timetz[]",
"timestamp[]": "timestamp[]",
"timestamptz[]": "timestamptz[]",
"interval[]": "interval[]",

// Other common types
"pg_catalog.uuid": "uuid",
"pg_catalog.json": "json",
Expand Down Expand Up @@ -780,12 +805,6 @@ func normalizePostgreSQLType(input string) string {
return after
}

// Handle custom array types (internal PostgreSQL array notation)
// e.g., _my_enum_type -> my_enum_type[]
if strings.HasPrefix(typeName, "_") {
return typeName[1:] + "[]"
}

// Return as-is if no mapping found
return typeName
}
Expand Down
22 changes: 20 additions & 2 deletions ir/queries/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,15 @@ SELECT
CASE WHEN dn.nspname = c.table_schema THEN dt.typname
ELSE dn.nspname || '.' || dt.typname
END
WHEN dt.typtype = 'b' AND dt.typelem <> 0 THEN
-- Array types: apply same schema qualification logic to element type
CASE
WHEN en.nspname = 'pg_catalog' THEN et.typname || '[]'
WHEN en.nspname = c.table_schema THEN et.typname || '[]'
ELSE en.nspname || '.' || et.typname || '[]'
END
WHEN dt.typtype = 'b' THEN
-- Base types: qualify if not in pg_catalog or table's schema
-- Non-array base types: qualify if not in pg_catalog or table's schema
CASE
WHEN dn.nspname = 'pg_catalog' THEN c.udt_name
WHEN dn.nspname = c.table_schema THEN dt.typname
Expand Down Expand Up @@ -110,6 +117,8 @@ LEFT JOIN pg_attribute a ON a.attrelid = cl.oid AND a.attname = c.column_name
LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
LEFT JOIN pg_type dt ON dt.oid = a.atttypid
LEFT JOIN pg_namespace dn ON dt.typnamespace = dn.oid
LEFT JOIN pg_type et ON dt.typelem = et.oid
LEFT JOIN pg_namespace en ON et.typnamespace = en.oid
WHERE
c.table_schema NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
AND c.table_schema NOT LIKE 'pg_temp_%'
Expand Down Expand Up @@ -143,8 +152,15 @@ SELECT
CASE WHEN dn.nspname = c.table_schema THEN dt.typname
ELSE dn.nspname || '.' || dt.typname
END
WHEN dt.typtype = 'b' AND dt.typelem <> 0 THEN
-- Array types: apply same schema qualification logic to element type
CASE
WHEN en.nspname = 'pg_catalog' THEN et.typname || '[]'
WHEN en.nspname = c.table_schema THEN et.typname || '[]'
ELSE en.nspname || '.' || et.typname || '[]'
END
WHEN dt.typtype = 'b' THEN
-- Base types: qualify if not in pg_catalog or table's schema
-- Non-array base types: qualify if not in pg_catalog or table's schema
CASE
WHEN dn.nspname = 'pg_catalog' THEN c.udt_name
WHEN dn.nspname = c.table_schema THEN dt.typname
Expand Down Expand Up @@ -172,6 +188,8 @@ LEFT JOIN pg_attribute a ON a.attrelid = cl.oid AND a.attname = c.column_name
LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
LEFT JOIN pg_type dt ON dt.oid = a.atttypid
LEFT JOIN pg_namespace dn ON dt.typnamespace = dn.oid
LEFT JOIN pg_type et ON dt.typelem = et.oid
LEFT JOIN pg_namespace en ON et.typnamespace = en.oid
WHERE
c.table_schema = $1
ORDER BY c.table_name, c.ordinal_position;
Expand Down
22 changes: 20 additions & 2 deletions ir/queries/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions testdata/diff/create_table/add_column_array/diff.sql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ALTER TABLE articles ADD COLUMN tags text[];

ALTER TABLE articles ADD COLUMN statuses status[];
10 changes: 9 additions & 1 deletion testdata/diff/create_table/add_column_array/new.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
CREATE TYPE status AS ENUM (
'pending',
'active',
'inactive',
'archived'
);

CREATE TABLE public.articles (
id integer NOT NULL,
title text,
tags text[]
tags text[],
statuses status[]
);
7 changes: 7 additions & 0 deletions testdata/diff/create_table/add_column_array/old.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
CREATE TYPE status AS ENUM (
'pending',
'active',
'inactive',
'archived'
);

CREATE TABLE public.articles (
id integer NOT NULL,
title text
Expand Down
10 changes: 8 additions & 2 deletions testdata/diff/create_table/add_column_array/plan.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"version": "1.0.0",
"pgschema_version": "1.4.0",
"pgschema_version": "1.4.1",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "e7d583c1ac9a0f1695b40f23fa0dd1366500b02eaf9c96a5ff5cd65813f48130"
"hash": "35e3acc4a8a360a2e7264d0a04f59eba5538bdda085a20fab68e0586f07ae199"
},
"groups": [
{
Expand All @@ -13,6 +13,12 @@
"type": "table.column",
"operation": "create",
"path": "public.articles.tags"
},
{
"sql": "ALTER TABLE articles ADD COLUMN statuses status[];",
"type": "table.column",
"operation": "create",
"path": "public.articles.statuses"
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions testdata/diff/create_table/add_column_array/plan.sql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ALTER TABLE articles ADD COLUMN tags text[];

ALTER TABLE articles ADD COLUMN statuses status[];
3 changes: 3 additions & 0 deletions testdata/diff/create_table/add_column_array/plan.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ Summary by type:

Tables:
~ articles
+ statuses (column)
+ tags (column)

DDL to be executed:
--------------------------------------------------

ALTER TABLE articles ADD COLUMN tags text[];

ALTER TABLE articles ADD COLUMN statuses status[];
1 change: 0 additions & 1 deletion testdata/diff/create_table/enum_array_column/diff.sql

This file was deleted.

12 changes: 0 additions & 12 deletions testdata/diff/create_table/enum_array_column/new.sql

This file was deleted.

11 changes: 0 additions & 11 deletions testdata/diff/create_table/enum_array_column/old.sql

This file was deleted.

20 changes: 0 additions & 20 deletions testdata/diff/create_table/enum_array_column/plan.json

This file was deleted.

1 change: 0 additions & 1 deletion testdata/diff/create_table/enum_array_column/plan.sql

This file was deleted.

13 changes: 0 additions & 13 deletions testdata/diff/create_table/enum_array_column/plan.txt

This file was deleted.

Loading