Skip to content

Commit 0edfdb4

Browse files
authored
Merge pull request #18 from BrainDriveAI/fix/migration-issue
Fixing/Clearing up migration process and workflow
2 parents 222f19a + 4f15921 commit 0edfdb4

18 files changed

+3855
-352
lines changed

.pre-commit-config.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: migration-validation
5+
name: Validate Database Migrations
6+
entry: python backend/scripts/validate_migrations.py
7+
language: python
8+
files: ^backend/migrations/versions/.*\.py$
9+
pass_filenames: false
10+
11+
- id: migration-naming
12+
name: Check Migration Naming Convention
13+
entry: python backend/scripts/check_migration_naming.py
14+
language: python
15+
files: ^backend/migrations/versions/.*\.py$
16+
17+
- id: migration-syntax
18+
name: Check Migration Syntax
19+
entry: python -m py_compile
20+
language: python
21+
files: ^backend/migrations/versions/.*\.py$
22+
23+
- repo: https://github.com/psf/black
24+
rev: 23.3.0
25+
hooks:
26+
- id: black
27+
files: ^(backend/|scripts/).*\.py$
28+
29+
- repo: https://github.com/pycqa/flake8
30+
rev: 6.0.0
31+
hooks:
32+
- id: flake8
33+
files: ^(backend/|scripts/).*\.py$

Makefile

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Database Migration Management Commands
2+
3+
.PHONY: validate-migrations verify-db test-migrations fix-migration-conflict advanced-tests monitor-health recovery-status
4+
5+
# Validate all migrations for conflicts
6+
validate-migrations:
7+
@echo "🔍 Validating migrations..."
8+
@cd backend && python scripts/validate_migrations.py --verbose
9+
10+
# Verify database state matches models
11+
verify-db:
12+
@echo "🔍 Verifying database state..."
13+
@cd backend && python scripts/verify_database_state.py --verbose
14+
15+
# Test migration up/down cycle
16+
test-migrations:
17+
@echo "🧪 Testing migrations..."
18+
@cd backend && python scripts/test_migrations.py
19+
20+
# Test individual migrations
21+
test-migrations-individual:
22+
@echo "🧪 Testing individual migrations..."
23+
@cd backend && python scripts/test_migrations.py --test-individual
24+
25+
# Advanced migration testing (Phase 3)
26+
advanced-tests:
27+
@echo "🧪 Running advanced migration tests..."
28+
@cd backend && python scripts/advanced_migration_tests.py --verbose
29+
30+
# Advanced performance testing
31+
performance-tests:
32+
@echo "⚡ Running performance tests..."
33+
@cd backend && python scripts/advanced_migration_tests.py --performance-only --verbose
34+
35+
# Monitor migration health
36+
monitor-health:
37+
@echo "🏥 Checking migration health..."
38+
@cd backend && python scripts/migration_monitor.py --verbose
39+
40+
# Start continuous monitoring
41+
monitor-continuous:
42+
@echo "🔄 Starting continuous monitoring..."
43+
@cd backend && python scripts/migration_monitor.py --continuous
44+
45+
# Recovery status and diagnostics
46+
recovery-status:
47+
@echo "📊 Migration recovery status..."
48+
@cd backend && python scripts/migration_recovery.py status
49+
50+
# Diagnose migration issues
51+
recovery-diagnose:
52+
@echo "🔍 Diagnosing migration issues..."
53+
@cd backend && python scripts/migration_recovery.py diagnose
54+
55+
# Create emergency backup
56+
emergency-backup:
57+
@echo "💾 Creating emergency backup..."
58+
@cd backend && python scripts/migration_recovery.py backup --name emergency_$(shell date +%Y%m%d_%H%M%S)
59+
60+
# List available backups
61+
list-backups:
62+
@echo "📦 Available backups:"
63+
@cd backend && python scripts/migration_recovery.py list-backups
64+
65+
# Validate recovery state
66+
validate-recovery:
67+
@echo "✅ Validating recovery state..."
68+
@cd backend && python scripts/migration_recovery.py validate
69+
70+
# Show migration dependency graph
71+
migration-graph:
72+
@echo "📊 Migration dependency graph:"
73+
@cd backend && python scripts/validate_migrations.py --graph
74+
75+
# Fix current migration conflict (backup database first!)
76+
fix-migration-conflict:
77+
@echo "🔧 Applying migration conflict fix..."
78+
@echo "⚠️ Make sure you have a database backup!"
79+
@read -p "Continue? (y/N): " confirm && [ "$$confirm" = "y" ]
80+
@cd backend && alembic upgrade head
81+
82+
# Create new migration with validation
83+
new-migration:
84+
@echo "📝 Creating new migration..."
85+
@read -p "Migration description: " desc && \
86+
cd backend && \
87+
alembic revision --autogenerate -m "$$desc" && \
88+
echo "✅ Migration created. Running validation..." && \
89+
python scripts/validate_migrations.py
90+
91+
# Setup pre-commit hooks
92+
setup-hooks:
93+
@echo "🔧 Setting up pre-commit hooks..."
94+
@pip install pre-commit
95+
@pre-commit install
96+
@echo "✅ Pre-commit hooks installed"
97+
98+
# Emergency: Reset migrations (DANGEROUS - only for development)
99+
reset-migrations:
100+
@echo "⚠️ WARNING: This will reset all migrations!"
101+
@echo "⚠️ Only use in development with backed up data!"
102+
@read -p "Are you absolutely sure? Type 'RESET' to continue: " confirm && [ "$$confirm" = "RESET" ]
103+
@cd backend && \
104+
rm -rf migrations/versions/*.py && \
105+
alembic revision --autogenerate -m "reset_baseline" && \
106+
echo "✅ Migrations reset. Review the new baseline migration before applying."
107+
108+
# Help
109+
help:
110+
@echo "Database Migration Management Commands:"
111+
@echo ""
112+
@echo "Phase 1 & 2 Commands:"
113+
@echo " validate-migrations - Check for migration conflicts"
114+
@echo " verify-db - Verify database matches models"
115+
@echo " test-migrations - Test migration up/down cycle"
116+
@echo " test-migrations-individual - Test each migration individually"
117+
@echo " migration-graph - Show migration dependency graph"
118+
@echo " fix-migration-conflict - Apply fix for current conflict"
119+
@echo " new-migration - Create new migration with validation"
120+
@echo " setup-hooks - Install pre-commit hooks"
121+
@echo ""
122+
@echo "Phase 3 Commands (Testing & Monitoring):"
123+
@echo " advanced-tests - Run comprehensive migration tests"
124+
@echo " performance-tests - Run migration performance tests"
125+
@echo " monitor-health - Check migration system health"
126+
@echo " monitor-continuous - Start continuous health monitoring"
127+
@echo " recovery-status - Show migration recovery status"
128+
@echo " recovery-diagnose - Diagnose migration issues"
129+
@echo " emergency-backup - Create emergency database backup"
130+
@echo " list-backups - List available backups"
131+
@echo " validate-recovery - Validate recovery state"
132+
@echo ""
133+
@echo "Emergency Commands:"
134+
@echo " reset-migrations - Reset all migrations (DANGEROUS)"
135+
@echo " help - Show this help"

0 commit comments

Comments
 (0)