Database Schema Management
SCHEMA SYNC SCRIPT RULES
- •✅ USE:
scripts/sync-production-schema.pyfor deployments - •✅ Idempotent, transparent, safe
- •❌ AVOID: Direct Alembic migrations
- •⚠️ Keep Alembic files for docs
Workflow
1. Dry Run
bash
python scripts/sync-production-schema.py --dry-run
2. Apply to Production
bash
export DATABASE_URL="postgresql://..." python scripts/sync-production-schema.py --apply
3. Verify
bash
psql "$DATABASE_URL" -c "\dt"
Key Locations
- •Sync Script:
/Users/aideveloper/core/scripts/sync-production-schema.py - •Docs:
/Users/aideveloper/core/docs/deployment/SCHEMA_SYNC_GUIDE.md
Update Schema Process
Adding Tables
- •Define models in
src/backend/app/models/ - •Update sync script
- •Test with
--dry-run - •Apply to environments
Adding Columns
- •Update model
- •Add column check in sync script
- •Use IF NOT EXISTS
- •Test with
--dry-run
Safety Checks
- •Exists checks before creating
- •Transaction safety
- •Dry-run mode
- •Color-coded output
CI/CD Integration
yaml
release: python scripts/sync-production-schema.py --apply
ENFORCEMENT
- •❌ NO manual SQL in production
- •✅ ALWAYS use sync script
- •✅ ALWAYS dry-run first
- •✅ Verify in dev/staging
VIOLATION CONSEQUENCES
- •Schema drift
- •Deployment failures
- •Potential data loss
- •Production downtime
USE SCHEMA SYNC SCRIPT FOR ALL DATABASE CHANGES.
References
- •
references/sync-vs-alembic.md - •
references/workflow-examples.md - •Validate:
scripts/verify-sync-script.sh