Loading...
Loading...
Database design and migration patterns for Alembic migrations, schema design (SQL/NoSQL), and database versioning. Use when creating migrations, designing schemas, normalizing data, managing database versions, or handling schema drift.
npx skill4agent add yonatangross/orchestkit database-patternsrules/| Category | Rules | Impact | When to Use |
|---|---|---|---|
| Alembic Migrations | 3 | CRITICAL | Autogenerate, data migrations, branch management |
| Schema Design | 3 | HIGH | Normalization, indexing strategies, NoSQL patterns |
| Versioning | 3 | HIGH | Changelogs, rollback plans, schema drift detection |
| Zero-Downtime Migration | 2 | CRITICAL | Expand-contract, pgroll, rollback monitoring |
# Alembic: Auto-generate migration from model changes
# alembic revision --autogenerate -m "add user preferences"
def upgrade() -> None:
op.add_column('users', sa.Column('org_id', UUID(as_uuid=True), nullable=True))
op.execute("UPDATE users SET org_id = 'default-org-uuid' WHERE org_id IS NULL")
def downgrade() -> None:
op.drop_column('users', 'org_id')-- Schema: Normalization to 3NF with proper indexing
CREATE TABLE orders (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
customer_id UUID NOT NULL REFERENCES customers(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_orders_customer_id ON orders(customer_id);| Rule | File | Key Pattern |
|---|---|---|
| Autogenerate | | Auto-generate from models, async env.py, review workflow |
| Data Migration | | Batch backfill, two-phase NOT NULL, zero-downtime |
| Branching | | Feature branches, merge migrations, conflict resolution |
| Rule | File | Key Pattern |
|---|---|---|
| Normalization | | 1NF-3NF, when to denormalize, JSON vs normalized |
| Indexing | | B-tree, GIN, HNSW, partial/covering indexes |
| NoSQL Patterns | | Embed vs reference, document design, sharding |
| Rule | File | Key Pattern |
|---|---|---|
| Changelog | | Schema version table, semantic versioning, audit trails |
| Rollback | | Rollback testing, destructive rollback docs, CI verification |
| Drift Detection | | Environment sync, checksum verification, migration locks |
| Rule | File | Key Pattern |
|---|---|---|
| Selection Guide | | PostgreSQL-first, tier-based matrix, anti-patterns |
| Decision | Recommendation | Rationale |
|---|---|---|
| Async dialect | | Native async support for SQLAlchemy 2.0 |
| NOT NULL column | Two-phase: nullable first, then alter | Avoids locking, backward compatible |
| Large table index | | Zero-downtime, no table locks |
| Normalization target | 3NF for OLTP | Reduces redundancy while maintaining query performance |
| Primary key strategy | UUID for distributed, INT for single-DB | Context-appropriate key generation |
| Soft deletes | | Preserves audit trail, enables recovery |
| Migration granularity | One logical change per file | Easier rollback and debugging |
| Production deployment | Generate SQL, review, then apply | Never auto-run in production |
# NEVER: Add NOT NULL without default or two-phase approach
op.add_column('users', sa.Column('org_id', UUID, nullable=False)) # LOCKS TABLE!
# NEVER: Use blocking index creation on large tables
op.create_index('idx_large', 'big_table', ['col']) # Use CONCURRENTLY
# NEVER: Skip downgrade implementation
def downgrade():
pass # WRONG - implement proper rollback
# NEVER: Modify migration after deployment - create new migration instead
# NEVER: Run migrations automatically in production
# Use: alembic upgrade head --sql > review.sql
# NEVER: Run CONCURRENTLY inside transaction
op.execute("BEGIN; CREATE INDEX CONCURRENTLY ...; COMMIT;") # FAILS
# NEVER: Delete migration history
command.stamp(alembic_config, "head") # Loses history
# NEVER: Skip environments (Always: local -> CI -> staging -> production)| Resource | Description |
|---|---|
| references/ | Advanced patterns: Alembic, normalization, migration, audit, environment, versioning |
| checklists/ | Migration deployment and schema design checklists |
| examples/ | Complete migration examples, schema examples |
| scripts/ | Migration templates, model change detector |
| Rule | File | Key Pattern |
|---|---|---|
| Expand-Contract | | Expand phase, backfill, contract phase, pgroll automation |
| Rollback & Monitoring | | pgroll rollback, lock monitoring, replication lag, backfill progress |
sqlalchemy-2-asynctesting-patternscachingperformance