fix: guard test cleanup to a dedicated lyra_test DB (never wipe live data)

Test suites were running DELETE FROM.../deleteMany() against whatever
DATABASE_URL pointed to, which was the live app database, wiping user
credentials and library. Add a guard in both the worker conftest and
web test setup that raises before any cleanup unless the target DB
name ends in '_test'. Default DATABASE_URL to lyra_test when unset in
the worker, and make `npm test` target lyra_test explicitly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan
2026-07-11 16:16:12 +02:00
parent 88ef4149d9
commit f59f972c1b
3 changed files with 20 additions and 1 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"test": "vitest run",
"test": "DATABASE_URL=postgresql://lyra:lyra@localhost:5432/lyra_test vitest run",
"postinstall": "prisma generate",
"db:migrate": "prisma migrate deploy",
"db:dev": "prisma migrate dev"
+7
View File
@@ -2,6 +2,13 @@ import { beforeEach } from "vitest";
import { prisma } from "@/lib/db";
beforeEach(async () => {
const dbName = (process.env.DATABASE_URL ?? "").split("/").pop()?.split("?")[0] ?? "";
if (!dbName.endsWith("_test")) {
throw new Error(
`refusing to run test cleanup against non-test database '${dbName}'. ` +
"Set DATABASE_URL to a database whose name ends in '_test' (e.g. lyra_test).",
);
}
// Job cascades from Request; Request.monitoredReleaseId is SetNull. Delete children first.
await prisma.job.deleteMany();
await prisma.request.deleteMany();
+12
View File
@@ -1,12 +1,24 @@
import os
os.environ.setdefault("DATABASE_URL", "postgresql://lyra:lyra@localhost:5432/lyra_test")
import psycopg
import pytest
def _require_test_db(dsn: str) -> None:
name = dsn.rsplit("/", 1)[-1].split("?")[0]
if not name.endswith("_test"):
raise RuntimeError(
f"refusing to run destructive test fixtures against non-test database '{name}'. "
"Point DATABASE_URL at a database whose name ends in '_test' (e.g. lyra_test)."
)
@pytest.fixture()
def conn():
dsn = os.environ["DATABASE_URL"]
_require_test_db(dsn)
connection = psycopg.connect(dsn)
# Clean up any pre-existing rows before the test runs (Job cascades from Request).
with connection.cursor() as cur: