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,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:
|
||||
|
||||
Reference in New Issue
Block a user