diff --git a/server/src/ai/plan-normalize.ts b/server/src/ai/plan-normalize.ts index 543bcfd..4de503b 100644 --- a/server/src/ai/plan-normalize.ts +++ b/server/src/ai/plan-normalize.ts @@ -79,6 +79,12 @@ const normalizeOperation = (value: unknown): unknown => { } case "drop_index": return { type, tableName, indexName: operation.indexName ?? operation.name }; + case "add_unique": + return { type, tableName, constraintName: operation.constraintName ?? operation.name, columns: operation.columns ?? (operation.column !== undefined ? [operation.column] : undefined) }; + case "add_check": + return { type, tableName, constraintName: operation.constraintName ?? operation.name, expression: operation.expression ?? operation.condition ?? operation.check }; + case "drop_constraint": + return { type, tableName, constraintName: operation.constraintName ?? operation.name }; case "add_relationship": return { type, diff --git a/server/src/ai/schema-plan.ts b/server/src/ai/schema-plan.ts index 2e9ef1b..13bda9e 100644 --- a/server/src/ai/schema-plan.ts +++ b/server/src/ai/schema-plan.ts @@ -94,6 +94,9 @@ function makeOperationSchema(changes: Changes) { }).strict(), z.object({ type: z.literal("add_index"), tableName: identifier, index: aiIndexSchema }).strict(), z.object({ type: z.literal("drop_index"), tableName: identifier, indexName: identifier }).strict(), + z.object({ type: z.literal("add_unique"), tableName: identifier, constraintName: identifier.nullable().optional(), columns: z.array(identifier).min(1).max(32) }).strict(), + z.object({ type: z.literal("add_check"), tableName: identifier, constraintName: identifier.nullable().optional(), expression: z.string().trim().min(1).max(400) }).strict(), + z.object({ type: z.literal("drop_constraint"), tableName: identifier, constraintName: identifier }).strict(), ]); } diff --git a/server/src/ai/structured.test.ts b/server/src/ai/structured.test.ts index 3d2a467..ad9d46a 100644 --- a/server/src/ai/structured.test.ts +++ b/server/src/ai/structured.test.ts @@ -94,7 +94,7 @@ describe("the schemas the model is given", () => { expect(properties).toBeLessThan(500); expect(JSON.stringify(schema).length).toBeLessThan(60_000); const operations = schema.properties.operations.items.anyOf as Json[]; - expect(operations.map((operation) => operation.properties.type.const).sort()).toEqual(["add_column", "add_index", "add_relationship", "alter_column", "create_table", "drop_column", "drop_index", "drop_relationship", "drop_table", "rename_table"]); + expect(operations.map((operation) => operation.properties.type.const).sort()).toEqual(["add_check", "add_column", "add_index", "add_relationship", "add_unique", "alter_column", "create_table", "drop_column", "drop_constraint", "drop_index", "drop_relationship", "drop_table", "rename_table"]); }); it("the model's plan schema has the same top-level keys as the plan validator", () => { diff --git a/server/src/schema-change/compile.ts b/server/src/schema-change/compile.ts index 24eca88..1b7329e 100644 --- a/server/src/schema-change/compile.ts +++ b/server/src/schema-change/compile.ts @@ -2,6 +2,7 @@ import type { AiSchemaPlan } from "../ai/schema-plan.js"; import { keyedBackfill, pagedBackfill } from "./backfill.js"; import { castKey, + checkKey, defaultKey, emptyProbes, qualifiedName, @@ -76,7 +77,7 @@ class Compiler { private readonly uncompiled: number[] = []; private tables: Map; private readonly settings: CompileSettings; - private readonly needed: ProbeRequest = { types: [], typeChanges: [], functions: [], defaults: [], relations: [], schemas: [] }; + private readonly needed: ProbeRequest = { types: [], typeChanges: [], functions: [], defaults: [], checks: [], relations: [], schemas: [] }; private current = -1; /** Names this plan has claimed so far, so two operations can't both create `orders_status_idx`. */ private readonly claimedRelations = new Set(); @@ -295,6 +296,9 @@ class Compiler { case "drop_relationship": return this.dropRelationship(operation); case "add_index": return this.addIndex(operation); case "drop_index": return this.dropIndex(operation); + case "add_unique": return this.addUnique(operation); + case "add_check": return this.addCheck(operation); + case "drop_constraint": return this.dropConstraint(operation); default: this.fail("unsupported-operation", `Operation ${index + 1} has a type this compiler does not know.`); } @@ -623,9 +627,9 @@ class Compiler { } /** UNIQUE on columns of an existing table: the index is built without blocking writes, then attached as a constraint. */ - private uniqueSteps(table: TableFacts, columns: string[]): void { + private uniqueSteps(table: TableFacts, columns: string[], given?: string): void { const target = qualifiedIdent(table.schema, table.name); - const name = limitName(`${table.name}_${columns.join("_")}_key`, this.names(table)); + const name = given ?? limitName(`${table.name}_${columns.join("_")}_key`, this.names(table)); this.claimRelation(table.schema, name, "Index"); const list = columns.map(ident).join(", "); const size = sizeClass(table); @@ -1157,6 +1161,109 @@ class Compiler { }); table.indexes = table.indexes.filter((candidate) => candidate !== index); } + + // ---- constraints ------------------------------------------------------------------------------------------------------ + + private addUnique(operation: Extract): void { + const table = this.findTable(operation.tableName); + const columns = operation.columns.map((name) => this.findColumn(table, name).name); + if (new Set(columns.map(lower)).size !== columns.length) this.fail("duplicate-column", `The unique constraint on ${table.qualified} names a column twice.`, table.qualified); + const same = (names: readonly (string | null)[]) => names.length === columns.length && names.every((name, i) => name !== null && lower(name) === lower(columns[i])); + if (same(table.primaryKey) || table.uniques.some((unique) => unique.kind === "unique" && same(unique.columns))) { + this.note("info", "no-change", `${table.qualified} (${columns.join(", ")}) is already unique.`, table.qualified); + return; + } + // the index that enforces it carries the constraint's name, so uniqueSteps claims the name + this.uniqueSteps(table, columns, operation.constraintName ?? undefined); + } + + private addCheck(operation: Extract): void { + const table = this.findTable(operation.tableName); + const expression = operation.expression.trim(); + const label = `${table.qualified}`; + const problem = checkDefaultExpression(expression); + if (problem) this.fail("bad-check", `CHECK on ${label}: ${problem.replace(/^The default/, "The expression")}`, table.qualified); + const functions = functionsIn(expression); + const target = qualifiedIdent(table.schema, table.name); + if (this.discovery) { + this.needed.functions.push(...functions); + this.needed.checks.push({ table: target, expression }); + return; + } + const rejected = this.probes.checks.get(checkKey(target, expression)); + if (!this.probes.checks.has(checkKey(target, expression))) this.fail("check-unverified", `The CHECK on ${label} was not tried against the database.`, table.qualified); + if (rejected) this.fail("bad-check", `PostgreSQL rejects the CHECK (${expression}) on ${label}: ${rejected}`, table.qualified); + const unknown = functions.filter((fn) => !this.probes.volatility.get(fn)); + if (unknown.length > 0) this.fail("bad-check", `The CHECK on ${label} calls ${unknown.join(", ")}, which PostgreSQL does not have.`, table.qualified); + const notImmutable = functions.filter((fn) => this.probes.volatility.get(fn) !== "i"); + if (notImmutable.length > 0) this.fail("bad-check", `The CHECK on ${label} calls ${notImmutable.join(", ")}, which is not immutable. A constraint has to give the same answer for the same row every time.`, table.qualified); + + const name = operation.constraintName ?? limitName(`${table.name}_check`, this.names(table)); + this.checkNameLength(name, "Constraint"); + if (this.names(table).has(name)) this.fail("name-taken", `${label} already has a constraint called ${name}.`, table.qualified); + // the columns the expression uses, outside string literals: what a later column drop or type change has to know about + const bare = expression.replace(/'(?:[^']|'')*'/g, " "); + const used = table.columns.filter((column) => new RegExp(`(^|[^A-Za-z0-9_$"])"?${column.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}"?($|[^A-Za-z0-9_$"])`, "i").test(bare)).map((column) => column.name); + + if (sizeClass(table) === "new" || !this.hasRows(table)) { + this.add({ title: `Add the check ${name} to ${label}`, sql: `ALTER TABLE ${target} ADD CONSTRAINT ${ident(name)} CHECK (${expression});`, mode: "short", lock: "ACCESS EXCLUSIVE", work: this.hasRows(table) ? "scan" : "metadata", table, notes: ["The table is empty, so there is nothing to check."], undo: `ALTER TABLE ${target} DROP CONSTRAINT ${ident(name)};` }); + } else { + this.add({ + title: `Add the check ${name} to ${label} as NOT VALID`, + sql: `ALTER TABLE ${target} ADD CONSTRAINT ${ident(name)} CHECK (${expression}) NOT VALID;`, + mode: "short", + lock: "ACCESS EXCLUSIVE", + work: "metadata", + table, + notes: ["NOT VALID enforces the rule for new and changed rows without reading the existing ones."], + undo: `ALTER TABLE ${target} DROP CONSTRAINT ${ident(name)};`, + }); + this.add({ + title: `Validate the check ${name} on ${label}`, + sql: `ALTER TABLE ${target} VALIDATE CONSTRAINT ${ident(name)};`, + mode: "long", + lock: "SHARE UPDATE EXCLUSIVE", + work: "scan", + table, + notes: ["Reads the whole table but does not block reads or writes.", `If it fails, rows break the rule. Find them with: SELECT * FROM ${target} WHERE NOT (${expression}) LIMIT 10; fix them, then run this step again.`], + }); + } + table.checks.push({ name, kind: "check", definition: `CHECK ((${expression}))`, columns: used, validated: true }); + } + + private dropConstraint(operation: Extract): void { + const table = this.findTable(operation.tableName); + const name = operation.constraintName; + const target = qualifiedIdent(table.schema, table.name); + if (table.indexes.some((index) => index.primary && index.constraint === name)) this.fail("primary-key-constraint", `${name} is the primary key of ${table.qualified}; dropping it is not compiled.`, table.qualified); + if (table.foreignKeys.some((fk) => fk.name === name)) this.fail("foreign-key-constraint", `${name} is a foreign key of ${table.qualified}: use drop_relationship.`, table.qualified); + const check = table.checks.find((candidate) => candidate.name === name); + const unique = table.uniques.find((candidate) => candidate.name === name); + const found = check ?? unique; + if (!found) this.fail("unknown-constraint", `${table.qualified} has no unique or check constraint called ${name}.`, table.qualified); + + if (unique) { + const inbound = table.referencedBy.filter((fk) => fk.refColumns.length === unique.columns.length && fk.refColumns.every((column, i) => lower(column) === lower(unique.columns[i]))); + if (inbound.length > 0) this.fail("referenced", `${name} is the unique key that ${inbound.map((fk) => `${fk.schema}.${fk.table} (${fk.name})`).join(", ")} points at. Drop those foreign keys first.`, table.qualified); + } + const index = unique ? table.indexes.find((candidate) => candidate.constraint === name) : undefined; + this.add({ + title: `Drop the ${check ? "check" : "unique constraint"} ${name} on ${table.qualified}`, + sql: `ALTER TABLE ${target} DROP CONSTRAINT ${ident(name)};`, + mode: "short", + lock: "ACCESS EXCLUSIVE", + work: "metadata", + table, + notes: [ + check ? "Rows that break the rule can be written as soon as this runs." : "Its index is dropped with it, and duplicates can be written as soon as this runs.", + `To put it back: ALTER TABLE ${target} ADD CONSTRAINT ${ident(name)} ${found.definition}; (this reads the whole table under a lock; for a large table use add_${check ? "check" : "unique"} instead, which does it online).`, + ...(index && (index.scans ?? 0) > 0 ? [`Its index has been scanned ${formatCount(index.scans ?? 0)} times since the statistics were reset, so queries use it.`] : []), + ], + }); + table.checks = table.checks.filter((candidate) => candidate !== check); + table.uniques = table.uniques.filter((candidate) => candidate !== unique); + if (index) table.indexes = table.indexes.filter((candidate) => candidate !== index); + } } const baseOf = (type: string): string => parseType(type).base; diff --git a/server/src/schema-change/constraints.test.ts b/server/src/schema-change/constraints.test.ts new file mode 100644 index 0000000..7d11302 --- /dev/null +++ b/server/src/schema-change/constraints.test.ts @@ -0,0 +1,125 @@ +import { describe, expect, it } from "vitest"; +import { compile, facts, MIB, plan, table } from "./test-fixtures.js"; +import type { Runbook } from "./runbook.js"; + +const orders = (overrides: Partial[0]> = {}) => + table({ + name: "orders", rows: 5_000_000, bytes: 400 * MIB, + columns: ["id:bigint:notnull", "customer_id:bigint:notnull", "email:text", "qty:integer:notnull", "total:numeric(12,2)"], + ...overrides, + }); +const errors = (runbook: Runbook) => runbook.findings.filter((finding) => finding.severity === "error").map((finding) => finding.code); +const sql = (runbook: Runbook) => runbook.steps.map((step) => step.sql).join("\n"); + +describe("add_unique", () => { + it("builds the unique index without blocking writes, then attaches it as the constraint", () => { + const runbook = compile(plan({ type: "add_unique", tableName: "orders", columns: ["email"] }), facts(orders())); + expect(errors(runbook)).toEqual([]); + expect(runbook.steps.map((step) => step.title)).toEqual(["Build a unique index on public.orders(email)", "Attach the index as a unique constraint on public.orders(email)"]); + expect(sql(runbook)).toContain("CREATE UNIQUE INDEX CONCURRENTLY orders_email_key ON public.orders (email);"); + expect(sql(runbook)).toContain("ADD CONSTRAINT orders_email_key UNIQUE USING INDEX orders_email_key;"); + expect(runbook.expected.tables[0].uniques).toEqual([{ name: "orders_email_key", columns: ["email"] }]); + }); + + it("uses the name it is given, for the constraint and its index", () => { + const runbook = compile(plan({ type: "add_unique", tableName: "orders", constraintName: "orders_one_email", columns: ["customer_id", "email"] }), facts(orders())); + expect(errors(runbook)).toEqual([]); + expect(sql(runbook)).toContain("CREATE UNIQUE INDEX CONCURRENTLY orders_one_email ON public.orders (customer_id, email);"); + expect(sql(runbook)).toContain("ADD CONSTRAINT orders_one_email UNIQUE USING INDEX orders_one_email;"); + expect(compile(plan({ type: "add_unique", tableName: "orders", constraintName: "orders_pkey", columns: ["email"] }), facts(orders())).findings.map((finding) => finding.code)).toContain("name-taken"); + }); + + it("says so when the columns are already unique, and refuses unknown or repeated columns", () => { + expect(compile(plan({ type: "add_unique", tableName: "orders", columns: ["id"] }), facts(orders())).steps).toHaveLength(0); + expect(compile(plan({ type: "add_unique", tableName: "orders", columns: ["id"] }), facts(orders())).findings.map((finding) => finding.code)).toContain("no-change"); + expect(errors(compile(plan({ type: "add_unique", tableName: "orders", columns: ["nope"] }), facts(orders())))).toEqual(["unknown-column"]); + expect(errors(compile(plan({ type: "add_unique", tableName: "orders", columns: ["email", "EMAIL"] }), facts(orders())))).toEqual(["duplicate-column"]); + }); +}); + +describe("add_check", () => { + const add = (expression: string, extra: Record = {}, rows = 5_000_000) => compile(plan({ type: "add_check", tableName: "orders", expression, ...extra }), facts(orders({ rows, bytes: rows === 0 ? 0 : 400 * MIB }))); + + it("adds it NOT VALID, then validates without blocking writes", () => { + const runbook = add("qty > 0"); + expect(errors(runbook)).toEqual([]); + expect(runbook.steps.map((step) => [step.lock, step.work])).toEqual([["ACCESS EXCLUSIVE", "metadata"], ["SHARE UPDATE EXCLUSIVE", "scan"]]); + expect(sql(runbook)).toContain("ADD CONSTRAINT orders_check CHECK (qty > 0) NOT VALID;"); + expect(sql(runbook)).toContain("VALIDATE CONSTRAINT orders_check;"); + expect(runbook.expected.tables[0].checks).toEqual([{ name: "orders_check", validated: true }]); + }); + + it("is one step on an empty table, and takes the name it is given", () => { + const runbook = add("qty > 0", { constraintName: "orders_qty_positive" }, 0); + expect(runbook.steps).toHaveLength(1); + expect(sql(runbook)).toContain("ADD CONSTRAINT orders_qty_positive CHECK (qty > 0);"); + }); + + it("refuses what is not one plain boolean expression", () => { + for (const expression of ["qty > 0; DROP TABLE orders", "qty > 0 -- comment", "(SELECT 1) = 1", "qty > 0)) OR ((1=1"]) { + expect(errors(add(expression)), expression).toEqual(["bad-check"]); + } + }); + + it("asks the database, and reports what it says", () => { + const runbook = compile(plan({ type: "add_check", tableName: "orders", expression: "no_such > 0" }), facts(orders()), { rejectedChecks: { "no_such > 0": 'column "no_such" does not exist' } }); + expect(errors(runbook)).toEqual(["bad-check"]); + expect(runbook.findings.find((finding) => finding.code === "bad-check")?.message).toContain('column "no_such" does not exist'); + }); + + it("allows immutable functions only", () => { + expect(errors(add("lower(email) = email"))).toEqual([]); + expect(errors(add("random() < 2"))).toEqual(["bad-check"]); + expect(errors(add("shipped_at < now()"))).toEqual(["bad-check"]); + expect(add("shipped_at < now()").findings.find((finding) => finding.code === "bad-check")?.message).toContain("not immutable"); + expect(add("mystery(email)").findings.find((finding) => finding.code === "bad-check")?.message).toContain("which PostgreSQL does not have"); + }); + + it("is known to later steps: dropping a column it uses takes it along, and says so", () => { + const runbook = compile(plan({ type: "add_check", tableName: "orders", expression: "qty > 0 AND total >= 0" }, { type: "drop_column", tableName: "orders", columnName: "total" }), facts(orders())); + expect(errors(runbook)).toEqual([]); + const drop = runbook.steps.find((step) => step.title.startsWith("Drop column")); + expect(drop?.notes.join(" ")).toContain("CHECK constraint dropped with it: orders_check."); + expect(runbook.expected.tables[0].checks).toEqual([]); + }); +}); + +describe("drop_constraint", () => { + const withConstraints = () => + orders({ + uniques: [{ name: "orders_email_key", columns: ["email"] }], + indexes: [{ name: "orders_email_key", columns: ["email"], unique: true, constraint: "orders_email_key", scans: 7 }], + checks: [{ name: "orders_qty_positive", definition: "CHECK ((qty > 0))", columns: ["qty"] }], + foreignKeys: [{ name: "orders_customer_fk", columns: ["customer_id"], ref: "customers", refColumns: ["id"] }], + }); + const customers = () => table({ name: "customers", columns: ["id:bigint:notnull"] }); + const drop = (constraintName: string, ...others: ReturnType[]) => compile(plan({ type: "drop_constraint", tableName: "orders", constraintName }), facts(withConstraints(), customers(), ...others)); + + it("drops a check, and tells how to put it back", () => { + const runbook = drop("orders_qty_positive"); + expect(errors(runbook)).toEqual([]); + expect(sql(runbook)).toContain("ALTER TABLE public.orders DROP CONSTRAINT orders_qty_positive;"); + expect(runbook.steps[0].notes.join(" ")).toContain("ADD CONSTRAINT orders_qty_positive CHECK ((qty > 0));"); + expect(runbook.expected.tables.find((t) => t.qualified === "public.orders")?.checks).toEqual([]); + }); + + it("drops a unique constraint with its index, and says the index is in use", () => { + const runbook = drop("orders_email_key"); + expect(errors(runbook)).toEqual([]); + expect(runbook.steps[0].notes.join(" ")).toMatch(/scanned 7 times/); + const orders_ = runbook.expected.tables.find((t) => t.qualified === "public.orders")!; + expect(orders_.uniques).toEqual([]); + expect(orders_.indexes.map((index) => index.name)).not.toContain("orders_email_key"); + }); + + it("refuses a unique key that another table's foreign key points at", () => { + const child = table({ name: "emails", columns: ["id:bigint:notnull", "address:text"], foreignKeys: [{ name: "emails_fk", columns: ["address"], ref: "orders", refColumns: ["email"] }] }); + expect(errors(drop("orders_email_key", child))).toEqual(["referenced"]); + }); + + it("refuses the primary key, a foreign key, and a name that is not there", () => { + expect(errors(drop("orders_pkey"))).toEqual(["primary-key-constraint"]); + expect(errors(drop("orders_customer_fk"))).toEqual(["foreign-key-constraint"]); + expect(errors(drop("nothing_here"))).toEqual(["unknown-constraint"]); + }); +}); diff --git a/server/src/schema-change/facts.ts b/server/src/schema-change/facts.ts index 01dc442..c73242b 100644 --- a/server/src/schema-change/facts.ts +++ b/server/src/schema-change/facts.ts @@ -429,6 +429,8 @@ export interface ProbeRequest { functions: string[]; /** Default expressions with the type they are for: PostgreSQL is asked to plan `SELECT CAST(expression AS type)`. */ defaults: { expression: string; type: string }[]; + /** CHECK expressions with the (quoted, schema-qualified) table they are for: PostgreSQL is asked to plan `SELECT 1 FROM table WHERE (expression)`. */ + checks: { table: string; expression: string }[]; /** Quoted, schema-qualified names the plan wants to create: are they free? */ relations: { schema: string; name: string }[]; schemas: string[]; @@ -443,6 +445,8 @@ export interface Probes { volatility: Map; /** PostgreSQL's message when it rejects a default expression for a type; undefined when it accepts it. */ defaults: Map; + /** PostgreSQL's message when it rejects a CHECK expression on a table; undefined when it accepts it. */ + checks: Map; /** Whether a table, index or sequence already has this schema-qualified name. */ relationExists: Map; schemaExists: Map; @@ -450,9 +454,10 @@ export interface Probes { export const castKey = (from: string, to: string): string => `${from}\u0000${to}`; export const defaultKey = (expression: string, type: string): string => `${type}\u0000${expression}`; +export const checkKey = (table: string, expression: string): string => `${table}\u0000${expression}`; export const relationKey = (schema: string, name: string): string => `${schema}.${name}`; -export const emptyProbes = (): Probes => ({ types: new Map(), casts: new Map(), volatility: new Map(), defaults: new Map(), relationExists: new Map(), schemaExists: new Map() }); +export const emptyProbes = (): Probes => ({ types: new Map(), casts: new Map(), volatility: new Map(), defaults: new Map(), checks: new Map(), relationExists: new Map(), schemaExists: new Map() }); const quoteName = (name: string): string => `"${name.replace(/"/g, '""')}"`; @@ -522,5 +527,19 @@ export async function probe(client: Queryable, request: ProbeRequest): Promise

undefined); + probes.checks.set(key, error instanceof Error ? error.message : String(error)); + } + } return probes; } diff --git a/server/src/schema-change/prompt.ts b/server/src/schema-change/prompt.ts index c326255..cef38ab 100644 --- a/server/src/schema-change/prompt.ts +++ b/server/src/schema-change/prompt.ts @@ -8,11 +8,12 @@ export function plannerInstructions(): string { "You are GoForge's schema change planner for a large, live PostgreSQL database. You decide WHAT should change; a separate compiler turns your operations into online-safe SQL and checks them against the real catalog.", "Return JSON only. Do not use markdown fences or prose outside the JSON object.", "The JSON object must contain: summary (string), assumptions (string[]), warnings (string[]), clarifyingQuestions (string[]), operations (array).", - "Supported operation types: create_table, rename_table, drop_table, add_column, alter_column, drop_column, add_relationship, drop_relationship, add_index, drop_index.", + "Supported operation types: create_table, rename_table, drop_table, add_column, alter_column, drop_column, add_relationship, drop_relationship, add_index, drop_index, add_unique, add_check, drop_constraint.", "create_table.table contains name, optional note, fields, and optional indexes. A field contains name, dataType, and optional nullable, primaryKey, unique, autoIncrement, defaultValue, maxLength, precision, scale, note.", "add_column has tableName and field. alter_column has tableName, columnName and changes (any of newName, dataType, maxLength, precision, scale, nullable, unique, defaultValue, autoIncrement). drop_column has tableName and columnName.", "Every key of the answer's shape is always written: give null for any that does not apply (a field with no default has defaultValue null). To remove an existing column's default, set changes.dropDefault to true; to leave the default alone, leave it null.", "An index contains name, columns (existing column names, in order) and optional unique. add_index has tableName and index; drop_index has tableName and indexName.", + "add_unique has tableName, optional constraintName and columns (existing column names). add_check has tableName, optional constraintName and expression: ONE boolean SQL expression over the table's columns exactly as it follows CHECK, using only immutable functions and no subqueries. drop_constraint has tableName and constraintName of an existing unique or check constraint (use drop_relationship for a foreign key; the primary key is not dropped this way).", "add_relationship has relationship: sourceTable and sourceColumn are the REFERENCED (parent) side, targetTable and targetColumn are the column that HOLDS the foreign key (child side); optional name, onDelete, onUpdate (no_action, cascade, set_null, set_default, restrict).", "drop_relationship has sourceTable, sourceColumn, targetTable, targetColumn, with the same meaning of source and target.", "Table names are exactly as given in the facts, schema-qualified (schema.table). Use existing table, column and index names exactly. New object names may be anything valid.", diff --git a/server/src/schema-change/test-fixtures.ts b/server/src/schema-change/test-fixtures.ts index f33fe3d..7ec308e 100644 --- a/server/src/schema-change/test-fixtures.ts +++ b/server/src/schema-change/test-fixtures.ts @@ -1,6 +1,6 @@ import { aiSchemaPlanSchema, type AiSchemaPlan } from "../ai/schema-plan.js"; import { compilePlan, discoverProbes } from "./compile.js"; -import { castKey, defaultKey, emptyProbes, relationKey, type ColumnFacts, type Dependent, type Facts, type IndexFacts, type ProbeRequest, type Probes, type TableFacts } from "./facts.js"; +import { castKey, checkKey, defaultKey, emptyProbes, relationKey, type ColumnFacts, type Dependent, type Facts, type IndexFacts, type ProbeRequest, type Probes, type TableFacts } from "./facts.js"; import type { CompileSettings, Runbook } from "./runbook.js"; /** Hand-made facts for tests: a table is as big as the test says, with the columns and indexes it lists. */ @@ -102,12 +102,13 @@ const STABLE = new Set(["now", "current_timestamp", "current_date"]); const IMMUTABLE = new Set(["lower", "upper", "abs", "coalesce"]); /** What the database would say, for tests that don't have one: every type is known, defaults are accepted unless they name a function nobody has, and nothing exists under a new name. */ -export function answer(request: ProbeRequest, overrides: { unknownTypes?: string[]; existing?: string[]; missingSchemas?: string[]; rejectedDefaults?: Record } = {}): Probes { +export function answer(request: ProbeRequest, overrides: { unknownTypes?: string[]; existing?: string[]; missingSchemas?: string[]; rejectedDefaults?: Record; rejectedChecks?: Record } = {}): Probes { const probes = emptyProbes(); for (const type of [...request.types, ...request.typeChanges.flatMap((c) => [c.from, c.to])]) probes.types.set(type, overrides.unknownTypes?.includes(type) ? undefined : type); for (const change of request.typeChanges) probes.casts.set(castKey(change.from, change.to), CASTS[castKey(change.from, change.to)]); for (const name of request.functions) probes.volatility.set(name, VOLATILE.has(name) ? "v" : STABLE.has(name) ? "s" : IMMUTABLE.has(name) ? "i" : undefined); for (const { expression, type } of request.defaults) probes.defaults.set(defaultKey(expression, type), overrides.rejectedDefaults?.[expression]); + for (const { table, expression } of request.checks) probes.checks.set(checkKey(table, expression), overrides.rejectedChecks?.[expression]); for (const relation of request.relations) probes.relationExists.set(relationKey(relation.schema, relation.name), overrides.existing?.includes(relationKey(relation.schema, relation.name)) ?? false); for (const schema of request.schemas) probes.schemaExists.set(schema, !overrides.missingSchemas?.includes(schema)); return probes; diff --git a/server/src/schema-change/units.test.ts b/server/src/schema-change/units.test.ts index 1947a1d..57345b6 100644 --- a/server/src/schema-change/units.test.ts +++ b/server/src/schema-change/units.test.ts @@ -220,6 +220,7 @@ describe("facts", () => { typeChanges: [{ from: "integer", to: "bigint" }], functions: ["now", "x; y"], defaults: [], + checks: [], relations: [{ schema: "public", name: "ix" }], schemas: ["public"], }; @@ -244,8 +245,21 @@ describe("facts", () => { throw Object.assign(new Error("column \"active\" does not exist"), { code: "42703" }); }, }; - const rejected = await probe(rejecting, { types: [], typeChanges: [], functions: [], defaults: [{ expression: "active", type: "text" }], relations: [], schemas: [] }); + const rejected = await probe(rejecting, { types: [], typeChanges: [], functions: [], defaults: [{ expression: "active", type: "text" }], checks: [], relations: [], schemas: [] }); expect([...rejected.defaults.values()]).toEqual(['column "active" does not exist']); + + // a CHECK is planned against its table the same way, and a rejection is recorded, not thrown + const seenChecks: string[] = []; + const checking = { + async query(text: string) { + seenChecks.push(text); + if (text.startsWith("ROLLBACK")) return []; + throw Object.assign(new Error('column "nope" does not exist'), { code: "42703" }); + }, + }; + const checked = await probe(checking, { types: [], typeChanges: [], functions: [], defaults: [], checks: [{ table: "public.orders", expression: "nope > 0" }], relations: [], schemas: [] }); + expect([...checked.checks.values()]).toEqual(['column "nope" does not exist']); + expect(seenChecks.join("\n")).toContain("EXPLAIN SELECT 1 FROM public.orders WHERE (nope > 0)"); }); }); diff --git a/server/src/services/schema-plan-client.ts b/server/src/services/schema-plan-client.ts index b616b40..ea007b0 100644 --- a/server/src/services/schema-plan-client.ts +++ b/server/src/services/schema-plan-client.ts @@ -15,7 +15,7 @@ function buildInstructions(request: AiSchemaPlanRequest): string { "If a missing requirement could materially change table structure, keys, cardinality, tenancy, or deletion behavior, ask concise clarifying questions and return no operations.", "Use assumptions only for low-risk details that do not materially change the design.", "The JSON object must contain: summary (string), assumptions (string[]), warnings (string[]), clarifyingQuestions (string[]), operations (array).", - "Supported operation types: create_table, rename_table, drop_table, add_column, alter_column, drop_column, add_relationship, drop_relationship, add_index, drop_index.", + "Supported operation types: create_table, rename_table, drop_table, add_column, alter_column, drop_column, add_relationship, drop_relationship, add_index, drop_index, add_unique, add_check, drop_constraint.", "Use the exact nested operation shapes described below; do not put field attributes at the operation root.", "Every key of the answer's shape is always written: give null for any that does not apply. To remove an existing column's default, set changes.dropDefault to true; to leave the default alone, leave it null.", "create_table.table contains name, optional note, fields, and optional indexes.", diff --git a/src/features/schema-change/components/plan-panel.tsx b/src/features/schema-change/components/plan-panel.tsx index cfa0e2d..bebe6ce 100644 --- a/src/features/schema-change/components/plan-panel.tsx +++ b/src/features/schema-change/components/plan-panel.tsx @@ -149,6 +149,12 @@ function describeOperation(operation: PlanResponse["plan"]["operations"][number] return `index ${(operation.index as { name: string }).name} on ${get("tableName")} (${((operation.index as { columns: string[] }).columns ?? []).join(", ")})`; case "drop_index": return `drop index ${get("indexName")} on ${get("tableName")}`; + case "add_unique": + return `unique${operation.constraintName ? ` ${get("constraintName")}` : ""} on ${get("tableName")} (${((operation.columns as string[]) ?? []).join(", ")})`; + case "add_check": + return `check${operation.constraintName ? ` ${get("constraintName")}` : ""} on ${get("tableName")}: ${get("expression")}`; + case "drop_constraint": + return `drop constraint ${get("constraintName")} on ${get("tableName")}`; default: return String(operation.type); }