Claude-skill-registry database-schema
Enforces project database schema conventions when creating or modifying Drizzle ORM table definitions, including constraints, indexes, relations, and column patterns. This skill should be used proactively whenever working with schema files to ensure consistent schema design for PostgreSQL with Neon serverless.
git clone https://github.com/majiayu000/claude-skill-registry
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/database-schema" ~/.claude/skills/majiayu000-claude-skill-registry-database-schema && rm -rf "$T"
skills/data/database-schema/SKILL.mdDatabase Schema Conventions Enforcer
Purpose
This skill enforces the project database schema conventions automatically during schema development. It ensures consistent patterns for table definitions, constraints, indexes, foreign keys, and column naming in Drizzle ORM with PostgreSQL (Neon serverless).
When to Use This Skill
Use this skill proactively in the following scenarios:
- Creating new schema files in
src/lib/db/schema/ - Modifying existing table definitions
- Adding indexes or constraints
- Defining foreign key relationships
- Working with Drizzle ORM schema syntax (
)drizzle-orm/pg-core - Reviewing or fixing schema issues
- Any task involving database structure changes
Important: This skill should activate automatically without explicit user request whenever schema work is detected.
How to Use This Skill
1. Load Conventions Reference
Before creating or modifying any schema, load the complete conventions document:
Read references/Database-Schema-Conventions.md
This reference contains the authoritative schema standards including:
- Table definition templates
- Column conventions and patterns
- Foreign key relationship rules
- Check constraint patterns
- Index strategy (single, composite, covering, GIN)
- Junction table patterns
- Column naming conventions
- Constants usage
2. Apply Conventions During Development
When writing schema code, ensure strict adherence to all conventions:
Table Structure:
- Use
with constraints/indexes in the callback functionpgTable - Order columns alphabetically within the column definition object
- Always include standard columns:
,id
,createdAtupdatedAt - Include soft delete column when applicable:
(timestamp, null = not deleted)deletedAt
Column Patterns:
- UUID primary keys with
(never auto-increment)defaultRandom() - Use
constants for all varchar lengthsSCHEMA_LIMITS - Use
constants for default valuesDEFAULTS - Use
with explicit length (nevervarchar
)text - Timestamps with
defaultNow().notNull()
Foreign Key Rules:
- when parent owns child (delete children with parent)cascade
- for optional relationshipsset null
- to prevent orphansrestrict
Index Strategy:
- Single column indexes for frequently filtered columns
- Composite indexes for multi-column WHERE conditions
- Covering indexes for common queries (avoid table lookups)
- Descending indexes for ORDER BY ... DESC queries
- GIN indexes for text search and JSONB columns
- Always index foreign key columns
Check Constraints:
- Validate numeric ranges (year, positive values)
- Validate non-empty required strings
- Validate date logic (created <= updated)
- Validate non-negative counters
3. Automatic Convention Enforcement
After generating or modifying schema code, immediately perform automatic validation and correction:
-
Scan for violations: Review the generated code against all conventions from the reference document
-
Identify issues: Create a mental checklist of any violations found:
- Missing standard columns (id, createdAt, updatedAt)
- Hardcoded lengths instead of
SCHEMA_LIMITS - Hardcoded defaults instead of
DEFAULTS - Missing foreign key indexes
- Incorrect cascade rules
- Missing check constraints
- Using
instead oftextvarchar - Using auto-increment instead of UUID
- Missing soft delete column (
) when neededdeletedAt - Incorrect column naming (snake_case in DB, camelCase in code)
-
Fix automatically: Apply corrections immediately without asking for permission:
- Add missing standard columns
- Replace hardcoded values with constants
- Add missing indexes for foreign keys
- Add appropriate check constraints
- Fix column type issues
- Reorder columns alphabetically
- Add missing soft delete column (
)deletedAt
-
Verify completeness: Ensure all conventions are satisfied before presenting code to user
4. Reporting
After automatically fixing violations, provide a brief summary:
✓ Database schema conventions enforced: - Added missing timestamp columns (createdAt, updatedAt) - Replaced hardcoded length with SCHEMA_LIMITS.ENTITY.NAME.MAX - Added soft delete column (deletedAt) - Added foreign key index: collection_user_id_idx - Added check constraint: collection_name_not_empty
Do not ask for permission to apply fixes - the skill's purpose is automatic enforcement.
Convention Categories
The complete conventions are detailed in
references/Database-Schema-Conventions.md. Key categories include:
- File Structure - Schema file organization patterns
- Table Definition Template - Standard pgTable structure
- Column Conventions - Standard, soft delete, counter, visibility columns
- String Columns - varchar with SCHEMA_LIMITS
- Numeric Columns - Decimal precision, integer patterns
- Foreign Key Patterns - Cascade, set null, restrict rules
- Check Constraints - Numeric, string, date validation
- Index Strategy - Single, composite, covering, descending, GIN
- Junction Tables - Many-to-many relationship patterns
- Column Naming - snake_case DB, camelCase code conventions
Anti-Patterns to Avoid
- Never hardcode lengths - Use
constantsSCHEMA_LIMITS - Never hardcode defaults - Use
constantsDEFAULTS - Never skip timestamps - Always include
,createdAtupdatedAt - Never use auto-increment - Use UUID primary keys
- Never skip indexes - Index all foreign keys and filter columns
- Never cascade delete without consideration - Choose appropriate delete behavior
- Never skip check constraints - Validate data at database level
- Never use
type - Usetext
with explicit lengthvarchar
Important Notes
- Automatic enforcement: Apply fixes immediately without requesting permission
- No compromises: All conventions must be followed strictly
- Reference first: Always load the conventions reference before working with schema code
- Complete validation: Check all aspects of the conventions, not just obvious violations
- Proactive application: Use this skill automatically when schema work is detected, even if user doesn't mention conventions
Workflow Summary
1. Detect schema work (create/modify files in db/schema/) 2. Load references/Database-Schema-Conventions.md 3. Generate or modify schema code following all conventions 4. Scan generated code for any violations 5. Automatically fix all violations found 6. Present corrected code to user with brief summary of fixes applied
This workflow ensures every database schema in the project project maintains consistent, high-quality definitions that follow all established conventions.