Database Schema Management
USE SCHEMA SYNC SCRIPT, NOT ALEMBIC MIGRATIONS
PREFERRED APPROACH: Smart schema sync script that detects and applies only missing changes.
STRICT RULES
- * ✅ ALWAYS USE:
scripts/sync-production-schema.py for production deployments - ✅ IDEMPOTENT: Can run multiple times safely without errors
- ✅ TRANSPARENT: Shows exactly what will change before applying
- ❌ AVOID: Running Alembic migrations directly in production
- ❌ AVOID: Manual SQL scripts that aren't version controlled
- ⚠️ KEEP: Alembic migration files for documentation purposes only
Why Schema Sync Script?
✅ Advantages:
- * Simpler: One script vs managing many migration files
- Safer: Checks what exists before applying changes
- Idempotent: Run multiple times without errors
- Transparent: Shows diff before applying
- Flexible: Works with any database state (dev, staging, prod)
- No tracking: No need to manage "which migrations have run"
❌ Alembic Migration Problems:
- * Fails if run twice (not idempotent)
- Requires tracking which migrations applied
- All-or-nothing (can't skip one migration)
- Complex rollback scenarios
- Team coordination overhead
Workflow
1. DRY RUN FIRST (Always!)
CODEBLOCK0
2. REVIEW OUTPUT
CODEBLOCK1
3. APPLY TO PRODUCTION
CODEBLOCK2
4. VERIFY
CODEBLOCK3
Required Locations
- * Schema Sync Script: INLINECODE1
- Documentation: INLINECODE2
- Alembic Migrations (for documentation): INLINECODE3
When to Update Schema
Adding New Tables
- 1. Define models in INLINECODE4
- Add table creation logic to INLINECODE5
- Update
docs/deployment/SCHEMA_SYNC_GUIDE.md with new table info - Test with
--dry-run first - Apply to dev, staging, then production
Adding New Columns
- 1. Update model in INLINECODE8
- Add column check and ADD COLUMN logic to sync script
- Use IF NOT EXISTS patterns for safety
- Test with
--dry-run first - Apply to environments
Safety Checks Built-In
- * ✅ Checks table exists before creating
- ✅ Checks column exists before adding
- ✅ Transaction safety (rollback on error)
- ✅ Dry-run mode to preview changes
- ✅ Color-coded output for easy reading
- ✅ Summary of all changes applied
Integration with CI/CD
Railway Deployment:
CODEBLOCK4
GitHub Actions:
CODEBLOCK5
ENFORCEMENT
- * ❌ NEVER run
alembic upgrade head in production - ❌ NEVER manually execute SQL in production without sync script
- ❌ NEVER skip dry-run step for production changes
- ✅ ALWAYS use
scripts/sync-production-schema.py for schema changes - ✅ ALWAYS run
--dry-run before INLINECODE13 - ✅ ALWAYS verify changes in dev/staging before production
- ✅ ALWAYS update documentation when adding new tables/columns
VIOLATION CONSEQUENCES
- * Database schema drift between environments
- Failed deployments from migration conflicts
- Data loss from incorrect migrations
- Production downtime from schema errors
- Team confusion about database state
THIS IS A REQUIRED STANDARD. USE SCHEMA SYNC SCRIPT FOR ALL DATABASE CHANGES.
Reference Files
See references/sync-vs-alembic.md for detailed comparison of sync script vs Alembic migrations.
See references/workflow-examples.md for code examples of adding tables, columns, indexes, and handling complex migrations.
Run scripts/verify-sync-script.sh to validate that sync script exists and is properly configured.
数据库模式管理
使用模式同步脚本,而非Alembic迁移
首选方案: 智能模式同步脚本,可检测并仅应用缺失的变更。
严格规则
- * ✅ 始终使用: scripts/sync-production-schema.py 用于生产环境部署
- ✅ 幂等性: 可安全多次运行,不会出错
- ✅ 透明性: 应用前精确显示将要变更的内容
- ❌ 避免: 在生产环境中直接运行Alembic迁移
- ❌ 避免: 使用未进行版本控制的手动SQL脚本
- ⚠️ 保留: Alembic迁移文件仅用于文档目的
为何选择模式同步脚本?
✅ 优势:
- * 更简单: 一个脚本替代管理多个迁移文件
- 更安全: 应用变更前检查现有状态
- 幂等性: 可多次运行,不会出错
- 透明性: 应用前显示差异
- 灵活性: 适用于任何数据库状态(开发、预发布、生产)
- 无需追踪: 无需管理哪些迁移已执行
❌ Alembic迁移问题:
- * 运行两次会失败(不具备幂等性)
- 需要追踪已应用的迁移
- 全有或全无(无法跳过某个迁移)
- 复杂的回滚场景
- 团队协作开销
工作流程
1. 先进行预演运行(始终如此!)
bash
显示将要变更的内容,但不实际应用
python scripts/sync-production-schema.py --dry-run
2. 审查输出
bash
输出显示:
✓ 已存在的表/列(已跳过)
ℹ 将要创建的新表/列
⚠ 任何潜在问题
3. 应用到生产环境
bash
将变更应用到生产数据库
export DATABASE_URL=postgresql://...
python scripts/sync-production-schema.py --apply
4. 验证
bash
连接并验证模式
psql $DATABASE_URL -c \dt # 列出表
psql $DATABASE
URL -c \d tablename # 描述表
必需位置
- * 模式同步脚本: /Users/tobymorning/Desktop/core/scripts/sync-production-schema.py
- 文档: /Users/tobymorning/Desktop/core/docs/deployment/SCHEMASYNCGUIDE.md
- Alembic迁移(用于文档): /Users/tobymorning/Desktop/core/src/backend/alembic/versions/
何时更新模式
添加新表
- 1. 在 src/backend/app/models/ 中定义模型
- 在 scripts/sync-production-schema.py 中添加表创建逻辑
- 使用新表信息更新 docs/deployment/SCHEMASYNCGUIDE.md
- 先用 --dry-run 测试
- 依次应用到开发、预发布和生产环境
添加新列
- 1. 更新 src/backend/app/models/ 中的模型
- 在同步脚本中添加列检查和ADD COLUMN逻辑
- 使用IF NOT EXISTS模式确保安全
- 先用 --dry-run 测试
- 应用到各环境
内置安全检查
- * ✅ 创建前检查表是否存在
- ✅ 添加前检查列是否存在
- ✅ 事务安全性(出错时回滚)
- ✅ 预演模式预览变更
- ✅ 彩色输出便于阅读
- ✅ 所有已应用变更的摘要
与CI/CD集成
Railway部署:
yaml
在Procfile或部署脚本中
release: python scripts/sync-production-schema.py --apply
GitHub Actions:
yaml
env:
DATABASE
URL: ${{ secrets.DATABASEURL }}
run: python scripts/sync-production-schema.py --apply
强制执行
- * ❌ 绝不 在生产环境中运行 alembic upgrade head
- ❌ 绝不 在生产环境中不使用同步脚本手动执行SQL
- ❌ 绝不 对生产环境变更跳过预演步骤
- ✅ 始终 使用 scripts/sync-production-schema.py 进行模式变更
- ✅ 始终 在 --apply 之前运行 --dry-run
- ✅ 始终 在生产环境之前在开发/预发布环境中验证变更
- ✅ 始终 在添加新表/列时更新文档
违规后果
- * 环境间数据库模式漂移
- 因迁移冲突导致部署失败
- 因错误迁移导致数据丢失
- 因模式错误导致生产环境停机
- 团队对数据库状态产生困惑
这是必需标准。所有数据库变更均使用模式同步脚本。
参考文件
参见 references/sync-vs-alembic.md 了解同步脚本与Alembic迁移的详细对比。
参见 references/workflow-examples.md 获取添加表、列、索引以及处理复杂迁移的代码示例。
运行 scripts/verify-sync-script.sh 验证同步脚本是否存在且配置正确。