Sync Code Schema from DB (DB SSOT)
Purpose
Treat the real database as the schema Single Source of Truth (SSOT) and keep repo-local schema artifacts in sync so developers and LLMs can work without direct DB access.
The sync-code-schema-from-db skill is the inverse of sync-db-schema-from-code. Use the skill when DB → code is the authoritative direction.
Hard precondition (SSOT mode gate)
Use the skill only when the project DB SSOT is database:
- Source of truth: the running database
- Repo contains mirrors:
prisma/schema.prisma(derived via introspection)db/schema/tables.json(derived snapshot for LLMs)
To check the mode, read:
docs/project/db-ssot.json
If the project uses repo-prisma SSOT, STOP and use sync-db-schema-from-code.
When to use
Use when:
- the database already exists and is authoritative
- schema changes are applied outside the repo (DBA workflow, managed platform, legacy DB)
- you need to regenerate Prisma schema and repo mirrors after DB changes
- you need the LLM to learn the latest DB shape
Avoid when:
- you want to propose new persisted fields by changing repo schema (use DB handbook + human-first DB change)
- you want migrations to be the authoritative change mechanism (that is repo-prisma mode)
See ./reference/database-ssot-mechanism.md for the end-to-end pattern (including domain/repository boundaries).
Inputs
- Target environment (
dev/staging/prod) and how to connect (without exposing secrets) - Whether
prisma/schema.prismais already present - Whether the DB mirror assets are present (
db/schema/tables.json,db/handbook/, etc)
Outputs (evidence)
Choose one evidence directory (no secrets):
dev-docs/active/<task-slug>/artifacts/db/(recommended).ai/.tmp/db-sync/<run-id>/
Evidence files:
00-ssot-mode-check.md01-db-pull-instructions.md02-import-prisma-log.md03-context-refresh-log.md
Steps
Phase 0 — Confirm mode and scope
- Confirm intent is DB → code.
- Confirm DB SSOT mode is
database(docs/project/db-ssot.json).- If not, STOP and route to
sync-db-schema-from-code.
- If not, STOP and route to
- Confirm the target environment and dialect.
- Choose evidence directory.
Phase A — Pull schema from DB into Prisma (human-run)
- Provide human-safe instructions (no secrets in chat) to run introspection:
- Typical:
npx prisma db pull
Record the instructions and the environment assumptions in 01-db-pull-instructions.md.
Phase B — Import Prisma schema into repo mirror (LLM-safe)
Ensure the DB mirror assets are present (required for the mirror workflow):
- Required paths:
.ai/skills/features/database/sync-code-schema-from-db/scripts/ctl-db.mjsdb/schema/tables.json
- If
db/schema/tables.jsonis missing, install the mirror skeleton:- Copy templates from
.ai/skills/features/database/sync-code-schema-from-db/templates/into the repo root. - Run
node .ai/skills/features/database/sync-code-schema-from-db/scripts/ctl-db.mjs init, then re-run Phase B.
- Copy templates from
- If
.ai/skills/features/database/sync-code-schema-from-db/scripts/ctl-db.mjsis missing, the database feature skill is not installed in.ai/skills/. Restore it (e.g., revert deletion / copy from a fresh template checkout), then re-run Phase B.
- Required paths:
Import
prisma/schema.prismaintodb/schema/tables.json:
node .ai/skills/features/database/sync-code-schema-from-db/scripts/ctl-db.mjs import-prisma
Record output in 02-import-prisma-log.md.
Phase C — Refresh LLM DB context contract
- Regenerate
docs/context/db/schema.json:
node .ai/scripts/ctl-db-ssot.mjs sync-to-context
Record output in 03-context-refresh-log.md.
Phase D — Keep developer layers coherent
- Update repository mappings and domain entities to match the mirrored schema:
- Repositories must return domain entities (not Prisma client types).
- Business layer must remain Prisma-free.
Verification
- SSOT mode is
database - Human ran
prisma db pullagainst the correct environment -
ctl-db import-prismaupdateddb/schema/tables.json -
ctl-db-ssot sync-to-contextupdateddocs/context/db/schema.json - Domain/repository mapping updated (no Prisma types in business layer)
- Public central test suite passes:
node .ai/tests/run.mjs --suite database
Boundaries
- MUST NOT treat
prisma/schema.prismaas SSOT in database mode - MUST NOT hand-edit
db/schema/tables.json(generated snapshot) - MUST NOT request or log credentials
- SHOULD document desired-state proposals in
db/handbook/before asking humans to change DB
Converted and distributed by TomeVault — claim your Tome and manage your conversions.