# Data model (/docs/reference/data-model)



Decision Control uses **PostgreSQL** as its database engine and **Prisma** as its access layer.
The entire data model lives in a single file (`apps/api/prisma/schema.prisma`).

## Conventions [#conventions]

* **`id`** — every row uses a `cuid` (text), not an auto-incrementing number.
* **Dates** — `created_at` and `updated_at` are automatic.
* **Names** — tables and columns are in `snake_case` in the database (`project_id`); the code
  uses `camelCase` (`projectId`). Prisma translates.
* **Cascade delete** — when a workspace or a project is deleted, everything that depends on it
  goes too (`onDelete: Cascade`), except where stated otherwise.

## Soft delete and archive [#soft-delete-and-archive]

Deleting is **logical** for the assets people author and for the tenancy hierarchy. Those rows
carry a **`deleted_at`** timestamp (`decisions`, `decision_tables`, `datasets`, `projects`,
`workspaces`) and stay in the database; every read filters them out. `projects` and `workspaces`
also carry **`archived_at`**, which retires them without deleting anything.

A key or slug is still **unique**, so a deleted row is **renamed** on the way out
(`loan-approval` becomes `loan-approval__deleted__<stamp>`): the original key is free for reuse and
the lookup keeps its simple unique shape. Restoring renames it back, and is refused with a conflict
if the key was taken again.

## Enums [#enums]

These are stored as text with a fixed value.

| Enum                                    | Values                                                             | What they are for                |
| --------------------------------------- | ------------------------------------------------------------------ | -------------------------------- |
| `version_status`                        | `DRAFT`, `PUBLISHED`, `ARCHIVED`                                   | Decision / table version state.  |
| `outbox_status`                         | `PENDING`, `SENT`, `FAILED`                                        | Delivery state.                  |
| `execution_status`                      | `SUCCESS`, `FAILED`                                                | Overall result.                  |
| `execution_source`                      | `API`, `SIMULATE`                                                  | Where the execution came from.   |
| `workspace_role`                        | `OWNER`, `ADMIN`, `MEMBER`                                         | Workspace role.                  |
| `member_role` (formerly `project_role`) | `OWNER`, `ADMIN`, `EDITOR`, `VIEWER`                               | Project role.                    |
| `invitation_status`                     | `PENDING`, `ACCEPTED`, `REVOKED`, `EXPIRED`                        | Invitation state.                |
| `case_kind`                             | `ALERT`, `REVIEW`                                                  | Which action opened a case.      |
| `case_status`                           | `OPEN`, `IN_PROGRESS`, `RESOLVED`                                  | Working state.                   |
| `case_severity`                         | `LOW`, `MEDIUM`, `HIGH`, `CRITICAL`                                | Severity the case was born with. |
| `case_resolution`                       | `APPROVED`, `REJECTED`, `CONFIRMED`, `FALSE_POSITIVE`, `NO_ACTION` | Analyst's verdict.               |

## Identity and access [#identity-and-access]

| Table                   | Notable columns                                                                                                                               |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `users`                 | `email` (unique), `name`, `password_hash` (argon2id in Bun, scrypt in tests), `email_verified_at`.                                            |
| `sessions`              | `user_id` (FK, cascade), `token_hash`, `expires_at`, `last_used_at`. One row per active session — sign-out deletes it.                        |
| `workspaces`            | `slug` (readable, unique), `name`.                                                                                                            |
| `workspace_memberships` | `workspace_id`, `user_id`, `role`. Unique `(workspace_id, user_id)`.                                                                          |
| `project_memberships`   | `project_id`, `user_id`, `role`. Unique `(project_id, user_id)`. Access is **opt-in**: only members see a project.                            |
| `invitations`           | `workspace_id`, `project_id`, `email`, `role`, `token`, `status`, `invited_by_id`, `expires_at`, `accepted_at`. Unique `(project_id, email)`. |

## Project configuration [#project-configuration]

