agentleFS
sordi-aisordi-ai/skill-everything/.cursor/rules/db-schema.mdc

skill-everything / rules

Cursor rule20 starsChanged 7 months agoMITView on GitHub

Apply when designing database schemas, writing migrations, or reviewing table structure. Covers naming, keys, indexes, constraints, nullability, and migration safety.

The file

Show its rules and text

We split this db-schema.mdc into the rules it gives an agent

20 rules. About 0 are common, 3 are distinctive, and 17 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 17

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Snake case everywhere: Always use snake_case for table names, column names, and index names.

    line 17
  2. Very specific:

    Plural table names: Always name tables in the plural (users, orders, line_items).

    line 18
  3. Very specific:

    Consistent FK column naming: Always name foreign-key columns <referenced_table_singular>_id (e.g., user_id, order_id) so the reference is …

    line 19
  4. Very specific:

    Surrogate primary key: Always define an explicit surrogate primary key (id) on every table.

    line 23
  5. Very specific:

    UUID vs serial: Prefer uuid PKs for distributed systems or public-facing IDs.

    line 24
  6. Very specific:

    Declare FK constraints: Always declare FOREIGN KEY constraints in the schema.

    line 28
  7. Very specific:

    Explicit ON DELETE behaviour: Always specify ON DELETE behaviour (CASCADE, SET NULL, or RESTRICT) on every FK.

    line 29
  8. Distinctive:

    Missing FK is a data-integrity bug: Never ship a column that references another table without a corresponding FK constraint — orphaned rows …

    line 30
  9. Distinctive:

    Index every FK column: Always add an index on every foreign-key column.

    line 34
  10. Very specific:

    Covering indexes for hot queries: Use covering indexes (include all columns in the SELECT) for read-heavy queries rather than adding …

    line 35
  11. Distinctive:

    Unique constraints at DB level: Always enforce uniqueness at the database level rather than relying on application-layer deduplication.

    line 36
  12. Very specific:

    Explicit NOT NULL: Always mark columns NOT NULL unless NULL carries distinct semantic meaning (unknown vs.

    line 40
  13. Very specific:

    Meaningful defaults: Always supply a DEFAULT for columns that have a sensible zero-value (0, '', false, now()).

    line 41
  14. Very specific:

    Standard audit columns: Always include created_at TIMESTAMPTZ NOT NULL DEFAULT now() and updated_at TIMESTAMPTZ NOT NULL DEFAULT now() on …

    line 45
  15. Very specific:

    Soft delete with deleted_at: Prefer deleted_at TIMESTAMPTZ over hard deletes for user-facing entities.

    line 46
  16. Very specific:

    DB-native enum or check constraint: Always enforce enum-like columns with a CHECK constraint or a native ENUM type.

    line 50
  17. Very specific:

    Up and down migrations: Always write both an up and a down migration.

    line 54
  18. Very specific:

    Idempotent migrations: Always guard DDL statements with IF NOT EXISTS / IF EXISTS so re-running a migration does not error.

    line 55
  19. Very specific:

    Non-blocking index creation: Use CREATE INDEX CONCURRENTLY (Postgres) or equivalent for indexes on large tables.

    line 56
  20. Very specific:

    Check constraints for invariants: Always encode business invariants (e.g., price > 0, quantity >= 0) as CHECK constraints.

    line 60

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
db-schema.mdc
---
description: Apply when designing database schemas, writing migrations, or reviewing table structure. Covers naming, keys, indexes, constraints, nullability, and migration safety.
alwaysApply: false
---

# Sub-Skill: Database Schema Design
<!-- target: tokens_target above. Run `python tools/render_readme_table.py` to update README. -->

**Purpose:** Prevent data-integrity bugs, race conditions, and irreversible migrations by enforcing database-level constraints and safe migration patterns before code reaches production.

---

## Rules

### Naming Conventions

1. **Snake case everywhere.** Always use `snake_case` for table names, column names, and index names; never use camelCase or PascalCase in SQL identifiers.
2. **Plural table names.** Always name tables in the plural (`users`, `orders`, `line_items`); singular names cause inconsistency across ORMs and query builders.
3. **Consistent FK column naming.** Always name foreign-key columns `<referenced_table_singular>_id` (e.g., `user_id`, `order_id`) so the reference is self-documenting.

### Primary Keys

4. **Surrogate primary key.** Always define an explicit surrogate primary key (`id`) on every table; never rely on natural keys as the sole PK unless the domain guarantees immutability.
5. **UUID vs serial.** Prefer `uuid` PKs for distributed systems or public-facing IDs; prefer `bigserial` / `bigint GENERATED ALWAYS AS IDENTITY` for high-insert internal tables where sort order matters.

### Foreign Keys & Referential Integrity

6. **Declare FK constraints.** Always declare `FOREIGN KEY` constraints in the schema; never rely on application-layer join logic to enforce referential integrity.
7. **Explicit ON DELETE behaviour.** Always specify `ON DELETE` behaviour (`CASCADE`, `SET NULL`, or `RESTRICT`) on every FK; omitting it defaults to `RESTRICT`, which can cause surprising errors at runtime.
8. **Missing FK is a data-integrity bug.** Never ship a column that references another table without a corresponding FK constraint — orphaned rows accumulate silently. Reference: ERR-2026-022

### Indexes

9. **Index every FK column.** Always add an index on every foreign-key column; unindexed FKs cause full-table scans on joins and cascade operations.
10. **Covering indexes for hot queries.** Use covering indexes (include all columns in the `SELECT`) for read-heavy queries rather than adding redundant columns to the base table.
11. **Unique constraints at DB level.** Always enforce uniqueness at the database level rather than relying on application-layer deduplication; race conditions between concurrent writes bypass app checks. Reference: ERR-2026-022

### Nullability & Defaults

12. **Explicit NOT NULL.** Always mark columns `NOT NULL` unless `NULL` carries distinct semantic meaning (unknown vs. absent); nullable columns complicate query logic and ORM mapping.
13. **Meaningful defaults.** Always supply a `DEFAULT` for columns that have a sensible zero-value (`0`, `''`, `false`, `now()`); avoid forcing callers to supply values the DB can compute.

### Timestamps & Soft Delete

14. **Standard audit columns.** Always include `created_at TIMESTAMPTZ NOT NULL DEFAULT now()` and `updated_at TIMESTAMPTZ NOT NULL DEFAULT now()` on every table; omitting them makes debugging and auditing impossible.
15. **Soft delete with deleted_at.** Prefer `deleted_at TIMESTAMPTZ` over hard deletes for user-facing entities; ensure all queries filter `WHERE deleted_at IS NULL` or use a view/partial index.

### Enum Columns

16. **DB-native enum or check constraint.** Always enforce enum-like columns with a `CHECK` constraint or a native `ENUM` type; never store unconstrained strings and validate only in the application.

### Migrations

17. **Up and down migrations.** Always write both an `up` and a `down` migration; a rollback path is mandatory before any migration reaches production.
18. **Idempotent migrations.** Always guard DDL statements with `IF NOT EXISTS` / `IF EXISTS` so re-running a migration does not error; this is required for blue-green and multi-replica deployments.
19. **Non-blocking index creation.** Use `CREATE INDEX CONCURRENTLY` (Postgres) or equivalent for indexes on large tables; blocking index creation causes downtime.

### Constraints Over App Logic

20. **Check constraints for invariants.** Always encode business invariants (e.g., `price > 0`, `quantity >= 0`) as `CHECK` constraints; application-layer validation is bypassed by direct DB writes, scripts, and migrations.

---

## See also

- `skills/code-quality/SKILL.md` — general code-quality rules that apply to migration scripts
- `skills/review-deployment/SKILL.md` — deployment checklist that includes migration safety gates

---

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.