|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import type { SchemaSnapshot, TableDescriptor, CheckDescriptor } from "../../src/types.js"; |
| 3 | +import { diff } from "../../src/diff/index.js"; |
| 4 | +import { emit } from "../../src/emit/index.js"; |
| 5 | + |
| 6 | +const CHK = (n: string, e: string): CheckDescriptor => ({ name: n, expression: e }); |
| 7 | +function tbl(checks: CheckDescriptor[]): TableDescriptor { |
| 8 | + return { name: "orders", columns: [{ name: "qty", sqlType: { kind: "integer", bits: 32 }, nullable: false }], |
| 9 | + indexes: [], foreignKeys: [], checks, primaryKey: ["qty"] }; |
| 10 | +} |
| 11 | +const snap = (checks: CheckDescriptor[]): SchemaSnapshot => ({ tables: [tbl(checks)], views: [] }); |
| 12 | + |
| 13 | +describe("e2e: existing-table CHECK evolution (postgres)", () => { |
| 14 | + test("adding a check on an existing table emits ALTER TABLE ADD CONSTRAINT", async () => { |
| 15 | + const r = await diff({ expected: snap([CHK("orders_qty_chk", "qty >= 1")]), actual: snap([]), dialect: "postgres" }); |
| 16 | + const { up } = emit(r.changes, { dialect: "postgres" }); |
| 17 | + expect(up).toContain(`ALTER TABLE "orders" ADD CONSTRAINT "orders_qty_chk" CHECK (qty >= 1);`); |
| 18 | + }); |
| 19 | + test("create-table on empty actual inlines the check (no separate ADD CONSTRAINT)", async () => { |
| 20 | + // a brand-new table carries its checks inline in CREATE TABLE; diffTableChecks |
| 21 | + // runs only for tables present on BOTH sides, so no add-check fires here. |
| 22 | + const fresh: SchemaSnapshot = { tables: [tbl([CHK("orders_qty_chk", "qty >= 1")])], views: [] }; |
| 23 | + const r = await diff({ expected: fresh, actual: { tables: [], views: [] }, dialect: "postgres" }); |
| 24 | + const { up } = emit(r.changes, { dialect: "postgres" }); |
| 25 | + expect(up).toContain(`CONSTRAINT "orders_qty_chk" CHECK (qty >= 1)`); // inline in CREATE TABLE |
| 26 | + expect(up).not.toContain(`ADD CONSTRAINT "orders_qty_chk"`); // not also an ALTER |
| 27 | + }); |
| 28 | +}); |
0 commit comments