|
| 1 | +/** |
| 2 | + * Real column-default drift on a live Postgres database reaches the same verdict through |
| 3 | + * `meta verify --db` and `meta migrate --from-db --dry-run`, both on the shadow path: a |
| 4 | + * default somebody changed by hand (an integer 7 made 8, `now()` made `now() + 1 day`) is |
| 5 | + * reported by verify and planned by migrate, while the defaults exactly as the metadata |
| 6 | + * declares them read clean. The oracle is the ALTER TABLE the test runs itself. |
| 7 | + * |
| 8 | + * An explicitly configured scratch database that cannot be reached is a hard error naming |
| 9 | + * the cause in both commands, never a silent fall-back to another comparison. Plain |
| 10 | + * `meta migrate --dry-run` (no --from-db) reads no database at all: it says so, and it |
| 11 | + * refuses a --shadow-db it would otherwise have ignored. |
| 12 | + * |
| 13 | + * Skips gracefully when MIGRATE_TS_PG_URL is not set. |
| 14 | + */ |
| 15 | +import { describe, test, expect, beforeAll, afterAll } from "bun:test"; |
| 16 | +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; |
| 17 | +import { tmpdir } from "node:os"; |
| 18 | +import { join } from "node:path"; |
| 19 | +import { Pool } from "pg"; |
| 20 | +import { run } from "../../src/index.js"; |
| 21 | + |
| 22 | +const PG_URL = process.env.MIGRATE_TS_PG_URL; |
| 23 | +const TABLE = "shadow_default_drift_events"; |
| 24 | +const UNREACHABLE_SHADOW = "postgresql://nobody:nothing@127.0.0.1:1/nope"; |
| 25 | + |
| 26 | +const MODEL = JSON.stringify({ |
| 27 | + "metadata.root": { |
| 28 | + children: [{ |
| 29 | + "object.entity": { |
| 30 | + name: "Event", |
| 31 | + children: [ |
| 32 | + { "source.rdb": { name: "src", "@table": TABLE } }, |
| 33 | + { "field.long": { name: "id" } }, |
| 34 | + { "field.int": { name: "seq", "@default": 7, "@required": true } }, |
| 35 | + { "field.timestamp": { name: "startsAt", "@column": "starts_at", "@default": "NOW()", "@required": true } }, |
| 36 | + { "identity.primary": { name: "pk", "@fields": ["id"] } }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + }], |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +describe("shadow path: live default drift and an unreachable explicit shadow (env-gated)", () => { |
| 44 | + if (PG_URL === undefined || PG_URL.length === 0) { |
| 45 | + test.skip("skipped — MIGRATE_TS_PG_URL not set", () => {}); |
| 46 | + return; |
| 47 | + } |
| 48 | + let pool: Pool; |
| 49 | + let root: string; |
| 50 | + const args = (...rest: string[]): string[] => [...rest, "--cwd", root, "--db", PG_URL, "--dialect", "postgres"]; |
| 51 | + |
| 52 | + /** Run the CLI, collecting everything it prints. */ |
| 53 | + const captured = async (argv: string[]): Promise<{ exit: number; out: string }> => { |
| 54 | + const lines: string[] = []; |
| 55 | + const origLog = console.log; |
| 56 | + const origErr = console.error; |
| 57 | + const origWarn = console.warn; |
| 58 | + const keep = (...a: unknown[]): void => { lines.push(a.map(String).join(" ")); }; |
| 59 | + console.log = keep; console.error = keep; console.warn = keep; |
| 60 | + try { |
| 61 | + return { exit: await run(argv), out: lines.join("\n") }; |
| 62 | + } finally { |
| 63 | + console.log = origLog; console.error = origErr; console.warn = origWarn; |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + beforeAll(async () => { |
| 68 | + pool = new Pool({ connectionString: PG_URL }); |
| 69 | + await pool.query(`DROP TABLE IF EXISTS ${TABLE} CASCADE`); |
| 70 | + root = mkdtempSync(join(tmpdir(), "shadow-default-drift-pg-")); |
| 71 | + mkdirSync(join(root, "metaobjects"), { recursive: true }); |
| 72 | + writeFileSync(join(root, "metaobjects", "meta.events.json"), MODEL, "utf8"); |
| 73 | + expect((await captured(args("migrate", "--from-db", "--slug", "init", "--apply"))).exit).toBe(0); |
| 74 | + }); |
| 75 | + afterAll(async () => { |
| 76 | + await pool.query(`DROP TABLE IF EXISTS ${TABLE} CASCADE`); |
| 77 | + await pool.end(); |
| 78 | + rmSync(root, { recursive: true, force: true }); |
| 79 | + }); |
| 80 | + |
| 81 | + test("the defaults as declared read clean through both commands", async () => { |
| 82 | + expect((await captured(args("verify"))).exit).toBe(0); |
| 83 | + const planned = await captured(args("migrate", "--from-db", "--dry-run")); |
| 84 | + expect(planned.exit).toBe(0); |
| 85 | + expect(planned.out).not.toContain("SET DEFAULT"); |
| 86 | + }); |
| 87 | + |
| 88 | + test("seq 8 vs 7 and starts_at now() + 1 day vs now() are drift for verify and planned by migrate", async () => { |
| 89 | + await pool.query(`ALTER TABLE ${TABLE} ALTER COLUMN seq SET DEFAULT 8`); |
| 90 | + await pool.query(`ALTER TABLE ${TABLE} ALTER COLUMN starts_at SET DEFAULT now() + interval '1 day'`); |
| 91 | + try { |
| 92 | + const verified = await captured(args("verify")); |
| 93 | + expect(verified.exit).toBe(1); |
| 94 | + expect(verified.out).toContain("seq"); |
| 95 | + expect(verified.out).toContain("starts_at"); |
| 96 | + |
| 97 | + const planned = await captured(args("migrate", "--from-db", "--dry-run")); |
| 98 | + expect(planned.exit).toBe(0); |
| 99 | + expect(planned.out).toContain("compared against a scratch (shadow) database"); |
| 100 | + expect(planned.out).toMatch(/ALTER COLUMN \\?"seq\\?" SET DEFAULT '?7'?;/); |
| 101 | + expect(planned.out).toMatch(/ALTER COLUMN \\?"starts_at\\?" SET DEFAULT now\(\);/); |
| 102 | + |
| 103 | + // Without --from-db, migrate diffs the metadata against the committed snapshot and |
| 104 | + // never reads the database: it plans nothing here, and says so rather than reading |
| 105 | + // as a clean comparison with the database. |
| 106 | + const offline = await captured(args("migrate", "--dry-run")); |
| 107 | + expect(offline.exit).toBe(0); |
| 108 | + expect(offline.out).toContain("no database was read"); |
| 109 | + expect(offline.out).toContain("--from-db"); |
| 110 | + } finally { |
| 111 | + await pool.query(`ALTER TABLE ${TABLE} ALTER COLUMN seq SET DEFAULT 7`); |
| 112 | + await pool.query(`ALTER TABLE ${TABLE} ALTER COLUMN starts_at SET DEFAULT now()`); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + test("an explicit --shadow-db that cannot be reached is a hard error in migrate and verify", async () => { |
| 117 | + // A run that reads no database refuses a --shadow-db it would otherwise ignore. |
| 118 | + const offline = await captured(args("migrate", "--dry-run", "--shadow-db", UNREACHABLE_SHADOW)); |
| 119 | + expect(offline.exit).toBe(2); |
| 120 | + expect(offline.out).toContain("--shadow-db needs --from-db"); |
| 121 | + for (const argv of [ |
| 122 | + args("migrate", "--from-db", "--dry-run", "--shadow-db", UNREACHABLE_SHADOW), |
| 123 | + args("verify", "--shadow-db", UNREACHABLE_SHADOW), |
| 124 | + ]) { |
| 125 | + const r = await captured(argv); |
| 126 | + expect(r.exit).not.toBe(0); |
| 127 | + expect(r.out).toContain("shadow database"); |
| 128 | + expect(r.out).toContain("127.0.0.1:1"); |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
0 commit comments