Skip to content

Commit 7641d4c

Browse files
committed
feat: check constraints
1 parent 0548e79 commit 7641d4c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/lib/PostgresMetaColumns.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ COMMIT;`
213213
is_nullable,
214214
is_unique,
215215
comment,
216+
check,
216217
}: {
217218
name?: string
218219
type?: string
@@ -224,6 +225,7 @@ COMMIT;`
224225
is_nullable?: boolean
225226
is_unique?: boolean
226227
comment?: string
228+
check?: string
227229
}
228230
): Promise<PostgresMetaResult<PostgresColumn>> {
229231
const { data: old, error } = await this.retrieve({ id })
@@ -328,6 +330,34 @@ $$;
328330
old!.name
329331
)} IS ${literal(comment)};`
330332

333+
const checkSql =
334+
check === undefined
335+
? ''
336+
: `
337+
DO $$
338+
DECLARE
339+
v_conname name;
340+
BEGIN
341+
SELECT conname into v_conname FROM pg_constraint WHERE
342+
contype = 'c'
343+
AND cardinality(conkey) = 1
344+
AND conrelid = ${literal(old!.table_id)}
345+
AND conkey[1] = ${literal(old!.ordinal_position)}
346+
LIMIT 1;
347+
348+
IF v_conname IS NOT NULL THEN
349+
EXECUTE format('ALTER TABLE ${ident(old!.schema)}.${ident(
350+
old!.table
351+
)} DROP CONSTRAINT %s', v_conname);
352+
END IF;
353+
354+
ALTER TABLE ${ident(old!.schema)}.${ident(old!.table)} ADD CONSTRAINT ${ident(
355+
old!.table
356+
)}_${ident(old!.name)}_check CHECK (${check});
357+
END
358+
$$;
359+
`
360+
331361
// TODO: Can't set default if column is previously identity even if
332362
// is_identity: false. Must do two separate PATCHes (once to drop identity
333363
// and another to set default).
@@ -341,6 +371,7 @@ BEGIN;
341371
${identitySql}
342372
${isUniqueSql}
343373
${commentSql}
374+
${checkSql}
344375
${nameSql}
345376
COMMIT;`
346377
{

src/lib/sql/columns.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ SELECT
4343
OR c.relkind IN ('v', 'f') AND pg_column_is_updatable(c.oid, a.attnum, FALSE)
4444
) AS is_updatable,
4545
uniques.table_id IS NOT NULL AS is_unique,
46+
check_constraints.definition AS "check",
4647
array_to_json(
4748
array(
4849
SELECT
@@ -81,6 +82,18 @@ FROM
8182
FROM pg_catalog.pg_constraint
8283
WHERE contype = 'u' AND cardinality(conkey) = 1
8384
) AS uniques ON uniques.table_id = c.oid AND uniques.ordinal_position = a.attnum
85+
LEFT JOIN (
86+
SELECT
87+
conrelid AS table_id,
88+
conkey[1] AS ordinal_position,
89+
substring(
90+
pg_get_constraintdef(pg_constraint.oid, true),
91+
8,
92+
length(pg_get_constraintdef(pg_constraint.oid, true)) - 8
93+
) AS "definition"
94+
FROM pg_constraint
95+
WHERE contype = 'c' AND cardinality(conkey) = 1
96+
) AS check_constraints ON check_constraints.table_id = c.oid AND check_constraints.ordinal_position = a.attnum
8497
WHERE
8598
NOT pg_is_other_temp_schema(nc.oid)
8699
AND a.attnum > 0

0 commit comments

Comments
 (0)