|
1 | 1 | import { test, expect, describe } from "bun:test"; |
2 | 2 | import { diff } from "../../src/diff/index.js"; |
| 3 | +import { emit } from "../../src/emit/index.js"; |
3 | 4 | import type { SchemaSnapshot, ColumnDescriptor } from "../../src/types.js"; |
4 | 5 |
|
5 | 6 | const empty: SchemaSnapshot = { tables: [], views: [] }; |
@@ -137,3 +138,93 @@ describe("diff — view-body drift", () => { |
137 | 138 | }); |
138 | 139 | }); |
139 | 140 | }); |
| 141 | + |
| 142 | +// --------------------------------------------------------------------------- |
| 143 | +// #239 — the Postgres adopt-view branch (DB view carries no fingerprint) must |
| 144 | +// make the SAME legal/illegal OR-REPLACE decision the managed path makes. |
| 145 | +// A structural change (rename / reorder / mid-insert) is NOT a legal |
| 146 | +// CREATE OR REPLACE, so it must be emitted as drop-view + create-view — and the |
| 147 | +// adoption of an unmanaged view still requires allow.adoptView (the recreate-pair |
| 148 | +// auto-allow must not wave through clobbering hand-written SQL). |
| 149 | +// --------------------------------------------------------------------------- |
| 150 | +describe("diff — Postgres adopt-view legality (#239)", () => { |
| 151 | + const T = { kind: "text" as const }; |
| 152 | + // A view metadata (expected) side always carries a fingerprint. |
| 153 | + const expView = (cols: string[]) => ({ |
| 154 | + name: "v_foo", |
| 155 | + sql: `SELECT ${cols.map((c) => `t.${c} AS ${c}`).join(", ")} FROM t`, |
| 156 | + fingerprint: "a".repeat(64), // raw sha256-hex; renderFingerprintMarker adds the prefix |
| 157 | + columns: cols.map((name) => ({ name, sqlType: T })), |
| 158 | + }); |
| 159 | + // The DB (actual) side has NO fingerprint — pre-fingerprint or hand-written. |
| 160 | + const dbView = (cols: string[]) => ({ |
| 161 | + name: "v_foo", |
| 162 | + sql: `SELECT ${cols.map((c) => `t.${c} AS ${c}`).join(", ")} FROM t`, |
| 163 | + columns: cols.map((name) => ({ name, sqlType: T })), |
| 164 | + }); |
| 165 | + const opts = (adoptView = false) => ({ dialect: "postgres" as const, allow: { adoptView } }); |
| 166 | + |
| 167 | + test("STRUCTURAL change (renamed output column) → drop-view + create-view, NOT replace-view", async () => { |
| 168 | + // DB view outputs (a, b); metadata renames b→c → columns (a, c). Postgres cannot |
| 169 | + // OR-REPLACE a column rename ("cannot change name of view column"). |
| 170 | + const r = await diff( |
| 171 | + { tables: [], views: [expView(["a", "c"])] }, |
| 172 | + { tables: [], views: [dbView(["a", "b"])] }, |
| 173 | + opts(true), |
| 174 | + ); |
| 175 | + expect(r.changes.find((c) => c.kind === "replace-view")).toBeUndefined(); |
| 176 | + expect(r.changes.find((c) => c.kind === "drop-view")).toMatchObject({ kind: "drop-view", view: "v_foo" }); |
| 177 | + expect(r.changes.find((c) => c.kind === "create-view")).toMatchObject({ |
| 178 | + kind: "create-view", view: { name: "v_foo" }, |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + test("adopting an unmanaged view via drop+create STILL requires allow.adoptView", async () => { |
| 183 | + // Without allow.adoptView the drop must be BLOCKED — the recreate-pair auto-allow |
| 184 | + // must not silently clobber the (possibly hand-written) unmanaged view. |
| 185 | + const r = await diff( |
| 186 | + { tables: [], views: [expView(["a", "c"])] }, |
| 187 | + { tables: [], views: [dbView(["a", "b"])] }, |
| 188 | + opts(false), |
| 189 | + ); |
| 190 | + const dropv = r.changes.find((c) => c.kind === "drop-view"); |
| 191 | + expect(dropv?.status.state).toBe("blocked"); |
| 192 | + // With allow.adoptView → allowed. |
| 193 | + const r2 = await diff( |
| 194 | + { tables: [], views: [expView(["a", "c"])] }, |
| 195 | + { tables: [], views: [dbView(["a", "b"])] }, |
| 196 | + opts(true), |
| 197 | + ); |
| 198 | + expect(r2.changes.find((c) => c.kind === "drop-view")?.status.state).toBe("allowed"); |
| 199 | + }); |
| 200 | + |
| 201 | + test("PURE APPEND (columns are a prefix) is still a legal replace-view (unmanagedActual)", async () => { |
| 202 | + // DB (a, b); metadata (a, b, c) — c appended. Legal OR-REPLACE → replace-view, |
| 203 | + // still gated on adoptView. |
| 204 | + const r = await diff( |
| 205 | + { tables: [], views: [expView(["a", "b", "c"])] }, |
| 206 | + { tables: [], views: [dbView(["a", "b"])] }, |
| 207 | + opts(true), |
| 208 | + ); |
| 209 | + expect(r.changes.find((c) => c.kind === "replace-view")).toMatchObject({ |
| 210 | + kind: "replace-view", view: { name: "v_foo" }, unmanagedActual: true, |
| 211 | + }); |
| 212 | + expect(r.changes.find((c) => c.kind === "drop-view")).toBeUndefined(); |
| 213 | + }); |
| 214 | + |
| 215 | + test("emitted up.sql for a structural adoption is DROP+CREATE (no illegal CREATE OR REPLACE)", async () => { |
| 216 | + // The bug's symptom: `CREATE OR REPLACE VIEW` for a rename fails at apply on a DB |
| 217 | + // that already holds the prior view. The fix emits DROP VIEW + CREATE VIEW + a |
| 218 | + // fingerprint COMMENT (so the next migrate converges). |
| 219 | + const r = await diff( |
| 220 | + { tables: [], views: [expView(["a", "c"])] }, |
| 221 | + { tables: [], views: [dbView(["a", "b"])] }, |
| 222 | + opts(true), |
| 223 | + ); |
| 224 | + const up = emit(r.changes, { dialect: "postgres" }).up; |
| 225 | + expect(up).not.toMatch(/CREATE OR REPLACE VIEW/i); |
| 226 | + expect(up).toMatch(/DROP VIEW/i); |
| 227 | + expect(up).toMatch(/CREATE VIEW/i); |
| 228 | + expect(up).toMatch(/COMMENT ON VIEW .* IS 'metaobjects:/i); |
| 229 | + }); |
| 230 | +}); |
0 commit comments