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:
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
"dev": "next dev",
|
"dev": "next dev",
|
||||||
"build": "next build",
|
"build": "next build",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"test": "vitest run",
|
"test": "DATABASE_URL=postgresql://lyra:lyra@localhost:5432/lyra_test vitest run",
|
||||||
"postinstall": "prisma generate",
|
"postinstall": "prisma generate",
|
||||||
"db:migrate": "prisma migrate deploy",
|
"db:migrate": "prisma migrate deploy",
|
||||||
"db:dev": "prisma migrate dev"
|
"db:dev": "prisma migrate dev"
|
||||||
|
|||||||
@@ -2,6 +2,13 @@ import { beforeEach } from "vitest";
|
|||||||
import { prisma } from "@/lib/db";
|
import { prisma } from "@/lib/db";
|
||||||
|
|
||||||
beforeEach(async () => {
|
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.
|
// Job cascades from Request; Request.monitoredReleaseId is SetNull. Delete children first.
|
||||||
await prisma.job.deleteMany();
|
await prisma.job.deleteMany();
|
||||||
await prisma.request.deleteMany();
|
await prisma.request.deleteMany();
|
||||||
|
|||||||
@@ -1,12 +1,24 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
os.environ.setdefault("DATABASE_URL", "postgresql://lyra:lyra@localhost:5432/lyra_test")
|
||||||
|
|
||||||
import psycopg
|
import psycopg
|
||||||
import pytest
|
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()
|
@pytest.fixture()
|
||||||
def conn():
|
def conn():
|
||||||
dsn = os.environ["DATABASE_URL"]
|
dsn = os.environ["DATABASE_URL"]
|
||||||
|
_require_test_db(dsn)
|
||||||
connection = psycopg.connect(dsn)
|
connection = psycopg.connect(dsn)
|
||||||
# Clean up any pre-existing rows before the test runs (Job cascades from Request).
|
# Clean up any pre-existing rows before the test runs (Job cascades from Request).
|
||||||
with connection.cursor() as cur:
|
with connection.cursor() as cur:
|
||||||
|
|||||||
Reference in New Issue
Block a user