Spartan-ai-toolkit database-patterns
Database design patterns including schema design, migrations, soft deletes, and Exposed ORM. Use when creating tables, writing migrations, or implementing repositories.
install
source · Clone the upstream repo
git clone https://github.com/c0x12c/ai-toolkit
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/c0x12c/ai-toolkit "$T" && mkdir -p ~/.claude/skills && cp -r "$T/toolkit/skills/database-patterns" ~/.claude/skills/spartan-stratos-spartan-ai-toolkit-database-patterns-b4c172 && rm -rf "$T"
manifest:
toolkit/skills/database-patterns/SKILL.mdsource content
Database Patterns — Quick Reference
Hard Rules
| Rule | Do | Don't |
|---|---|---|
| Data types | TEXT | VARCHAR |
| Primary keys | UUID | SERIAL, BIGINT |
| Soft delete | deleted_at TIMESTAMP | DELETE FROM |
| Foreign keys | App-level validation | REFERENCES, ON DELETE CASCADE |
| Standard columns | id, created_at, updated_at, deleted_at | Skip any of these |
Migration Template
SQL migration with table, trigger, and partial indexes for soft delete.
See code-templates.md for the complete SQL template.
Exposed Table Object
Extend
UUIDTable, use text() not varchar(), add standard timestamp columns.
See code-templates.md for the complete template.
Entity Data Class
Implement
Entity<Instant>, include all business fields + createdAt, updatedAt, deletedAt.
See code-templates.md for the complete template.
Repository Pattern
Interface +
Default* implementation. Reads on db.replica, writes on db.primary. Soft delete via deletedAt update. convert() method maps ResultRow to entity.
See code-templates.md for the full interface + implementation code.
When Creating a New Table
Full checklist:
- SQL migration file (next number in sequence)
- Table object in
module-repository/table/ - Entity data class in
module-repository/entity/ - Enum/constants in
(if needed)module-repository/constant/ - Repository interface + implementation
- Factory bean for repository
- Repository tests
Flyway Rules
- NEVER add a migration that fills a gap in deployed sequence
- NEVER rename an already-deployed migration file
- Migration numbers must be sequential from the latest
- Keep migrations simple and focused (one table per migration)
Gotchas
- Only
usesid
-- everything else is direct..value
gives UUID, butrow[Table.id].value
already returns UUID. Addingrow[Table.projectId]
to non-id columns causes compile errors..value
vsgen_random_uuid()
-- pick one per project. Mixing them works but confuses code review. Check existing migrations for which one the project uses.uuid_generate_v4()- Don't re-declare
,createdAt
,updatedAt
if extending SoftDeleteTable. They're inherited. Declaring them again causes duplicate column errors.deletedAt - Forgetting
on indexes wastes space. Every index on a soft-delete table should be partial. Full indexes include dead records nobody queries.WHERE deleted_at IS NULL - Text columns that hold JSON should still use
in Exposed. JSONB in Postgres,text()
in Kotlin, serialize/deserialize in the entity layer.text()