|
| 1 | +import { URL } from "node:url"; |
| 2 | +import pg from "pg"; |
| 3 | +import { exec as execRaw } from "node:child_process"; |
| 4 | +import { promisify } from "node:util"; |
| 5 | +import Express from "express"; |
| 6 | + |
| 7 | +const exec = promisify(execRaw); |
| 8 | + |
| 9 | +function invariant(cond: any, message: string): asserts cond { |
| 10 | + if (!cond) { |
| 11 | + throw new Error("Invariant violation: " + message); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +const __dirname = new URL(".", import.meta.url).pathname; |
| 16 | + |
| 17 | +const rawUrl = process.env.DATABASE_URL; |
| 18 | +invariant(rawUrl, "DATABASE_URL not set"); |
| 19 | +const url = new URL(rawUrl); |
| 20 | +invariant( |
| 21 | + url.protocol === "postgresql:", |
| 22 | + "DATABASE_URL must be a postgres URL", |
| 23 | +); |
| 24 | + |
| 25 | +const client = new pg.Client({ |
| 26 | + user: url.username, |
| 27 | + host: url.hostname, |
| 28 | + database: url.pathname.slice(1), |
| 29 | + password: url.password, |
| 30 | + port: parseInt(url.port || "5432", 10), |
| 31 | +}); |
| 32 | +await client.connect(); |
| 33 | + |
| 34 | +// First, create a template database |
| 35 | +await client.query("DROP DATABASE IF EXISTS badger_test_template"); |
| 36 | +await client.query("CREATE DATABASE badger_test_template"); |
| 37 | +// Run migrations against it |
| 38 | +const templateURL = new URL(rawUrl); |
| 39 | +templateURL.pathname = "badger_test_template"; |
| 40 | +await exec("npx -y prisma migrate deploy --schema ../prisma/schema.prisma", { |
| 41 | + env: { |
| 42 | + ...process.env, |
| 43 | + DATABASE_URL: templateURL.toString(), |
| 44 | + }, |
| 45 | + cwd: __dirname, |
| 46 | +}); |
| 47 | +// Disable connections |
| 48 | +await client.query( |
| 49 | + "UPDATE pg_database SET datallowconn = false WHERE datname = 'badger_test_template'", |
| 50 | +); |
| 51 | +await client.query( |
| 52 | + "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'badger_test_template' AND pid <> pg_backend_pid()", |
| 53 | +); |
| 54 | + |
| 55 | +// Now start our dev server. |
| 56 | +const app = Express(); |
| 57 | + |
| 58 | +const dbPool = new Set<string>(); |
| 59 | + |
| 60 | +function randomString() { |
| 61 | + return Math.round(Math.random() * 1e9).toString(36); |
| 62 | +} |
| 63 | + |
| 64 | +async function prepareDB() { |
| 65 | + const dbName = "badger_test_" + randomString(); |
| 66 | + await client.query(`CREATE DATABASE ${dbName} TEMPLATE badger_test_template`); |
| 67 | + dbPool.add(dbName); |
| 68 | + return dbName; |
| 69 | +} |
| 70 | + |
| 71 | +async function getPooledDBOrPrepare() { |
| 72 | + if (dbPool.size > 0) { |
| 73 | + const next = dbPool.values().next(); |
| 74 | + dbPool.delete(next.value); |
| 75 | + process.nextTick(async () => { |
| 76 | + dbPool.add(await prepareDB()); |
| 77 | + }); |
| 78 | + return next.value; |
| 79 | + } |
| 80 | + return await prepareDB(); |
| 81 | +} |
| 82 | + |
| 83 | +app.get("/healthz", async (_, res) => { |
| 84 | + res.json({ ok: true }); |
| 85 | +}); |
| 86 | + |
| 87 | +app.post("/db", async (req, res) => { |
| 88 | + const db = await getPooledDBOrPrepare(); |
| 89 | + const url = new URL(rawUrl); |
| 90 | + url.pathname = db; |
| 91 | + res.json({ db: db, url: url.toString() }); |
| 92 | +}); |
| 93 | + |
| 94 | +app.delete("/db/:db", async (req, res) => { |
| 95 | + await client.query(`DROP DATABASE ${req.params.db}`); |
| 96 | + res.json({ ok: true }); |
| 97 | +}); |
| 98 | + |
| 99 | +process.on("beforeExit", async () => { |
| 100 | + for (const db of dbPool) { |
| 101 | + await client.query(`DROP DATABASE ${db}`); |
| 102 | + } |
| 103 | + await client.query("DROP DATABASE badger_test_template"); |
| 104 | + await client.end(); |
| 105 | + console.log("Goodbye!"); |
| 106 | +}); |
| 107 | + |
| 108 | +const port = process.env.DB_DEV_SERVER_PORT |
| 109 | + ? parseInt(process.env.DB_DEV_SERVER_PORT, 10) |
| 110 | + : 5937; |
| 111 | +app.listen(port, () => { |
| 112 | + console.log(`DB server listening on http://localhost:${port}`); |
| 113 | +}); |
0 commit comments