|
| 1 | +// F58 + F71 — `verify --replay`'s remedy line prescribed something that cannot work. |
| 2 | +// |
| 3 | +// The old line was unconditional: "fix this with a NEW migration that creates the missing |
| 4 | +// object". Migrations apply in timestamp order, so a new one sorts AFTER the file that |
| 5 | +// failed and the object is still missing when it runs — the advice is wrong in every case, |
| 6 | +// and worse when another tool owns schema creation, where following it literally means |
| 7 | +// duplicating a schema this project does not own. |
| 8 | +// |
| 9 | +// These assert the CLAIMS in the message, because a remedy that names a fix which cannot |
| 10 | +// apply is the whole defect. The failure is silent by construction: nothing about a |
| 11 | +// wrong-but-confident sentence turns an exit code red. |
| 12 | + |
| 13 | +import { describe, test, expect } from "bun:test"; |
| 14 | +import { MigrationApplyError } from "@metaobjectsdev/migrate-ts"; |
| 15 | +import { replayRemedy } from "../src/lib/replay-remedy.js"; |
| 16 | + |
| 17 | +const text = (err: unknown): string => replayRemedy(err).join(" "); |
| 18 | + |
| 19 | +describe("replayRemedy", () => { |
| 20 | + test("never prescribes a new migration — the fix that cannot work", () => { |
| 21 | + for (const err of [ |
| 22 | + new MigrationApplyError("20260521012518-init", 0, 3, new Error("no such table: purchases")), |
| 23 | + new MigrationApplyError("20260812090000-add-fk", 2, 3, new Error("no such table: purchases")), |
| 24 | + new Error("no such table: purchases"), |
| 25 | + ]) { |
| 26 | + const msg = text(err); |
| 27 | + expect(msg).not.toMatch(/fix this with a NEW migration/i); |
| 28 | + expect(msg).toContain("A NEW migration cannot fix this"); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + test("names the failing migration and its position in the chain", () => { |
| 33 | + const msg = text(new MigrationApplyError("20260812090000-add-fk", 2, 5, new Error("boom"))); |
| 34 | + expect(msg).toContain("'20260812090000-add-fk'"); |
| 35 | + expect(msg).toContain("migration 3 of 5"); |
| 36 | + }); |
| 37 | + |
| 38 | + test("F71 — the head of the chain gets the sentence that is only true there", () => { |
| 39 | + const msg = text(new MigrationApplyError("20260521012518-init", 0, 3, new Error("boom"))); |
| 40 | + expect(msg).toContain("FIRST migration in the chain"); |
| 41 | + expect(msg).toContain("base schema comes from outside it"); |
| 42 | + }); |
| 43 | + |
| 44 | + test("a later migration does NOT get the head-of-chain sentence", () => { |
| 45 | + const msg = text(new MigrationApplyError("20260812090000-add-fk", 2, 3, new Error("boom"))); |
| 46 | + expect(msg).not.toContain("FIRST migration in the chain"); |
| 47 | + }); |
| 48 | + |
| 49 | + test("F58 — offers the possibility that another tool owns schema creation", () => { |
| 50 | + // The estate that produced this has drizzle-kit building the tables and |
| 51 | + // `.metaobjects/migrations/` holding later patches. The old message had no third |
| 52 | + // option: it assumed MetaObjects is the only schema authority. |
| 53 | + const msg = text(new MigrationApplyError("20260521012518-init", 0, 3, new Error("boom"))); |
| 54 | + expect(msg).toMatch(/another tool or an out-of-band script owns schema creation/i); |
| 55 | + expect(msg).toContain("don't wire"); |
| 56 | + }); |
| 57 | + |
| 58 | + test("immutability is stated as the condition it is, not a blanket ban", () => { |
| 59 | + // A migration no database has applied yet is edited in place every day; the checksum |
| 60 | + // guard is what stops you once one has. The old line forbade the edit outright. |
| 61 | + const msg = text(new MigrationApplyError("20260521012518-init", 0, 1, new Error("boom"))); |
| 62 | + expect(msg).toContain("if no database has applied it yet"); |
| 63 | + expect(msg).toContain("baseline --from-db"); |
| 64 | + }); |
| 65 | + |
| 66 | + test("degrades to the un-positioned form for an error that is not a MigrationApplyError", () => { |
| 67 | + // applyPending can fail before it reaches any file (the checksum tamper guard, a |
| 68 | + // ledger failure). The remedy must still be right, just less specific. |
| 69 | + const msg = text(new Error("ledger unavailable")); |
| 70 | + expect(msg).toContain("the committed chain does not apply to an empty database"); |
| 71 | + expect(msg).not.toContain("FIRST migration in the chain"); |
| 72 | + }); |
| 73 | +}); |
0 commit comments