|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import Database from "better-sqlite3"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +import { runMigrations } from "./migrate"; |
| 8 | + |
| 9 | +const MIGRATIONS_FOLDER = path.resolve(__dirname, "migrations"); |
| 10 | + |
| 11 | +let sqlite: InstanceType<typeof Database>; |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + sqlite = new Database(":memory:"); |
| 15 | + sqlite.pragma("foreign_keys = ON"); |
| 16 | +}); |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + sqlite.close(); |
| 20 | +}); |
| 21 | + |
| 22 | +function ledgerMax(db: InstanceType<typeof Database>): number | null { |
| 23 | + const row = db |
| 24 | + .prepare("SELECT MAX(created_at) AS max FROM __drizzle_migrations") |
| 25 | + .get() as { max: number | null }; |
| 26 | + return row.max; |
| 27 | +} |
| 28 | + |
| 29 | +function hasColumn( |
| 30 | + db: InstanceType<typeof Database>, |
| 31 | + table: string, |
| 32 | + column: string, |
| 33 | +): boolean { |
| 34 | + return db |
| 35 | + .prepare(`PRAGMA table_info(${table})`) |
| 36 | + .all() |
| 37 | + .some((c) => (c as { name: string }).name === column); |
| 38 | +} |
| 39 | + |
| 40 | +describe("runMigrations", () => { |
| 41 | + it("applies every migration on a fresh database", () => { |
| 42 | + runMigrations(sqlite, MIGRATIONS_FOLDER); |
| 43 | + |
| 44 | + expect(hasColumn(sqlite, "workspaces", "pr_urls")).toBe(true); |
| 45 | + expect(ledgerMax(sqlite)).not.toBeNull(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("is a no-op when run twice", () => { |
| 49 | + runMigrations(sqlite, MIGRATIONS_FOLDER); |
| 50 | + const afterFirst = ledgerMax(sqlite); |
| 51 | + |
| 52 | + expect(() => runMigrations(sqlite, MIGRATIONS_FOLDER)).not.toThrow(); |
| 53 | + expect(ledgerMax(sqlite)).toBe(afterFirst); |
| 54 | + }); |
| 55 | + |
| 56 | + it("boots when the schema is already ahead of the migration ledger", () => { |
| 57 | + runMigrations(sqlite, MIGRATIONS_FOLDER); |
| 58 | + const latest = ledgerMax(sqlite); |
| 59 | + |
| 60 | + sqlite |
| 61 | + .prepare("DELETE FROM __drizzle_migrations WHERE created_at = ?") |
| 62 | + .run(latest); |
| 63 | + expect(ledgerMax(sqlite)).not.toBe(latest); |
| 64 | + |
| 65 | + expect(() => runMigrations(sqlite, MIGRATIONS_FOLDER)).not.toThrow(); |
| 66 | + expect(hasColumn(sqlite, "workspaces", "pr_urls")).toBe(true); |
| 67 | + expect(ledgerMax(sqlite)).toBe(latest); |
| 68 | + }); |
| 69 | + |
| 70 | + it("propagates errors that are not benign schema conflicts", () => { |
| 71 | + const dir = mkdtempSync(path.join(tmpdir(), "migrate-test-")); |
| 72 | + try { |
| 73 | + mkdirSync(path.join(dir, "meta"), { recursive: true }); |
| 74 | + writeFileSync( |
| 75 | + path.join(dir, "meta", "_journal.json"), |
| 76 | + JSON.stringify({ |
| 77 | + version: "7", |
| 78 | + dialect: "sqlite", |
| 79 | + entries: [ |
| 80 | + { |
| 81 | + idx: 0, |
| 82 | + version: "6", |
| 83 | + when: 1, |
| 84 | + tag: "0000_broken", |
| 85 | + breakpoints: true, |
| 86 | + }, |
| 87 | + ], |
| 88 | + }), |
| 89 | + ); |
| 90 | + writeFileSync( |
| 91 | + path.join(dir, "0000_broken.sql"), |
| 92 | + "DROP TABLE `table_that_does_not_exist`;", |
| 93 | + ); |
| 94 | + |
| 95 | + expect(() => runMigrations(sqlite, dir)).toThrow(/no such table/i); |
| 96 | + } finally { |
| 97 | + rmSync(dir, { recursive: true, force: true }); |
| 98 | + } |
| 99 | + }); |
| 100 | +}); |
0 commit comments