Prisma Integration#
Overview#
Cedar integrates Prisma as its database layer for API-side services. The most operationally significant part of this integration is schema synchronization during test setup: before each test run, Cedar pushes your Prisma schema to the test database so it matches the current state of schema.prisma. This sync runs automatically via Jest's globalSetup or Vitest's custom environment, and is controlled by several environment variables.
Schema Sync During Test Setup#
When you run cedar test, the following happens before tests execute:
-
cedar testCLI handler setsDATABASE_URLtoTEST_DATABASE_URL(if set) or falls back to a SQLite file at.cedar/test.db. It also callswarnIfNonStandardDatasourceUrl()when theapiside is included . -
Jest globalSetup (
packages/testing/src/config/jest/api/globalSetup.ts) or the Vitest custom environment (CedarApiVitestEnv.ts) runs the Prisma schema sync command:- Default strategy:
prisma db push --force-reset --accept-data-loss resetstrategy (whenTEST_DATABASE_STRATEGY=reset):prisma migrate reset --force
- Default strategy:
Controlling Schema Sync#
| Environment Variable | Effect |
|---|---|
TEST_DATABASE_URL | Overrides the test database URL. Required for non-SQLite schemas. |
TEST_DATABASE_STRATEGY | Set to reset to use migrate reset instead of db push. |
SKIP_DB_PUSH | Set to 1 to skip schema sync entirely (also set by --no-db-push CLI flag). |
TEST_DIRECT_URL | Override for directUrl during testing (Prisma v6+). |
The --no-db-push flag sets SKIP_DB_PUSH=1 via the CLI handler . Both the Jest globalSetup and Vitest environment respect this flag.
Datasource Warning (datasourceWarning.ts)#
packages/cli/src/commands/test/datasourceWarning.ts exports warnIfNonStandardDatasourceUrl(), which runs before Jest/Vitest starts (while the process still has an interactive TTY).
It parses the Prisma config file for a url: env(...) line . If the datasource URL env var is anything other than DATABASE_URL, it warns the user and prompts for confirmation β since Cedar only overrides DATABASE_URL (and optionally DIRECT_DATABASE_URL) during testing, a custom name risks running destructive commands against a non-test database .
Pass --force to suppress the interactive prompt .
Known Limitation: No Provider/URL Protocol Validation#
There is no validation that the Prisma schema provider matches the resolved DATABASE_URL protocol.
The classic failure mode (tracked in issue #2284):
schema.prismadeclaresprovider = "postgresql"TEST_DATABASE_URLis unset in.env- The CLI falls back to
file:.cedar/test.db(a SQLite path) prisma db pushis invoked with a SQLite URL against a PostgreSQL schema- Result: the process hangs indefinitely with zero output β no error, no progress indicator
This is especially problematic for non-interactive/agent contexts, where there is no way to distinguish "still working" from "hung forever" without an external timeout.
Workaround: Always set TEST_DATABASE_URL explicitly in .env when your schema uses any provider other than sqlite.
The fix suggested in #2284 is to detect the mismatch upfront β comparing config.datasources[0].provider against the URL scheme of the resolved test database URL β and fail fast with an actionable message before invoking Prisma .
Key Source Files#
| File | Purpose |
|---|---|
packages/testing/src/config/jest/api/globalSetup.ts | Jest global setup: schema sync before all tests |
packages/testing/src/api/vitest/CedarApiVitestEnv.ts | Vitest custom environment: schema sync before all tests |
packages/cli/src/commands/test/testHandler.ts | Jest test handler: sets env vars, invokes datasource warning |
packages/cli/src/commands/test/datasourceWarning.ts | Warns about non-standard datasource URL env var names |