Skip to content

Commit 39d8158

Browse files
Fix encoding of domains over composite types (#771)
1 parent f79090d commit 39d8158

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

lib/postgrex/types.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ defmodule Postgrex.Types do
9393
ARRAY (
9494
SELECT a.atttypid
9595
FROM pg_attribute AS a
96-
WHERE a.attrelid = t.typrelid AND a.attnum > 0 AND NOT a.attisdropped
96+
WHERE a.attrelid = coalesce(d.typrelid, t.typrelid) AND a.attnum > 0 AND NOT a.attisdropped
9797
ORDER BY a.attnum
9898
)
9999
"""

test/query_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ defmodule QueryTest do
8484
assert [[^uuid]] = query("SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid", [])
8585
end
8686

87+
@tag min_pg_version: "11.0"
88+
test "decode composite domain", context do
89+
assert [[{"a", 1}]] = query("SELECT '(a, 1)'::composite_domain", [])
90+
end
91+
92+
@tag min_pg_version: "11.0"
93+
test "decode arrays of composite domain", context do
94+
assert [[[{"a", 1}]]] = query("SELECT ARRAY['(a, 1)']::composite_domain[]", [])
95+
end
96+
8797
test "decode arrays", context do
8898
assert [[[]]] = query("SELECT ARRAY[]::integer[]", [])
8999
assert [[[1]]] = query("SELECT ARRAY[1]", [])
@@ -121,6 +131,16 @@ defmodule QueryTest do
121131
assert [[^points_string]] = query("SELECT $1::points_domain::text", [points])
122132
end
123133

134+
@tag min_pg_version: "11.0"
135+
test "encode composite domain", context do
136+
assert [[{"a", 1}]] = query("SELECT $1::composite_domain", [{"a", 1}])
137+
end
138+
139+
@tag min_pg_version: "11.0"
140+
test "encode arrays of composite domain", context do
141+
assert [[[{"a", 1}]]] = query("SELECT $1::composite_domain[]", [[{"a", 1}]])
142+
end
143+
124144
test "decode interval", context do
125145
assert [[%Postgrex.Interval{months: 0, days: 0, secs: 0, microsecs: 0}]] =
126146
query("SELECT interval '0'", [])

test/test_helper.exs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,20 @@ replication_exclude =
7272
end
7373

7474
version_exclude =
75-
[{8, 4}, {9, 0}, {9, 1}, {9, 2}, {9, 3}, {9, 4}, {9, 5}, {10, 0}, {13, 0}, {14, 0}, {17, 0}]
75+
[
76+
{8, 4},
77+
{9, 0},
78+
{9, 1},
79+
{9, 2},
80+
{9, 3},
81+
{9, 4},
82+
{9, 5},
83+
{10, 0},
84+
{11, 0},
85+
{13, 0},
86+
{14, 0},
87+
{17, 0}
88+
]
7689
|> Enum.filter(fn x -> x > pg_version end)
7790
|> Enum.map(fn {major, minor} -> {:min_pg_version, "#{major}.#{minor}"} end)
7891

@@ -127,6 +140,20 @@ sql_test =
127140
sql_test
128141
end
129142

143+
sql_test =
144+
if pg_version >= {11, 0} do
145+
sql_test <>
146+
"""
147+
DROP TYPE IF EXISTS composite_test;
148+
CREATE TYPE composite_test AS (a text, b integer);
149+
150+
DROP DOMAIN IF EXISTS composite_domain;
151+
CREATE DOMAIN composite_domain AS composite_test CHECK ((VALUE).a is NOT NULL);
152+
"""
153+
else
154+
sql_test
155+
end
156+
130157
sql_test_with_schemas = """
131158
DROP SCHEMA IF EXISTS test;
132159
CREATE SCHEMA test;

0 commit comments

Comments
 (0)