# Test database is separate from dev database

- **Pattern:** `ab-000308` (`ai-slop-test-theater.test-operability.test-database-isolated-from-dev`)
- **Severity:** info
- **Lifecycle:** active
- **Last modified:** 2026-04-18
- **Canonical URL:** https://auditbuffet.com/patterns/ab-000308
- **License:** CC-BY-4.0 — attribute to AuditBuffet Pattern Catalog (https://auditbuffet.com/patterns/ab-000308)

## Why it matters

When the test and development database URLs are identical, every test run that inserts, updates, or deletes records modifies the database you actively develop against. Count-based assertions drift over runs. A `deleteMany` in a cleanup hook that lacks a proper `where` clause can truncate tables you were relying on. More commonly, accumulating test records cause unique-constraint failures mid-development that look like bugs in new code. ISO-25010:2011 recoverability requires that test execution be reversible and isolated.

## Severity rationale

Info because sharing dev and test DB is typically caught before catastrophic data loss, but it is a persistent source of confusion and intermittent failures.

## Remediation

Ensure the database name suffix in `.env.test` differs from the one in `.env` or `.env.example`. Load `.env.test` explicitly in your test runner so there is no path through which tests can reach the dev database:

```bash
# .env (development)
DATABASE_URL=postgresql://user:pass@localhost:5432/myapp_dev

# .env.test (test — different DB name)
DATABASE_URL=postgresql://user:pass@localhost:5432/myapp_test
```

```json
// package.json
{
  "scripts": {
    "test": "dotenv -e .env.test -- vitest run"
  }
}
```

Confirm isolation by running `npx tsx -e "console.log(process.env.DATABASE_URL)"` before and after loading the test env.

## Detection

- **ID:** `test-database-isolated-from-dev`
- **Severity:** `info`
- **What to look for:** When `.env.test` exists OR a test-specific `DATABASE_URL` is set in test config, verify the value is different from the value in `.env.example`/`.env`. Count all database URL declarations across the env files and compare the database name/path components. EXCLUDE projects with no database configuration at all.
- **Pass criteria:** Test database URL is distinct from dev database URL. Report: "Test DB: [name], dev DB: [name], isolated: yes."
- **Fail criteria:** Test database URL equals dev database URL.
- **Skip (N/A) when:** No `.env.test` exists OR no test-specific DB config exists.
- **Detail on fail:** `"Test DB and dev DB use the same DATABASE_URL — tests will pollute the dev database"`
- **Remediation:** Use a different database name for tests so they don't share state with development:

  ```bash
  # .env (development)
  DATABASE_URL=postgresql://user:pass@localhost:5432/myapp_dev

  # .env.test (test)
  DATABASE_URL=postgresql://user:pass@localhost:5432/myapp_test
  ```

  Then in `package.json` scripts:

  ```json
  {
    "scripts": {
      "test": "dotenv -e .env.test -- vitest run"
    }
  }
  ```

---

## External references

- iso-25010 reliability.recoverability

Taxons: data-integrity, operational-readiness

HTML version: https://auditbuffet.com/patterns/ab-000308
