Claude-Skills database-designer
git clone https://github.com/borghei/Claude-Skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/borghei/Claude-Skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/engineering/database-designer" ~/.claude/skills/borghei-claude-skills-database-designer && rm -rf "$T"
engineering/database-designer/SKILL.mdDatabase Designer
The agent analyzes SQL schemas for normalization compliance, recommends optimal indexes based on query patterns, and generates safe migration scripts with rollback procedures. It produces Mermaid ERDs, detects redundant indexes, and implements zero-downtime expand-contract migration patterns for PostgreSQL and MySQL.
Quick Start
# Analyze a schema for normalization issues and generate ERD python schema_analyzer.py --input schema.sql --generate-erd --output-format json # Recommend indexes based on query patterns python index_optimizer.py --schema schema.json --queries queries.json --analyze-existing # Generate migration scripts between schema versions python migration_generator.py --current current.json --target target.json --zero-downtime
Core Workflows
Workflow 1: Analyze and Optimize a Schema
- Provide DDL (SQL) or JSON schema definition
- Run
to detect normalization violations (1NF-BCNF), missing constraints, and naming issuesschema_analyzer.py - Review generated Mermaid ERD for relationship visualization
- Run
with query patterns to get index recommendationsindex_optimizer.py - Validation checkpoint: All 1NF-3NF violations addressed; foreign keys declared; no redundant indexes
python schema_analyzer.py -i schema.sql -f json -e -o report.json python index_optimizer.py -s schema.json -q queries.json -e -p 2 -o index_report.json
Workflow 2: Generate a Safe Migration
- Export current and target schemas as JSON
- Run
to produce forward and rollback SQLmigration_generator.py - For large tables (10M+ rows), add
for expand-contract pattern--zero-downtime - Review validation queries that confirm migration success
- Validation checkpoint: Every forward step has a rollback counterpart; validation queries pass on test data
python migration_generator.py -c current.json -t target.json -z --include-validations -f json -o plan.json
Workflow 3: Index Optimization for Query Patterns
- Document top 10 query patterns as JSON (WHERE clauses, JOINs, ORDER BY)
- Run
withindex_optimizer.py
to find redundancies--analyze-existing - Review composite index column ordering (most selective first)
- Check for covering index opportunities
- Validation checkpoint: Query patterns covered; no overlapping indexes; estimated 40%+ query time reduction
Index Type Selection
| Index Type | Best For | Example |
|---|---|---|
| B-tree | Range queries, sorting, equality | |
| Partial | Subset queries on hot data | |
| Covering | Avoiding table lookups | |
| Hash | Exact match only | Primary keys, cache keys |
| GIN | JSONB, array, full-text | |
Anti-Patterns
- Over-indexing -- every column indexed wastes write performance and storage; index only columns appearing in WHERE, JOIN, and ORDER BY
- Missing foreign keys -- relying on application-layer referential integrity leads to orphaned records; always declare FK constraints
- VARCHAR(255) everywhere -- oversized columns waste memory in indexes; right-size columns based on actual data
- Premature denormalization -- denormalize only when EXPLAIN ANALYZE shows join-related bottlenecks, not preemptively
- Direct ALTER on large tables --
on a 100M-row table locks the table; use expand-contract patternALTER TABLE ... SET NOT NULL - No validation queries in migrations -- migrations without post-step validation risk silent data corruption
Troubleshooting
| Problem | Cause | Solution |
|---|---|---|
| Schema analyzer reports false 1NF violations | JSON or array columns detected as multi-valued fields | Review flagged columns; intentional JSONB/array usage is valid for document-style storage patterns |
| Index optimizer recommends indexes on low-selectivity columns | Boolean or status columns appear in frequent WHERE clauses | Use partial indexes () instead of full-column indexes to reduce overhead |
| Migration generator produces high-risk steps for column type changes | Direct can lock tables and fail on incompatible data | Use the flag to generate expand-contract migration patterns with safe backfill steps |
| ERD output missing relationships | Foreign key constraints not declared in DDL or JSON input | Ensure all FK relationships are explicitly defined; the analyzer only detects declared constraints |
| Composite index column order seems wrong | Optimizer orders by estimated selectivity, not query clause order | Verify cardinality estimates in the schema JSON; provide per column for accurate ordering |
| Redundancy analysis flags covering indexes as overlapping | Overlap ratio calculation uses Jaccard similarity on column sets | Review flagged pairs manually; covering indexes with INCLUDE columns serve a different purpose than their subsets |
| Validation queries fail after migration | Target schema JSON does not match actual post-migration state | Run before and after migration; ensure the target JSON reflects all intended changes precisely |
Success Criteria
- Schema analysis detects 90%+ of normalization violations (1NF through BCNF) when provided complete DDL input
- Index recommendations reduce query execution time by 40%+ for analyzed query patterns (measured via EXPLAIN ANALYZE before/after)
- Migration scripts execute with zero data loss and include verified rollback for every forward step
- ERD generation produces valid Mermaid diagrams that render correctly for schemas with up to 50 tables
- Redundant index detection identifies 95%+ of duplicate and overlapping indexes with less than 5% false positive rate
- Zero-downtime migrations maintain full application availability during schema changes on tables with 10M+ rows
- Generated SQL statements are syntactically valid and compatible with PostgreSQL 14+ and MySQL 8.0+
Scope & Limitations
Covers:
- Schema design analysis for SQL databases (PostgreSQL, MySQL) including normalization, constraints, naming, and data types
- Index optimization with selectivity estimation, composite index ordering, covering indexes, and redundancy detection
- Migration generation with forward/rollback scripts, zero-downtime patterns, and validation queries
- ERD generation in Mermaid format from DDL or JSON schema definitions
Does NOT cover:
- Runtime query performance monitoring or live database profiling (see
skill)performance-profiler - NoSQL-specific schema design for MongoDB, DynamoDB, or Cassandra (conceptual guidance only in the reference sections)
- Database administration tasks such as backup/restore, replication setup, or user/role management
- Application-level ORM configuration, connection pool tuning, or driver-specific optimizations (see
for ORM-adjacent patterns)database-schema-designer
Integration Points
| Skill | Integration | Data Flow |
|---|---|---|
| Migration strategy and execution planning for large-scale schema changes | Database Designer generates migration SQL; Migration Architect orchestrates multi-service deployment order and rollback coordination |
| Complementary schema design with focus on application-layer patterns | Database Designer provides normalization analysis; Schema Designer applies ORM mapping and application modeling conventions |
| Runtime validation of index and schema optimization recommendations | Database Designer outputs recommended indexes; Performance Profiler measures actual query plan improvements via EXPLAIN ANALYZE |
| Alignment between database schema and API resource contracts | Database Designer defines table structures; API Design Reviewer validates that endpoint schemas match underlying data models |
| Automated migration execution in deployment pipelines | Database Designer generates migration scripts; CI/CD Pipeline Builder integrates them into deployment stages with validation gates |
| Database performance monitoring and alerting post-optimization | Database Designer identifies query patterns; Observability Designer configures slow query alerts and index usage dashboards |
Tool Reference
schema_analyzer.py
Purpose: Analyzes SQL DDL statements and JSON schema definitions for normalization compliance, missing constraints, data type issues, naming convention violations, and relationship mapping. Generates Mermaid ERD diagrams.
Usage:
python schema_analyzer.py --input schema.sql --output-format json python schema_analyzer.py --input schema.json --output-format text python schema_analyzer.py --input schema.sql --generate-erd --output analysis.json python schema_analyzer.py --input schema.sql --erd-only
Flags/Parameters:
| Flag | Short | Required | Description |
|---|---|---|---|
| | Yes | Input file path (SQL DDL or JSON schema) |
| | No | Output file path (default: stdout) |
| | No | Output format: or (default: ) |
| | No | Include Mermaid ERD diagram in output |
| No | Output only the Mermaid ERD diagram |
Example:
python schema_analyzer.py -i my_schema.sql -f json -e -o report.json
Output Formats:
-- Human-readable report with normalization findings, constraint issues, data type recommendations, and naming violationstext
-- Structured JSON withjson
,normalization_issues
,constraint_issues
,data_type_issues
,naming_issues
, and optionalrelationships
fieldserd_diagram
index_optimizer.py
Purpose: Analyzes schema definitions and query patterns to recommend optimal indexes. Identifies missing indexes, detects redundant and overlapping indexes, suggests composite index column ordering, estimates selectivity, and generates CREATE INDEX statements.
Usage:
python index_optimizer.py --schema schema.json --queries queries.json --format text python index_optimizer.py --schema schema.json --queries queries.json --output recommendations.json --format json python index_optimizer.py --schema schema.json --queries queries.json --analyze-existing python index_optimizer.py --schema schema.json --queries queries.json --min-priority 2
Flags/Parameters:
| Flag | Short | Required | Description |
|---|---|---|---|
| | Yes | Schema definition JSON file |
| | Yes | Query patterns JSON file |
| | No | Output file path (default: stdout) |
| | No | Output format: or (default: ) |
| | No | Include analysis of existing indexes for redundancy |
| | No | Minimum priority level to include: 1=highest, 4=lowest (default: ) |
Example:
python index_optimizer.py -s schema.json -q queries.json -f json -e -p 2 -o index_report.json
Output Formats:
-- Human-readable report with analysis summary, high-priority recommendations, redundancy issues, performance impact analysis, and CREATE INDEX statementstext
-- Structured JSON withjson
,analysis_summary
(by priority),index_recommendations
,redundancy_analysis
,size_estimates
, andsql_statements
fieldsperformance_impact
migration_generator.py
Purpose: Generates safe migration scripts between schema versions. Compares current and target schemas, produces ALTER TABLE statements, implements zero-downtime expand-contract patterns, creates rollback scripts, and generates validation queries.
Usage:
python migration_generator.py --current current.json --target target.json --format text python migration_generator.py --current current.json --target target.json --output migration.sql --format sql python migration_generator.py --current current.json --target target.json --zero-downtime --format json python migration_generator.py --current current.json --target target.json --validate-only
Flags/Parameters:
| Flag | Short | Required | Description |
|---|---|---|---|
| | Yes | Current schema JSON file |
| | Yes | Target schema JSON file |
| | No | Output file path (default: stdout) |
| | No | Output format: , , or (default: ) |
| | No | Generate zero-downtime migration using expand-contract pattern |
| | No | Only generate validation queries, skip migration steps |
| No | Include validation queries in migration output |
Example:
python migration_generator.py -c current.json -t target.json -z --include-validations -f json -o migration_plan.json
Output Formats:
-- Human-readable migration plan with ordered steps, forward SQL, rollback SQL, risk levels, and execution timelinetext
-- Structured JSON withjson
,migration_id
(each withsteps
,sql_forward
,sql_rollback
,validation_sql
,risk_level
),zero_downtime_phase
,summary
, andexecution_orderrollback_order
-- Raw SQL output with forward migration statements, suitable for direct execution or piping into a database clientsql