Conventions db

From stella/stella · View interactive version

Conventions db is an AI agent skill from the GitHub repository stella/stella. Apply when writing or modifying database schema, queries, migrations, transactions, or tenant-scoped persistence.

How to use this skill

Skill file

Database Conventions

Apply when writing or modifying database schema, queries, migrations,

transactions, or tenant-scoped persistence.

Schema

Schema lives under apps/api/src/db/schema/; read the owning slice and related

foreign keys before editing. To learn whether something is stored and where,

search the generated index first: rg <word> apps/api/src/db/schema-index gives

one line per column with its builder, flags, file:line and the first sentence

of its comment, so lead a column's comment with what the column means.

PostgreSQL enum only when the value set is genuinely permanent. [review]

shared file references. Trace the full FK graph before multi-resource

deletion. [review]

composite indexes with the tenant-scoping columns. Treat changes to large

tables as lock-sensitive. [review]

Migrations

Expand-only pre-deploy phase

SQL migrations are a blocking pre-deploy phase, so keep them expand-only: add

tables or columns, add constraints as NOT VALID, and use only short-lock DDL.

Never put data backfills, CREATE [UNIQUE] INDEX CONCURRENTLY, or

ALTER TABLE ... VALIDATE CONSTRAINT in a migration. Governed, bounded

backfills belong to apps/api/src/db/backfill-runtime.ts and registered online

repairs and concurrent index builds (ONLINE_MIGRATION_INDEXES) in

apps/api/src/db/online-migrations.ts.

[check: scripts/check-migration-safety.ts, .github/workflows/ci.yml]

and deployment. db:push is a local declarative schema-diff tool; it does

not replace committed migrations and must not be described as the deployment

path. [check: .github/workflows/ci.yml]

reads/writes, backfill in bounded batches, switch, then remove the old shape

in a later release. [check: scripts/check-migration-safety.ts]

compatible with the migrated schema, and a failed rollout must have a safe

forward-fix path. [review]

application changes. Destructive and access-control SQL requires a

statement-scoped acknowledgement enforced by

scripts/check-migration-safety.ts, placed in the comment block directly

above the statement:

-- stella-migration-safety: reviewed <rule-id> - <why this is safe>.

An acknowledgement that clears nothing is an error. Every migration sets

lock_timeout and statement_timeout first.

[check: scripts/check-migration-safety.ts, scripts/check-migration-locks.ts]

schema migrations and checkpoint durably.

[test: apps/api/src/db/online-migrations.test.ts]

directory whose name the database has not recorded, in name order. Never

rename or delete a merged migration; add a new one instead.

[check: scripts/check-migration-order.ts]

fresh database, then confirm

bun --filter @stll/api db:push -- --explain reports no schema drift. Do not

repair drift by resetting a shared database.

[check: .github/workflows/ci.yml]

Tenant Scope and Queries

Prefer Drizzle's relational query API for ordinary relation reads, and SQL-like

syntax where it states the invariant more directly (cross-table filters,

aggregation, locking, unions, mutations).

PostgreSQL RLS and query-level scope reinforce each other. Raw/root database

access needs a demonstrated system-level reason and a deny-by-default RLS

posture. [check: ratchet:direct-root-connection-imports]

columns, then narrow structured content with a type guard rather than a cast.

[review]

Concurrency and Transactions

I/O outside them. [review]

SELECT ... FOR UPDATE, or encode the expected state/version in the

mutation WHERE clause and check the affected-row count. [review]

transitions, and idempotency keys are stronger than read-before-insert

checks. [review]

locked, define and reuse a deterministic ordering to avoid deadlocks.

[review]

Verification

Test behavior that schema inference cannot prove: cross-tenant denial,

concurrent transitions, replay/idempotence, migration parity, destructive

delete ordering, and cursor stability under inserts. Prefer invariant or

integration tests over mocked query-shape tests.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.