|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { stat, writeFile } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { backupDatabase } from "../scripts/backup"; |
| 5 | +import { migrateDatabase } from "../scripts/migrate"; |
| 6 | +import { |
| 7 | + createDatabasePort, |
| 8 | + readMigrationCount, |
| 9 | + withDatabasePath, |
| 10 | +} from "./helpers/database-script-helpers"; |
| 11 | + |
| 12 | +const EMBEDDED_POSTGRES_TEST_TIMEOUT_MS = 20_000; |
| 13 | + |
| 14 | +describe("database backup script", () => { |
| 15 | + it( |
| 16 | + "omits stale PostgreSQL runtime files from copied backups", |
| 17 | + () => |
| 18 | + withDatabasePath(async (dbPath) => { |
| 19 | + await migrateDatabase({ dbPath, port: await createDatabasePort() }); |
| 20 | + const staleRuntimeFiles = ["postmaster.pid", "postmaster.opts"]; |
| 21 | + |
| 22 | + for (const fileName of staleRuntimeFiles) { |
| 23 | + await writeFile(path.join(dbPath, fileName), "stale runtime state"); |
| 24 | + } |
| 25 | + |
| 26 | + const result = await backupDatabase({ |
| 27 | + dbPath, |
| 28 | + now: new Date("2026-05-20T01:02:03.004Z"), |
| 29 | + }); |
| 30 | + |
| 31 | + for (const fileName of staleRuntimeFiles) { |
| 32 | + await expectPath(path.join(dbPath, fileName), true); |
| 33 | + await expectPath(path.join(result.backupPath, fileName), false); |
| 34 | + } |
| 35 | + await expect( |
| 36 | + readMigrationCount(result.backupPath), |
| 37 | + ).resolves.toBeGreaterThan(0); |
| 38 | + }), |
| 39 | + EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, |
| 40 | + ); |
| 41 | +}); |
| 42 | + |
| 43 | +async function expectPath(filePath: string, exists: boolean): Promise<void> { |
| 44 | + await expect(pathExists(filePath)).resolves.toBe(exists); |
| 45 | +} |
| 46 | + |
| 47 | +async function pathExists(filePath: string): Promise<boolean> { |
| 48 | + try { |
| 49 | + await stat(filePath); |
| 50 | + return true; |
| 51 | + } catch { |
| 52 | + return false; |
| 53 | + } |
| 54 | +} |
0 commit comments