| Table              | Notable columns                                                                                                                                |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `projects`         | `workspace_id` (FK), `key` (unique per workspace), `name`, `execution_retention_days` (null follows the platform default; 0 keeps for ever).   |
| `api_keys`         | `project_id`, `name`, `prefix` (lookup), `key_hash`, `scopes` (text list, see API limits for the manage scope), `last_used_at`, `disabled_at`. |
| `credentials`      | `project_id`, `name` (unique), `type`, `secret_ciphertext`, `iv`, `auth_tag`. **Encrypted at rest (AES-256-GCM).**                             |
| `global_variables` | `project_id`, `name` (unique), `description`, `type`, `value` (JSON). Read in expressions as `global.<name>`. Pinned at publish.               |
| `connections`      | `project_id`, `name`, `kind`, `secret_ciphertext`, `iv`, `auth_tag`. Encrypted like credentials; summary (host, region) shown.                 |
| `webhooks`         | `project_id`, `url`, `events` (text list), `secret_ciphertext`, `iv`, `auth_tag`. Secret encrypted.                                            |

## Decisions and decision tables [#decisions-and-decision-tables]

The same pattern repeats: a **container** with an editable **draft** and an immutable list of
**versions**.

| Table                     | Notable columns                                                                                                                                                                                                   |
| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `decisions`               | `project_id`, `key` (unique per project), `name`, `description`, `revision` (optimistic concurrency), `draft` (JSON graph).                                                                                       |
| `decision_versions`       | `decision_id`, `version` (unique per decision), `status`, `label` (e.g. `stable`), `changelog`, `input_schema`, `output_schema`, `graph`, `refs` (pinned tables / sub-decisions), `published_by`, `published_at`. |
| `decision_tables`         | `project_id`, `key`, `name`, `description`, `revision`, `draft` (JSON table definition).                                                                                                                          |
| `decision_table_versions` | `table_id`, `version`, `status`, `label`, `changelog`, `definition` (rows, columns, hit policy, defaults).                                                                                                        |

## Tags [#tags]

| Table           | Notable columns                                                                                                                   |
| --------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `tags`          | `project_id`, `name` (unique per project), `color` (one of `neutral`, `blue`, `green`, `amber`, `red`, `violet` — a theme token). |
| `decision_tags` | `decision_id`, `tag_id`. A decision carries many tags and a tag groups many decisions; deleting either side drops only the link.  |

## Test cases [#test-cases]

| Table                 | Notable columns                                                                                                                                                                                  |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `decision_test_cases` | `decision_id`, `name`, `input` (JSON), `expected_status`, `expected_decision`, `expected_outputs` (JSON), `expected_actions` (text list), `expected_error_code`, `mocks` (JSON — `now`, `http`). |

## Datasets [#datasets]

| Table              | Notable columns                                                          |
| ------------------ | ------------------------------------------------------------------------ |
| `datasets`         | `project_id`, `key` (unique per project), `name`, `description`.         |
| `dataset_versions` | `dataset_id`, `version`, `definition` (rows), `published_at`. Immutable. |

## Environments and deployments [#environments-and-deployments]

| Table                        | Notable columns                                                                                                  |
| ---------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| `environments`               | `project_id`, `key` (unique per project: `development`, `staging`, `production`), `is_protected`.                |
| `deployments`                | `project_id`, `decision_id`, `environment_id`, `version`, `deployed_by`, `deployed_at`.                          |
| `experiments`                | `project_id`, `decision_id`, `environment_id`, `mode` (`split`/`shadow`), `challenger_version`, `routing_field`. |
| `environment_follows_latest` | `environment_id`, `decision_id` — when true, the environment runs whatever version is published.                 |

## Publish requests (four-eyes) [#publish-requests-four-eyes]

| Table                     | Notable columns                                                                                                            |
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `publish_requests`        | `decision_id`, `requested_by`, `status`, `requested_at`, `changelog`, `frozen_draft` (the draft the approver publishes).   |
| `publish_request_actions` | `request_id`, `actor_id`, `action` (`requested`/`approved`/`rejected`/`cancelled`), `comment`, `actor_name`, `created_at`. |

## Execution [#execution]

