|
| 1 | +import { Database } from "bun:sqlite"; |
| 2 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { afterEach, describe, expect, it } from "@effect/vitest"; |
| 6 | +import { drizzle } from "drizzle-orm/bun-sqlite"; |
| 7 | +import { migrate } from "drizzle-orm/bun-sqlite/migrator"; |
| 8 | + |
| 9 | +import { |
| 10 | + LocalDatabaseMigrationHistoryMismatch, |
| 11 | + LocalDatabaseSchemaTooNew, |
| 12 | + checkDrizzleMigrationCompatibility, |
| 13 | + readAppliedDrizzleMigrationHashes, |
| 14 | + readBundledDrizzleMigrationHashes, |
| 15 | +} from "./executor"; |
| 16 | + |
| 17 | +const MIGRATIONS_FOLDER = join(import.meta.dirname, "../../drizzle"); |
| 18 | + |
| 19 | +const workDirs: string[] = []; |
| 20 | + |
| 21 | +const tempDb = (): { db: Database; path: string } => { |
| 22 | + const dir = mkdtempSync(join(tmpdir(), "executor-schema-compat-")); |
| 23 | + workDirs.push(dir); |
| 24 | + const path = join(dir, "data.db"); |
| 25 | + return { db: new Database(path), path }; |
| 26 | +}; |
| 27 | + |
| 28 | +const createMigrationTable = (db: Database): void => { |
| 29 | + db.exec(` |
| 30 | + CREATE TABLE __drizzle_migrations ( |
| 31 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 32 | + hash TEXT NOT NULL, |
| 33 | + created_at NUMERIC |
| 34 | + ) |
| 35 | + `); |
| 36 | +}; |
| 37 | + |
| 38 | +const insertMigrationHashes = (db: Database, hashes: ReadonlyArray<string>): void => { |
| 39 | + const stmt = db.prepare("INSERT INTO __drizzle_migrations (hash, created_at) VALUES (?, ?)"); |
| 40 | + for (const [index, hash] of hashes.entries()) { |
| 41 | + stmt.run(hash, index + 1); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +afterEach(() => { |
| 46 | + for (const dir of workDirs.splice(0)) { |
| 47 | + rmSync(dir, { recursive: true, force: true }); |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +describe("Drizzle migration compatibility preflight", () => { |
| 52 | + it("allows a fresh DB without __drizzle_migrations", () => { |
| 53 | + const { db, path } = tempDb(); |
| 54 | + try { |
| 55 | + expect(() => |
| 56 | + checkDrizzleMigrationCompatibility({ |
| 57 | + sqlite: db, |
| 58 | + dbPath: path, |
| 59 | + migrationsFolder: MIGRATIONS_FOLDER, |
| 60 | + }), |
| 61 | + ).not.toThrow(); |
| 62 | + } finally { |
| 63 | + db.close(); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + it("allows an existing but empty __drizzle_migrations table", () => { |
| 68 | + const { db, path } = tempDb(); |
| 69 | + try { |
| 70 | + createMigrationTable(db); |
| 71 | + |
| 72 | + expect(() => |
| 73 | + checkDrizzleMigrationCompatibility({ |
| 74 | + sqlite: db, |
| 75 | + dbPath: path, |
| 76 | + migrationsFolder: MIGRATIONS_FOLDER, |
| 77 | + }), |
| 78 | + ).not.toThrow(); |
| 79 | + } finally { |
| 80 | + db.close(); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + it("computes bundled hashes that exactly match hashes written by Drizzle", () => { |
| 85 | + const { db } = tempDb(); |
| 86 | + try { |
| 87 | + migrate(drizzle(db), { migrationsFolder: MIGRATIONS_FOLDER }); |
| 88 | + |
| 89 | + expect(readAppliedDrizzleMigrationHashes(db)).toEqual( |
| 90 | + readBundledDrizzleMigrationHashes(MIGRATIONS_FOLDER), |
| 91 | + ); |
| 92 | + } finally { |
| 93 | + db.close(); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it("throws LocalDatabaseSchemaTooNew when the DB has more migrations than the binary", () => { |
| 98 | + const { db, path } = tempDb(); |
| 99 | + try { |
| 100 | + const bundled = readBundledDrizzleMigrationHashes(MIGRATIONS_FOLDER); |
| 101 | + createMigrationTable(db); |
| 102 | + insertMigrationHashes(db, [...bundled, "future-migration-hash"]); |
| 103 | + |
| 104 | + expect(() => |
| 105 | + checkDrizzleMigrationCompatibility({ |
| 106 | + sqlite: db, |
| 107 | + dbPath: path, |
| 108 | + migrationsFolder: MIGRATIONS_FOLDER, |
| 109 | + }), |
| 110 | + ).toThrow(LocalDatabaseSchemaTooNew); |
| 111 | + |
| 112 | + try { |
| 113 | + checkDrizzleMigrationCompatibility({ |
| 114 | + sqlite: db, |
| 115 | + dbPath: path, |
| 116 | + migrationsFolder: MIGRATIONS_FOLDER, |
| 117 | + }); |
| 118 | + } catch (error) { |
| 119 | + expect(error).toBeInstanceOf(LocalDatabaseSchemaTooNew); |
| 120 | + expect((error as Error).message).toContain( |
| 121 | + "This Executor binary is older than the schema", |
| 122 | + ); |
| 123 | + expect((error as Error).message).toContain("Use a newer Executor binary"); |
| 124 | + } |
| 125 | + } finally { |
| 126 | + db.close(); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + it("throws LocalDatabaseMigrationHistoryMismatch when hashes diverge", () => { |
| 131 | + const { db, path } = tempDb(); |
| 132 | + try { |
| 133 | + const bundled = readBundledDrizzleMigrationHashes(MIGRATIONS_FOLDER); |
| 134 | + createMigrationTable(db); |
| 135 | + insertMigrationHashes(db, ["different-migration-hash", ...bundled.slice(1)]); |
| 136 | + |
| 137 | + expect(() => |
| 138 | + checkDrizzleMigrationCompatibility({ |
| 139 | + sqlite: db, |
| 140 | + dbPath: path, |
| 141 | + migrationsFolder: MIGRATIONS_FOLDER, |
| 142 | + }), |
| 143 | + ).toThrow(LocalDatabaseMigrationHistoryMismatch); |
| 144 | + |
| 145 | + try { |
| 146 | + checkDrizzleMigrationCompatibility({ |
| 147 | + sqlite: db, |
| 148 | + dbPath: path, |
| 149 | + migrationsFolder: MIGRATIONS_FOLDER, |
| 150 | + }); |
| 151 | + } catch (error) { |
| 152 | + expect(error).toBeInstanceOf(LocalDatabaseMigrationHistoryMismatch); |
| 153 | + expect((error as Error).message).toContain( |
| 154 | + "does not match this Executor build", |
| 155 | + ); |
| 156 | + expect((error as Error).message).toContain("restore a backup"); |
| 157 | + } |
| 158 | + } finally { |
| 159 | + db.close(); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it("allows an older DB whose migration history is a bundled prefix", () => { |
| 164 | + const { db, path } = tempDb(); |
| 165 | + try { |
| 166 | + const bundled = readBundledDrizzleMigrationHashes(MIGRATIONS_FOLDER); |
| 167 | + createMigrationTable(db); |
| 168 | + insertMigrationHashes(db, bundled.slice(0, 1)); |
| 169 | + |
| 170 | + expect(() => |
| 171 | + checkDrizzleMigrationCompatibility({ |
| 172 | + sqlite: db, |
| 173 | + dbPath: path, |
| 174 | + migrationsFolder: MIGRATIONS_FOLDER, |
| 175 | + }), |
| 176 | + ).not.toThrow(); |
| 177 | + } finally { |
| 178 | + db.close(); |
| 179 | + } |
| 180 | + }); |
| 181 | +}); |
0 commit comments