Skip to content
Open
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
18 changes: 13 additions & 5 deletions src/lib/sql/columns.sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,19 @@ FROM
SELECT DISTINCT ON (table_id, ordinal_position)
conrelid AS table_id,
conkey[1] AS ordinal_position,
substring(
pg_get_constraintdef(pg_constraint.oid, true),
8,
length(pg_get_constraintdef(pg_constraint.oid, true)) - 8
) AS "definition"
-- Prefer pg_get_expr over stripping CHECK (...) from pg_get_constraintdef:
-- suffixes like NOT VALID / NO INHERIT make the old length-8 heuristic corrupt
-- the expression (e.g. "id > 0) NOT VALI"). Strip one outer paren pair so the
-- API shape stays "c <> 0" rather than "(c <> 0)".
CASE
WHEN pg_get_expr(conbin, conrelid) LIKE '(%)' THEN
substring(
pg_get_expr(conbin, conrelid),
2,
length(pg_get_expr(conbin, conrelid)) - 2
)
ELSE pg_get_expr(conbin, conrelid)
END AS "definition"
FROM pg_constraint
WHERE contype = 'c' AND cardinality(conkey) = 1
ORDER BY table_id, ordinal_position, oid asc
Expand Down
35 changes: 35 additions & 0 deletions test/lib/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,41 @@ test('dropping column checks', async () => {
await pgMeta.query(`drop table t`)
})

test('column check with NOT VALID suffix', async () => {
await pgMeta.query(`
create table public.t (id int8);
alter table public.t add constraint t_id_check check (id > 0) not valid;
`)

const res = await pgMeta.columns.retrieve({
schema: 'public',
table: 't',
name: 'id',
})
expect(res.error).toBeNull()
// Must be the expression only — not `id > 0) NOT VALI` from the old substring heuristic
expect(res.data?.check).toBe('id > 0')

await pgMeta.query(`drop table public.t`)
})

test('column check with NO INHERIT suffix', async () => {
await pgMeta.query(`
create table public.t (id int8);
alter table public.t add constraint t_id_check check (id > 0) no inherit;
`)

const res = await pgMeta.columns.retrieve({
schema: 'public',
table: 't',
name: 'id',
})
expect(res.error).toBeNull()
expect(res.data?.check).toBe('id > 0')

await pgMeta.query(`drop table public.t`)
})

test('column with fully-qualified type', async () => {
await pgMeta.query(`create table public.t(); create schema s; create type s.my_type as enum ();`)

Expand Down