Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting column check constraints #472

Merged
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
15 changes: 15 additions & 0 deletions src/deprecatia/integration-tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
{
"tables": [
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -277,6 +278,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -821,6 +823,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -1025,6 +1028,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -1313,6 +1317,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -1517,6 +1522,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -2199,6 +2205,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -3073,6 +3080,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -3316,6 +3324,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -3555,6 +3564,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -3848,6 +3858,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -4052,6 +4063,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -4508,6 +4520,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -5032,6 +5045,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down Expand Up @@ -5763,6 +5777,7 @@ exports[`extractSchema > dvd-rental database > Should match snapshot 1`] = `
"tags": {},
},
{
"checks": [],
"columns": [
{
"comment": undefined,
Expand Down
24 changes: 24 additions & 0 deletions src/kinds/extractTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, expect } from "vitest";
import { test, testWith } from "../tests/useSchema";
import extractTable, {
ColumnReference,
TableCheck,
TableColumn,
TableDetails,
} from "./extractTable";
Expand Down Expand Up @@ -44,6 +45,7 @@ describe("extractTable", () => {
is_typed: "NO",
commit_action: null,
},
checks: [],
columns: [
{
name: "id",
Expand Down Expand Up @@ -160,6 +162,28 @@ describe("extractTable", () => {
expect(actual).toEqual(expected);
});

test("it should fetch table checks", async ({ knex: [db] }) => {
await db.raw(`create table test.some_table_with_checks (
id integer constraint id_check check (id > 0),
products TEXT[],
number_of_products INT,
constraint products_len_check check (array_length(products, 1) = number_of_products)
)`);

const result = await extractTable(db, makePgType("some_table_with_checks"));
const actual = result.checks;

const expected: TableCheck[] = [
{ name: "id_check", clause: "id > 0" },
{
name: "products_len_check",
clause: "array_length(products, 1) = number_of_products",
},
];

expect(actual).toEqual(expected);
});

test("it should handle domains, composite types, ranges and enums as well as arrays of those", async ({
knex: [db],
}) => {
Expand Down
46 changes: 45 additions & 1 deletion src/kinds/extractTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export type TableColumnType = {
kind: "base" | "range" | "domain" | "composite" | "enum";
};

export interface TableCheck {
name: string;
clause: string;
}

export interface TableColumn {
name: string;
expandedType: string;
Expand All @@ -61,6 +66,7 @@ export interface TableColumn {

export interface TableDetails extends PgType<"table"> {
columns: TableColumn[];
checks: TableCheck[];
informationSchemaValue: InformationSchemaTable;
}

Expand Down Expand Up @@ -215,7 +221,45 @@ const extractTable = async (
reference: row.references[0] ?? null,
}));

return { ...table, informationSchemaValue, columns };
const checkQuery = await db.raw(
`
SELECT
source_namespace.nspname as "schema",
source_class.relname as "table",
json_agg(json_build_object(
'name', con.conname,
'clause', SUBSTRING(pg_get_constraintdef(con.oid) FROM 7)
)) as checks
FROM
pg_constraint con,
pg_class source_class,
pg_namespace source_namespace
WHERE
source_class.relname = :table_name
AND source_namespace.nspname = :schema_name
AND conrelid = source_class.oid
AND source_class.relnamespace = source_namespace.oid
AND con.contype = 'c'
GROUP BY source_namespace.nspname, source_class.relname;
`,
{ table_name: table.name, schema_name: table.schemaName },
);

const checks = checkQuery.rows
.flatMap((row: any) => row.checks as TableCheck)
.map(({ name, clause }: TableCheck) => {
const numberOfBrackets =
clause.startsWith("((") && clause.endsWith("))") ? 2 : 1;
return {
name,
clause: clause.slice(
numberOfBrackets,
clause.length - numberOfBrackets,
),
};
});

return { ...table, checks, informationSchemaValue, columns };
};

export default extractTable;