| Table              | Notable columns                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `executions`       | `public_id` (`DEC-XXXX`), `decision_id`, `decision_version_id` (null for simulated drafts), `version`, `status`, `source` (`API`/`SIMULATE`), `input`, `output`, `variables`, `reasons`, `actions`, `error`, `duration_ms`, `api_key_id` (set null on delete), `external_id` (the caller's own reference), `chain_seq`, `chain_hash`, `erased_at` (set when a data-subject erasure emptied the content). **Append-only** apart from erasure. |
| `action_outbox`    | `execution_id`, `kind`, `label`, `target`, `payload`, `status`, `attempts`, `max_attempts`, `next_attempt_at`, `last_error`, `sent_at`. Written in the same transaction as the execution.                                                                                                                                                                                                                                                    |
| `execution_traces` | `execution_public_id` (PK, FK to `executions.public_id`, cascade), `trace` (JSON). The step-by-step trace, kept apart because it is the largest field a run writes and no list reads it.                                                                                                                                                                                                                                                     |

## Rollups [#rollups]

| Table                                  | Notable columns                                                                                                                                                                                     |
| -------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `execution_rollups`                    | `project_id`, `decision_key`, `bucket` (hour), `total`, `failed`, `duration_buckets` (histogram), `max_duration_ms`, `last_execution_at`. Dashboard history; outlives the executions it summarises. |
| `execution_outcome_rollups`            | `decision_id`, `environment_key`, `version`, `outcome`, `bucket` (hour), `total`, `first_at`. What monitoring and drift read instead of scanning `executions`.                                      |
| `rollup_state`, `outcome_rollup_state` | A single row each: up to which hour the matching rollups are final.                                                                                                                                 |

## Audit [#audit]

| Table        | Notable columns                                                                                                                                                                                                   |
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `audit_logs` | `actor` (`user:email`, `api-key:<name>` or `seed`), `workspace_id`, `project_id` (loose references, not FKs), `entity_type`, `entity_id`, `action`, `diff` (JSON), `created_at`. Draft saves are **not** audited. |

## Cases (the inbox behind `createAlert` and `enqueueReview`) [#cases-the-inbox-behind-createalert-and-enqueuereview]

| Table         | Notable columns                                                                                                                                                                                                                                                                                                |
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cases`       | `public_id` (`CASE-XXXX`), `execution_id` (FK, cascade), `decision_key`, `version`, `environment_key`, `kind`, `status`, `severity`, `title`, `subject`, `payload`, `assignee_id`, `resolution`, `resolution_note`, `resolved_by`, `resolved_at`. Born from live executions (never simulations, never shadow). |
| `case_events` | `case_id` (FK, cascade), `type` (`opened`/`comment`/`assigned`/`status`/`severity`/`resolved`/`reopened`), `actor_id` (nullable, system or user), `actor_name` (kept), `body`, `data`, `created_at`.                                                                                                           |

## Features (feature store) [#features-feature-store]

| Table          | Notable columns                                                                                                             |
| -------------- | --------------------------------------------------------------------------------------------------------------------------- |
| `features`     | `project_id`, `name`, `description`. Series registry — observation rows can exist before it.                                |
| `observations` | `project_id`, `series`, `entity`, `value` (numeric), `attributes` (JSON), `occurred_at`, `execution_id`. One row per event. |

## Relationships [#relationships]

* A **workspace** has many **projects** and **memberships**.
* A **project** has decisions, tables, datasets, api_keys, credentials, connections, webhooks,
  global_variables, environments, experiments, executions, audit logs, cases, features, tags and
  memberships.
* A **decision** has many **versions** and many **executions**, and can carry many **tags**
  through `decision_tags`.
* A **decision_table** has many **decision_table_versions**.
* A **dataset** has many **dataset_versions**.
* An **execution** can generate several `action_outbox` rows and several **cases**.
* A **case** has many **case_events** (its timeline).
* A **publish_request** can produce a **version** when approved.

The full schema is the authoritative source — see `apps/api/prisma/schema.prisma` in the repo.
