teqo/scripts/guard-dev-db.mjs
Francisco Ignácio Fontoura Solla 4806c417c8 Engineering Pass 3: consolidate patterns and lock them with guards.
Fold duplicated campanha access, loaders, list/filter/form shells, and scripts into shared modules; reorganize utilities by domain; stabilize e2e; and document prevention-first audit workflow so the same smells cannot creep back in.
2026-07-29 06:11:29 -03:00

39 lines
1.7 KiB
JavaScript

/**
* Refuses to start the local dev server against a non-local database.
*
* Local development must use the Postgres from docker-compose. Pointing dev at
* the production Neon database (as .env / .env.local currently do) risks editing
* or deleting real data through the admin panel. This guard fails closed.
*
* Escape hatch (use with care): ALLOW_REMOTE_DB=true pnpm dev
*/
import { assertLocalDatabase } from './assert-local-database.mjs'
import { diagnoseDatabaseTarget, warnOnSharedDataVolumes } from './db-doctor.mjs'
import { isRemoteDbOverrideSet, loadCliEnv } from './lib/cli.mjs'
// Mirror Next.js precedence (.env.local wins over .env) WITHOUT overriding a
// DATABASE_URL that is already set in the real environment (e.g. injected by
// Playwright's webServer). dotenv's default `override: false` guarantees this.
loadCliEnv()
assertLocalDatabase(
'guard-dev-db',
'This is almost certainly the production or a shared database.\n\n' +
'Local development should use the local Postgres from docker-compose:\n' +
' 1. pnpm db:start\n' +
' 2. set DATABASE_URL=postgresql://teqo:teqo@localhost:5432/teqo in .env.local',
)
// Connectivity preflight: fail here with a named remedy (which container to
// start, who is holding the port) instead of letting Next/Payload throw a
// bare ECONNREFUSED minutes into the dev session. Skipped under the same
// escape hatch as the locality guard — remote databases are on their own.
if (!isRemoteDbOverrideSet()) {
const volumesClean = warnOnSharedDataVolumes()
const healthy = await diagnoseDatabaseTarget({
label: 'guard-dev-db',
databaseUrl: process.env.DATABASE_URL,
})
if (!healthy || !volumesClean) process.exit(1)
}