Skip to content

Commit db74779

Browse files
authored
List enum types when reading schema (#443)
Part of #376
1 parent b02c324 commit db74779

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

pkg/schema/schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ type Column struct {
7373

7474
// Optional comment for the column
7575
Comment string `json:"comment"`
76+
77+
// Will contain possible enum values if the type is an enum
78+
EnumValues []string `json:"enumValues"`
7679
}
7780

7881
// Index represents an index on a table

pkg/state/init.sql

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ BEGIN
8989
'oid', t.oid,
9090
'comment', descr.description,
9191
'columns', (SELECT COALESCE(json_object_agg(name, c), '{}'::json)
92-
FROM (SELECT attr.attname AS name,
93-
pg_get_expr(def.adbin, def.adrelid) AS default,
92+
FROM (SELECT attr.attname AS name,
93+
pg_get_expr(def.adbin, def.adrelid) AS default,
9494
NOT (
9595
attr.attnotnull
9696
OR tp.typtype = 'd'
9797
AND tp.typnotnull
98-
) AS nullable,
98+
) AS nullable,
9999
CASE
100100
WHEN 'character varying' :: regtype = ANY
101101
(ARRAY [attr.atttypid, tp.typelem]) THEN REPLACE(
@@ -110,18 +110,24 @@ BEGIN
110110
'timestamptz'
111111
)
112112
ELSE format_type(attr.atttypid, attr.atttypmod)
113-
END AS type,
114-
descr.description AS comment,
115-
(EXISTS (SELECT 1
116-
FROM pg_constraint
117-
WHERE conrelid = attr.attrelid
118-
AND ARRAY [attr.attnum::int] @> conkey::int[]
119-
AND contype = 'u') OR EXISTS (SELECT 1
120-
FROM pg_index
121-
JOIN pg_class ON pg_class.oid = pg_index.indexrelid
122-
WHERE indrelid = attr.attrelid
123-
AND indisunique
124-
AND ARRAY [attr.attnum::int] @> pg_index.indkey::int[])) AS unique
113+
END AS type,
114+
descr.description AS comment,
115+
(EXISTS
116+
117+
118+
(SELECT 1
119+
FROM pg_constraint
120+
WHERE conrelid = attr.attrelid
121+
AND ARRAY [attr.attnum::int] @> conkey::int[]
122+
AND contype = 'u') OR EXISTS (SELECT 1
123+
FROM pg_index
124+
JOIN pg_class ON pg_class.oid = pg_index.indexrelid
125+
WHERE indrelid = attr.attrelid
126+
AND indisunique
127+
AND ARRAY [attr.attnum::int] @> pg_index.indkey::int[])) AS unique,
128+
(SELECT array_agg(e.enumlabel ORDER BY e.enumsortorder)
129+
from pg_enum as e
130+
WHERE e.enumtypid = tp.oid) AS enumValues
125131
FROM pg_attribute AS attr
126132
INNER JOIN pg_type AS tp ON attr.atttypid = tp.oid
127133
LEFT JOIN pg_attrdef AS def ON attr.attrelid = def.adrelid

pkg/state/state_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,36 @@ func TestReadSchema(t *testing.T) {
816816
},
817817
},
818818
},
819+
{
820+
name: "custom enum types",
821+
createStmt: "CREATE TYPE review AS ENUM ('good', 'bad', 'ugly'); CREATE TABLE public.table1 (name text, review review);",
822+
wantSchema: &schema.Schema{
823+
Name: "public",
824+
Tables: map[string]schema.Table{
825+
"table1": {
826+
Name: "table1",
827+
Columns: map[string]schema.Column{
828+
"name": {
829+
Name: "name",
830+
Type: "text",
831+
Nullable: true,
832+
},
833+
"review": {
834+
Name: "review",
835+
Type: "public.review",
836+
Nullable: true,
837+
EnumValues: []string{"good", "bad", "ugly"},
838+
},
839+
},
840+
PrimaryKey: []string{},
841+
Indexes: map[string]schema.Index{},
842+
ForeignKeys: map[string]schema.ForeignKey{},
843+
CheckConstraints: map[string]schema.CheckConstraint{},
844+
UniqueConstraints: map[string]schema.UniqueConstraint{},
845+
},
846+
},
847+
},
848+
},
819849
}
820850

821851
for _, tt := range tests {

0 commit comments

Comments
 (0)