Skip to content

Commit bfbcbea

Browse files
dmealingclaude
andcommitted
test(migrate-ts): e2e existing-table CHECK evolution + no double-emit on create
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 434d8dc commit bfbcbea

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

  • server/typescript/packages/migrate-ts/test/check-evolution
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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

Comments
 (0)