|
| 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 | +const MID_HISTORY_ADD_COLUMN_TIMESTAMP = 1782781314961; |
| 12 | + |
| 13 | +let sqlite: InstanceType<typeof Database>; |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + sqlite = new Database(":memory:"); |
| 17 | + sqlite.pragma("foreign_keys = ON"); |
| 18 | +}); |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + sqlite.close(); |
| 22 | +}); |
| 23 | + |
| 24 | +function ledgerMax(db: InstanceType<typeof Database>): number | null { |
| 25 | + const row = db |
| 26 | + .prepare("SELECT MAX(created_at) AS max FROM __drizzle_migrations") |
| 27 | + .get() as { max: number | null }; |
| 28 | + return row.max; |
| 29 | +} |
| 30 | + |
| 31 | +function ledgerHas( |
| 32 | + db: InstanceType<typeof Database>, |
| 33 | + timestamp: number, |
| 34 | +): boolean { |
| 35 | + const row = db |
| 36 | + .prepare( |
| 37 | + "SELECT COUNT(*) AS count FROM __drizzle_migrations WHERE created_at = ?", |
| 38 | + ) |
| 39 | + .get(timestamp) as { count: number }; |
| 40 | + return row.count > 0; |
| 41 | +} |
| 42 | + |
| 43 | +function hasColumn( |
| 44 | + db: InstanceType<typeof Database>, |
| 45 | + table: string, |
| 46 | + column: string, |
| 47 | +): boolean { |
| 48 | + return db |
| 49 | + .prepare(`PRAGMA table_info(${table})`) |
| 50 | + .all() |
| 51 | + .some((c) => (c as { name: string }).name === column); |
| 52 | +} |
| 53 | + |
| 54 | +describe("runMigrations", () => { |
| 55 | + it("applies every migration on a fresh database", () => { |
| 56 | + runMigrations(sqlite, MIGRATIONS_FOLDER); |
| 57 | + |
| 58 | + expect(hasColumn(sqlite, "workspaces", "pr_urls")).toBe(true); |
| 59 | + expect(ledgerMax(sqlite)).not.toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("is a no-op when run twice", () => { |
| 63 | + runMigrations(sqlite, MIGRATIONS_FOLDER); |
| 64 | + const afterFirst = ledgerMax(sqlite); |
| 65 | + |
| 66 | + expect(() => runMigrations(sqlite, MIGRATIONS_FOLDER)).not.toThrow(); |
| 67 | + expect(ledgerMax(sqlite)).toBe(afterFirst); |
| 68 | + }); |
| 69 | + |
| 70 | + it("boots when the schema is already ahead of the migration ledger", () => { |
| 71 | + runMigrations(sqlite, MIGRATIONS_FOLDER); |
| 72 | + const latest = ledgerMax(sqlite); |
| 73 | + |
| 74 | + sqlite |
| 75 | + .prepare("DELETE FROM __drizzle_migrations WHERE created_at = ?") |
| 76 | + .run(latest); |
| 77 | + expect(ledgerMax(sqlite)).not.toBe(latest); |
| 78 | + |
| 79 | + expect(() => runMigrations(sqlite, MIGRATIONS_FOLDER)).not.toThrow(); |
| 80 | + expect(hasColumn(sqlite, "workspaces", "pr_urls")).toBe(true); |
| 81 | + expect(ledgerMax(sqlite)).toBe(latest); |
| 82 | + }); |
| 83 | + |
| 84 | + it("re-applies a missing mid-history ledger entry", () => { |
| 85 | + runMigrations(sqlite, MIGRATIONS_FOLDER); |
| 86 | + |
| 87 | + sqlite |
| 88 | + .prepare("DELETE FROM __drizzle_migrations WHERE created_at = ?") |
| 89 | + .run(MID_HISTORY_ADD_COLUMN_TIMESTAMP); |
| 90 | + expect(ledgerHas(sqlite, MID_HISTORY_ADD_COLUMN_TIMESTAMP)).toBe(false); |
| 91 | + |
| 92 | + expect(() => runMigrations(sqlite, MIGRATIONS_FOLDER)).not.toThrow(); |
| 93 | + expect(ledgerHas(sqlite, MID_HISTORY_ADD_COLUMN_TIMESTAMP)).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it("propagates errors other than duplicate-column conflicts", () => { |
| 97 | + const dir = writeTempMigration("DROP TABLE `table_that_does_not_exist`;"); |
| 98 | + try { |
| 99 | + expect(() => runMigrations(sqlite, dir)).toThrow(/no such table/i); |
| 100 | + } finally { |
| 101 | + rmSync(dir, { recursive: true, force: true }); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + it("does not swallow an 'already exists' conflict from a new migration", () => { |
| 106 | + sqlite.exec("CREATE TABLE existing_table (id text)"); |
| 107 | + const dir = writeTempMigration( |
| 108 | + "CREATE TABLE `existing_table` (`id` text);", |
| 109 | + ); |
| 110 | + try { |
| 111 | + expect(() => runMigrations(sqlite, dir)).toThrow(/already exists/i); |
| 112 | + } finally { |
| 113 | + rmSync(dir, { recursive: true, force: true }); |
| 114 | + } |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +function writeTempMigration(sql: string): string { |
| 119 | + const dir = mkdtempSync(path.join(tmpdir(), "migrate-test-")); |
| 120 | + mkdirSync(path.join(dir, "meta"), { recursive: true }); |
| 121 | + writeFileSync( |
| 122 | + path.join(dir, "meta", "_journal.json"), |
| 123 | + JSON.stringify({ |
| 124 | + version: "7", |
| 125 | + dialect: "sqlite", |
| 126 | + entries: [ |
| 127 | + { idx: 0, version: "6", when: 1, tag: "0000_temp", breakpoints: true }, |
| 128 | + ], |
| 129 | + }), |
| 130 | + ); |
| 131 | + writeFileSync(path.join(dir, "0000_temp.sql"), sql); |
| 132 | + return dir; |
| 133 | +} |
0 commit comments