database-patterns by yonatangross/orchestkit
npx skills add https://github.com/yonatangross/orchestkit --skill database-patterns用于数据库迁移、模式设计和版本管理的全面模式集合。每个类别在 rules/ 目录下都有独立的规则文件,按需加载。
| 类别 | 规则数量 | 影响程度 | 使用时机 |
|---|---|---|---|
| Alembic 迁移 | 3 | 关键 | 自动生成、数据迁移、分支管理 |
| 模式设计 | 3 | 高 | 规范化、索引策略、NoSQL 模式 |
| 版本控制 | 3 | 高 | 变更日志、回滚计划、模式漂移检测 |
| 零停机迁移 | 2 | 关键 | 扩展-收缩模式、pgroll、回滚监控 |
| 数据库选择 | 1 | 高 | 选择合适的数据库、PostgreSQL 与 MongoDB 对比、成本分析 |
总计:5 个类别,共 12 条规则
# Alembic: 根据模型变更自动生成迁移
# 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: 使用适当索引进行 3NF 规范化
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);
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
适用于 SQLAlchemy 2.0 异步应用的 Alembic 迁移管理。
| 规则 | 文件 | 关键模式 |
|---|---|---|
| 自动生成 | ${CLAUDE_SKILL_DIR}/rules/alembic-autogenerate.md | 从模型自动生成、异步 env.py、审查工作流 |
| 数据迁移 | ${CLAUDE_SKILL_DIR}/rules/alembic-data-migration.md | 批量回填、两阶段 NOT NULL、零停机 |
| 分支管理 | ${CLAUDE_SKILL_DIR}/rules/alembic-branching.md | 功能分支、合并迁移、冲突解决 |
包含规范化、索引和约束模式的 SQL 与 NoSQL 模式设计。
| 规则 | 文件 | 关键模式 |
|---|---|---|
| 规范化 | ${CLAUDE_SKILL_DIR}/rules/schema-normalization.md | 1NF-3NF、何时反规范化、JSON 与规范化对比 |
| 索引 | ${CLAUDE_SKILL_DIR}/rules/schema-indexing.md | B-tree、GIN、HNSW、部分/覆盖索引 |
| NoSQL 模式 | ${CLAUDE_SKILL_DIR}/rules/schema-nosql.md | 嵌入与引用、文档设计、分片 |
跨环境的数据库版本控制和变更管理。
| 规则 | 文件 | 关键模式 |
|---|---|---|
| 变更日志 | ${CLAUDE_SKILL_DIR}/rules/versioning-changelog.md | 模式版本表、语义化版本控制、审计追踪 |
| 回滚 | ${CLAUDE_SKILL_DIR}/rules/versioning-rollback.md | 回滚测试、破坏性回滚文档、CI 验证 |
| 漂移检测 | ${CLAUDE_SKILL_DIR}/rules/versioning-drift.md | 环境同步、校验和验证、迁移锁 |
选择合适数据库的决策框架。默认:PostgreSQL。
| 规则 | 文件 | 关键模式 |
|---|---|---|
| 选择指南 | ${CLAUDE_SKILL_DIR}/rules/db-selection.md | PostgreSQL 优先、分层矩阵、反模式 |
| 决策项 | 推荐方案 | 原理 |
|---|---|---|
| 异步方言 | postgresql+asyncpg | SQLAlchemy 2.0 的原生异步支持 |
| NOT NULL 列 | 两阶段:先可空,后修改 | 避免锁表,向后兼容 |
| 大表索引 | CREATE INDEX CONCURRENTLY | 零停机,无表锁 |
| 规范化目标 | OLTP 使用 3NF | 减少冗余,同时保持查询性能 |
| 主键策略 | 分布式用 UUID,单数据库用 INT | 上下文相关的键生成 |
| 软删除 | deleted_at 时间戳列 | 保留审计追踪,支持恢复 |
| 迁移粒度 | 每个文件一个逻辑变更 | 便于回滚和调试 |
| 生产环境部署 | 生成 SQL,审查,然后应用 | 切勿在生产环境自动运行 |
# 禁止:添加 NOT NULL 时不使用默认值或两阶段方法
op.add_column('users', sa.Column('org_id', UUID, nullable=False)) # 会锁表!
# 禁止:在大表上使用阻塞式索引创建
op.create_index('idx_large', 'big_table', ['col']) # 应使用 CONCURRENTLY
# 禁止:跳过降级实现
def downgrade():
pass # 错误 - 应实现正确的回滚逻辑
# 禁止:部署后修改迁移文件 - 应创建新的迁移文件
# 禁止:在生产环境自动运行迁移
# 正确做法:alembic upgrade head --sql > review.sql
# 禁止:在事务内运行 CONCURRENTLY
op.execute("BEGIN; CREATE INDEX CONCURRENTLY ...; COMMIT;") # 会失败
# 禁止:删除迁移历史
command.stamp(alembic_config, "head") # 会丢失历史记录
# 禁止:跳过环境步骤(始终遵循:本地 -> CI -> 预发布 -> 生产)
| 资源 | 描述 |
|---|---|
${CLAUDE_SKILL_DIR}/references/ | 高级模式:Alembic、规范化、迁移、审计、环境、版本控制 |
${CLAUDE_SKILL_DIR}/checklists/ | 迁移部署和模式设计检查清单 |
${CLAUDE_SKILL_DIR}/examples/ | 完整的迁移示例、模式示例 |
${CLAUDE_SKILL_DIR}/scripts/ | 迁移模板、模型变更检测器 |
使用扩展-收缩模式和在线模式变更,实现无停机的安全数据库模式更改。
| 规则 | 文件 | 关键模式 |
|---|---|---|
| 扩展-收缩 | ${CLAUDE_SKILL_DIR}/rules/migration-zero-downtime.md | 扩展阶段、回填、收缩阶段、pgroll 自动化 |
| 回滚与监控 | ${CLAUDE_SKILL_DIR}/rules/migration-rollback.md | pgroll 回滚、锁监控、复制延迟、回填进度 |
sqlalchemy-2-async - 异步 SQLAlchemy 会话模式ork:testing-integration - 包含迁移测试的集成测试模式caching - 缓存层设计,补充数据库性能ork:performance - 性能优化模式每周安装量
103
代码仓库
GitHub 星标数
133
首次出现
2026年2月13日
安全审计
安装于
gemini-cli94
opencode94
codex93
github-copilot93
cursor91
kimi-cli86
Comprehensive patterns for database migrations, schema design, and version management. Each category has individual rule files in rules/ loaded on-demand.
| 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 |
| Database Selection | 1 | HIGH | Choosing the right database, PostgreSQL vs MongoDB, cost analysis |
Total: 12 rules across 5 categories
# 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);
Migration management with Alembic for SQLAlchemy 2.0 async applications.
| Rule | File | Key Pattern |
|---|---|---|
| Autogenerate | ${CLAUDE_SKILL_DIR}/rules/alembic-autogenerate.md | Auto-generate from models, async env.py, review workflow |
| Data Migration | ${CLAUDE_SKILL_DIR}/rules/alembic-data-migration.md | Batch backfill, two-phase NOT NULL, zero-downtime |
| Branching | ${CLAUDE_SKILL_DIR}/rules/alembic-branching.md | Feature branches, merge migrations, conflict resolution |
SQL and NoSQL schema design with normalization, indexing, and constraint patterns.
| Rule | File | Key Pattern |
|---|---|---|
| Normalization | ${CLAUDE_SKILL_DIR}/rules/schema-normalization.md | 1NF-3NF, when to denormalize, JSON vs normalized |
| Indexing | ${CLAUDE_SKILL_DIR}/rules/schema-indexing.md | B-tree, GIN, HNSW, partial/covering indexes |
| NoSQL Patterns | ${CLAUDE_SKILL_DIR}/rules/schema-nosql.md | Embed vs reference, document design, sharding |
Database version control and change management across environments.
| Rule | File | Key Pattern |
|---|---|---|
| Changelog | ${CLAUDE_SKILL_DIR}/rules/versioning-changelog.md | Schema version table, semantic versioning, audit trails |
| Rollback | ${CLAUDE_SKILL_DIR}/rules/versioning-rollback.md | Rollback testing, destructive rollback docs, CI verification |
| Drift Detection | ${CLAUDE_SKILL_DIR}/rules/versioning-drift.md | Environment sync, checksum verification, migration locks |
Decision frameworks for choosing the right database. Default: PostgreSQL.
| Rule | File | Key Pattern |
|---|---|---|
| Selection Guide | ${CLAUDE_SKILL_DIR}/rules/db-selection.md | PostgreSQL-first, tier-based matrix, anti-patterns |
| Decision | Recommendation | Rationale |
|---|---|---|
| Async dialect | postgresql+asyncpg | Native async support for SQLAlchemy 2.0 |
| NOT NULL column | Two-phase: nullable first, then alter | Avoids locking, backward compatible |
| Large table index | CREATE INDEX CONCURRENTLY | 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 | deleted_at timestamp column |
# 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 |
|---|---|
${CLAUDE_SKILL_DIR}/references/ | Advanced patterns: Alembic, normalization, migration, audit, environment, versioning |
${CLAUDE_SKILL_DIR}/checklists/ | Migration deployment and schema design checklists |
${CLAUDE_SKILL_DIR}/examples/ | Complete migration examples, schema examples |
${CLAUDE_SKILL_DIR}/scripts/ | Migration templates, model change detector |
Safe database schema changes without downtime using expand-contract pattern and online schema changes.
| Rule | File | Key Pattern |
|---|---|---|
| Expand-Contract | ${CLAUDE_SKILL_DIR}/rules/migration-zero-downtime.md | Expand phase, backfill, contract phase, pgroll automation |
| Rollback & Monitoring | ${CLAUDE_SKILL_DIR}/rules/migration-rollback.md | pgroll rollback, lock monitoring, replication lag, backfill progress |
sqlalchemy-2-async - Async SQLAlchemy session patternsork:testing-integration - Integration testing patterns including migration testingcaching - Cache layer design to complement database performanceork:performance - Performance optimization patternsWeekly Installs
103
Repository
GitHub Stars
133
First Seen
Feb 13, 2026
Security Audits
Gen Agent Trust HubWarnSocketPassSnykPass
Installed on
gemini-cli94
opencode94
codex93
github-copilot93
cursor91
kimi-cli86
Azure 升级评估与自动化工具 - 轻松迁移 Functions 计划、托管层级和 SKU
104,900 周安装
| 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 |