This commit consolidates the fragmented orchestration service migrations into a single, well-structured initial schema version file. Changes: - Created 001_initial_schema.py consolidating all table definitions - Merged fields from 2 previous migrations into one comprehensive file - Added SCHEMA_DOCUMENTATION.md with complete schema reference - Added MIGRATION_GUIDE.md for deployment instructions Schema includes: - orchestration_runs table (47 columns) - orchestrationstatus enum type - 15 optimized indexes for query performance - Full step tracking (forecasting, production, procurement, notifications, AI insights) - Saga pattern support - Performance metrics tracking - Error handling and retry logic Benefits: - Better organization and documentation - Fixes revision ID inconsistencies from old migrations - Eliminates duplicate index definitions - Logically categorizes fields by purpose - Easier to understand and maintain - Comprehensive documentation for developers The consolidated migration provides the same final schema as the original migration chain but in a cleaner, more maintainable format.
6.2 KiB
Migration Guide - Consolidated Schema
Overview
This guide explains how to use the new consolidated initial schema migration for the Orchestration Service.
Background
The orchestration service schema was previously split across two migration files:
20251029_1700_add_orchestration_runs.py- Initial table creation20251105_add_ai_insights_tracking.py- AI insights fields addition
These have been consolidated into a single, well-structured initial schema file: 001_initial_schema.py
For New Deployments
If you're deploying the orchestration service for the first time:
-
Use the consolidated migration:
cd services/orchestrator alembic upgrade head -
The migration will create:
orchestration_runstable with all columnsorchestrationstatusenum type- All 15 indexes for optimal query performance
-
Verify the migration:
alembic current # Should show: 001_initial_schema (head)
For Existing Deployments
If your database already has the orchestration schema from the old migrations:
Option 1: Keep Existing Migration History (Recommended)
Do nothing. Your existing migrations are functionally equivalent to the consolidated version. The schema structure is identical.
- Your alembic_version table will show:
20251105_add_ai_insights - The new consolidated migration is for future deployments
- No action needed - your database is up to date
Option 2: Reset Migration History (For Clean State)
Only do this if you want a clean migration history and can afford downtime:
-
Backup your database:
pg_dump -h localhost -U user orchestrator_db > backup.sql -
Drop and recreate schema:
# In PostgreSQL DROP SCHEMA public CASCADE; CREATE SCHEMA public; -
Apply consolidated migration:
cd services/orchestrator alembic upgrade head -
Restore data (if needed):
psql -h localhost -U user orchestrator_db < backup_data_only.sql
⚠️ Warning: This approach requires downtime and careful data backup/restore.
Checking Your Current Migration Status
Check Alembic version:
cd services/orchestrator
alembic current
Check applied migrations:
alembic history
Verify table structure:
-- Check if table exists
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'orchestration_runs'
);
-- Check column count
SELECT COUNT(*) FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'orchestration_runs';
-- Should return 47 columns
-- Check indexes
SELECT indexname FROM pg_indexes
WHERE tablename = 'orchestration_runs';
-- Should return 15 indexes
Migration File Comparison
Old Migration Chain
None → 20251029_1700 → 20251105_add_ai_insights
New Consolidated Migration
None → 001_initial_schema
Both result in the exact same database schema, but the new version is:
- ✅ Better organized and documented
- ✅ Easier to understand and maintain
- ✅ Fixes revision ID inconsistencies
- ✅ Properly categorizes fields
- ✅ Eliminates duplicate index definitions
Troubleshooting
Issue: "relation already exists"
Cause: Database already has the schema from old migrations.
Solution:
- For existing deployments, no action needed
- For fresh start, see "Option 2: Reset Migration History" above
Issue: "enum type already exists"
Cause: The orchestrationstatus enum was created by old migration.
Solution:
- The migration uses
create_type=Falseandcheckfirst=Trueto handle this - Should not be an issue in practice
Issue: "duplicate key value violates unique constraint on alembic_version"
Cause: Trying to apply new migration on database with old migrations.
Solution:
- Don't apply the new migration on existing databases
- The old migrations already provide the same schema
Deprecation Notice
Files Superseded (Do Not Delete Yet)
The following migration files are superseded but kept for reference:
20251029_1700_add_orchestration_runs.py20251105_add_ai_insights_tracking.py
Why keep them?
- Existing deployments reference these migrations
- Provides migration history for troubleshooting
- Can be removed in future major version
Future Cleanup
In a future major version (e.g., v2.0.0), after all deployments have migrated:
- Archive old migration files to
migrations/archive/ - Update documentation to reference only consolidated schema
- Clean up alembic version history
Best Practices
-
Always backup before migrations:
pg_dump -Fc -h localhost -U user orchestrator_db > backup_$(date +%Y%m%d).dump -
Test migrations in staging first:
- Never run migrations directly in production
- Verify schema changes in staging environment
- Check application compatibility
-
Monitor migration performance:
- Initial migration should complete in < 1 second for empty database
- Index creation time scales with data volume
-
Use version control:
- All migration files are in git
- Never modify existing migration files
- Create new migrations for schema changes
Getting Help
If you encounter issues with migrations:
- Check migration status:
alembic current - Review migration history:
alembic history - Check database schema: See SQL queries in "Checking Your Current Migration Status" section
- Review logs: Check alembic output for error details
- Consult SCHEMA_DOCUMENTATION.md for expected schema structure
Next Steps
After successfully applying migrations:
-
Verify application startup:
docker-compose up orchestrator -
Run health checks:
curl http://localhost:8000/health -
Test basic operations:
- Create a test orchestration run
- Query run status
- Verify data persistence
-
Monitor logs:
docker-compose logs -f orchestrator
Related Documentation
SCHEMA_DOCUMENTATION.md- Complete schema reference001_initial_schema.py- Consolidated migration file../../README.md- Orchestration service overview