Skip to content

Commit 634f13e

Browse files
committed
fix: remove oid-to-name conversions
Turns out that oid :: reg* :: text leaves behind some quotes: https://stackoverflow.com/questions/55981426/how-to-convert-regclass-to-string-without-quotes We don't want this, especially when doing filtering or showing it to the frontend, so we should use `name` columns from the corresponding system catalogs instead.
1 parent ec35d0f commit 634f13e

File tree

8 files changed

+31
-18
lines changed

8 files changed

+31
-18
lines changed

src/api/tables.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ const getTablesSql = (sqlTemplates) => {
150150
FROM
151151
relationships
152152
WHERE
153-
(relationships.source_schema :: text = tables.schema AND relationships.source_table_name :: text = tables.name)
154-
OR (relationships.target_table_schema :: text = tables.schema AND relationships.target_table_name :: text = tables.name)`
153+
(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
154+
OR (relationships.target_table_schema = tables.schema AND relationships.target_table_name = tables.name)`
155155
)}
156156
FROM tables;`.trim()
157157
}
@@ -183,8 +183,8 @@ const selectSingleSql = (sqlTemplates: { [key: string]: string }, id: number) =>
183183
FROM
184184
relationships
185185
WHERE
186-
(relationships.source_schema :: text = tables.schema AND relationships.source_table_name :: text = tables.name)
187-
OR (relationships.target_table_schema :: text = tables.schema AND relationships.target_table_name :: text = tables.name)`
186+
(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
187+
OR (relationships.target_table_schema = tables.schema AND relationships.target_table_name = tables.name)`
188188
)}
189189
FROM tables;`.trim()
190190
}
@@ -223,8 +223,8 @@ const selectSingleByName = (
223223
FROM
224224
relationships
225225
WHERE
226-
(relationships.source_schema :: text = tables.schema AND relationships.source_table_name :: text = tables.name)
227-
OR (relationships.target_table_schema :: text = tables.schema AND relationships.target_table_name :: text = tables.name)`
226+
(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
227+
OR (relationships.target_table_schema = tables.schema AND relationships.target_table_name = tables.name)`
228228
)}
229229
FROM tables;`.trim()
230230
}

src/lib/sql/columns.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ SELECT
5959
FROM
6060
pg_catalog.pg_enum enums
6161
WHERE
62-
COALESCE(bt.typname, t.typname) = format_type(enums.enumtypid :: regclass, NULL)
62+
COALESCE(bt.typname, t.typname) = format_type(enums.enumtypid, NULL)
6363
ORDER BY
6464
enums.enumsortorder
6565
)

src/lib/sql/extensions.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
SELECT
22
e.name,
3-
x.extnamespace :: regnamespace AS schema,
3+
n.nspname AS schema,
44
e.default_version,
55
x.extversion AS installed_version,
66
e.comment
77
FROM
88
pg_available_extensions() e(name, default_version, comment)
99
LEFT JOIN pg_extension x ON e.name = x.extname
10+
LEFT JOIN pg_namespace n ON x.extnamespace = n.oid

src/lib/sql/primary_keys.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
SELECT
22
n.nspname AS schema,
3-
c.oid :: regclass AS table_name,
3+
c.relname AS table_name,
44
a.attname AS name,
55
c.oid :: int8 AS table_id
66
FROM

src/lib/sql/publications.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SELECT
22
p.oid :: int8 AS id,
33
p.pubname AS name,
4-
p.pubowner :: regrole AS owner,
4+
r.rolname AS owner,
55
p.pubinsert AS publish_insert,
66
p.pubupdate AS publish_update,
77
p.pubdelete AS publish_delete,
@@ -15,14 +15,16 @@ FROM
1515
LEFT JOIN LATERAL (
1616
SELECT
1717
COALESCE(
18-
array_agg(pr.prrelid :: regclass :: text) filter (
18+
array_agg(c.relname :: text) filter (
1919
WHERE
20-
pr.prrelid IS NOT NULL
20+
c.relname IS NOT NULL
2121
),
2222
'{}'
2323
) AS tables
2424
FROM
2525
pg_catalog.pg_publication_rel AS pr
26+
JOIN pg_class AS c ON pr.prrelid = c.oid
2627
WHERE
2728
pr.prpubid = p.oid
2829
) AS pr ON 1 = 1
30+
JOIN pg_roles AS r ON p.pubowner = r.oid

src/lib/sql/relationships.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
SELECT
22
c.oid :: int8 AS id,
33
c.conname AS constraint_name,
4-
csa.relnamespace :: regnamespace AS source_schema,
5-
c.conrelid :: regclass AS source_table_name,
4+
nsa.nspname AS source_schema,
5+
csa.relname AS source_table_name,
66
sa.attname AS source_column_name,
7-
cta.relnamespace :: regnamespace AS target_table_schema,
8-
c.confrelid :: regclass AS target_table_name,
7+
nta.nspname AS target_table_schema,
8+
cta.relname AS target_table_name,
99
ta.attname AS target_column_name
1010
FROM
1111
pg_constraint c
1212
JOIN (
1313
pg_attribute sa
1414
JOIN pg_class csa ON sa.attrelid = csa.oid
15+
JOIN pg_namespace nsa ON csa.relnamespace = nsa.oid
1516
) ON sa.attrelid = c.conrelid
1617
AND sa.attnum = ANY (c.conkey)
1718
JOIN (
1819
pg_attribute ta
1920
JOIN pg_class cta ON ta.attrelid = cta.oid
21+
JOIN pg_namespace nta ON cta.relnamespace = nta.oid
2022
) ON ta.attrelid = c.confrelid
2123
AND ta.attnum = ANY (c.confkey)
2224
WHERE

test/integration/index.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ describe('/tables', async () => {
219219
assert.equal(true, relationship.target_table_schema === 'public')
220220
assert.equal(true, relationship.target_table_name === 'users')
221221
})
222+
it('/tables should return relationships for quoted names', async () => {
223+
const tables = await axios.get(`${URL}/tables`)
224+
const todos = tables.data.find((x) => `${x.schema}.${x.name}` === 'public.todos')
225+
const relationship = todos.relationships.find(
226+
(x) => x.source_schema === 'public' && x.source_table_name === 'todos'
227+
)
228+
assert.equal(true, relationship.source_column_name === 'user-id')
229+
})
222230
it('GET /tables with system tables', async () => {
223231
const res = await axios.get(`${URL}/tables?include_system_schemas=true`)
224232
const included = res.data.find((x) => `${x.schema}.${x.name}` === 'pg_catalog.pg_type')

test/postgres/mnt/00-init.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ VALUES
1717
CREATE TABLE public.todos (
1818
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
1919
details text,
20-
user_id bigint REFERENCES users NOT NULL
20+
"user-id" bigint REFERENCES users NOT NULL
2121
);
2222

2323
INSERT INTO
24-
public.todos (details, user_id)
24+
public.todos (details, "user-id")
2525
VALUES
2626
('Star the repo', 1),
2727
('Watch the releases', 2);

0 commit comments

Comments
 (0)