|
| 1 | +import { execFileSync } from 'node:child_process'; |
| 2 | +import { randomUUID } from 'node:crypto'; |
| 3 | +import dotenv from 'dotenv'; |
| 4 | +import { Pool } from 'pg'; |
| 5 | + |
| 6 | +const pnpmCommand = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm'; |
| 7 | + |
| 8 | +dotenv.config(); |
| 9 | + |
| 10 | +function resolveDirectDatabaseUrl() { |
| 11 | + const candidate = process.env.DIRECT_DATABASE_URL ?? process.env.DIRECT_URL; |
| 12 | + |
| 13 | + if (!candidate) { |
| 14 | + throw new Error('Set DIRECT_DATABASE_URL or legacy DIRECT_URL in your shell or .env before pushing.'); |
| 15 | + } |
| 16 | + |
| 17 | + if (candidate.startsWith('prisma://') || candidate.startsWith('prisma+postgres://')) { |
| 18 | + throw new Error('Pre-push checks require DIRECT_DATABASE_URL or DIRECT_URL to point at direct PostgreSQL, not Prisma Accelerate.'); |
| 19 | + } |
| 20 | + |
| 21 | + return new URL(candidate); |
| 22 | +} |
| 23 | + |
| 24 | +function isLocalDatabaseHost(hostname: string) { |
| 25 | + return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1'; |
| 26 | +} |
| 27 | + |
| 28 | +function tempDatabaseUrl(baseUrl: URL, databaseName: string) { |
| 29 | + const next = new URL(baseUrl.toString()); |
| 30 | + next.pathname = `/${databaseName}`; |
| 31 | + |
| 32 | + return next.toString(); |
| 33 | +} |
| 34 | + |
| 35 | +function adminDatabaseUrl(baseUrl: URL) { |
| 36 | + const next = new URL(baseUrl.toString()); |
| 37 | + next.pathname = '/postgres'; |
| 38 | + |
| 39 | + return next.toString(); |
| 40 | +} |
| 41 | + |
| 42 | +function quoteIdentifier(value: string) { |
| 43 | + return `"${value.replaceAll('"', '""')}"`; |
| 44 | +} |
| 45 | + |
| 46 | +function toError(error: unknown) { |
| 47 | + if (error instanceof Error) { |
| 48 | + return error; |
| 49 | + } |
| 50 | + |
| 51 | + return new Error(String(error)); |
| 52 | +} |
| 53 | + |
| 54 | +function runPnpm(args: string[], env: NodeJS.ProcessEnv) { |
| 55 | + execFileSync(pnpmCommand, args, { |
| 56 | + env, |
| 57 | + stdio: 'inherit', |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +async function dropTemporaryDatabase(pool: Pool, databaseName: string) { |
| 62 | + await pool.query( |
| 63 | + `SELECT pg_terminate_backend(pid) |
| 64 | + FROM pg_stat_activity |
| 65 | + WHERE datname = $1 |
| 66 | + AND pid <> pg_backend_pid()`, |
| 67 | + [databaseName], |
| 68 | + ); |
| 69 | + await pool.query(`DROP DATABASE IF EXISTS ${quoteIdentifier(databaseName)}`); |
| 70 | +} |
| 71 | + |
| 72 | +async function main() { |
| 73 | + const directUrl = resolveDirectDatabaseUrl(); |
| 74 | + |
| 75 | + if (!isLocalDatabaseHost(directUrl.hostname) && process.env.ALLOW_REMOTE_PREPUSH_DB !== '1') { |
| 76 | + throw new Error('Pre-push checks refuse to use non-local databases by default. Set ALLOW_REMOTE_PREPUSH_DB=1 if you really want that.'); |
| 77 | + } |
| 78 | + |
| 79 | + const originalDatabase = directUrl.pathname.replace(/^\//, '') || 'locations_api'; |
| 80 | + const tempDatabaseName = `${originalDatabase}_prepush_${randomUUID().replace(/-/g, '').slice(0, 8)}`; |
| 81 | + const isolatedDatabaseUrl = tempDatabaseUrl(directUrl, tempDatabaseName); |
| 82 | + const adminPool = new Pool({ |
| 83 | + connectionString: adminDatabaseUrl(directUrl), |
| 84 | + }); |
| 85 | + |
| 86 | + let primaryError: unknown; |
| 87 | + |
| 88 | + try { |
| 89 | + await adminPool.query(`CREATE DATABASE ${quoteIdentifier(tempDatabaseName)}`); |
| 90 | + |
| 91 | + runPnpm(['test:ci'], { |
| 92 | + ...process.env, |
| 93 | + DATABASE_URL: isolatedDatabaseUrl, |
| 94 | + DIRECT_DATABASE_URL: isolatedDatabaseUrl, |
| 95 | + NODE_ENV: 'test', |
| 96 | + }); |
| 97 | + } catch (error) { |
| 98 | + primaryError = error; |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + await dropTemporaryDatabase(adminPool, tempDatabaseName); |
| 103 | + } catch (cleanupError) { |
| 104 | + if (primaryError) { |
| 105 | + console.error('Failed to drop temporary pre-push database after the primary failure.'); |
| 106 | + console.error(cleanupError); |
| 107 | + } else { |
| 108 | + throw cleanupError; |
| 109 | + } |
| 110 | + } finally { |
| 111 | + await adminPool.end(); |
| 112 | + } |
| 113 | + |
| 114 | + if (primaryError) { |
| 115 | + throw toError(primaryError); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +await main(); |
0 commit comments