-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_migration.py
More file actions
56 lines (44 loc) · 1.94 KB
/
apply_migration.py
File metadata and controls
56 lines (44 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
"""
Apply database migrations in backend/migrations
"""
import asyncio
import sys
import os
from pathlib import Path
# Add the backend directory to the Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))
from app.core.database import AsyncSessionLocal
async def apply_migration():
"""Apply SQL migrations from backend/migrations (sorted by filename)."""
print("🔧 Applying database migrations from backend/migrations ...")
migrations_dir = Path("backend/migrations")
if not migrations_dir.exists():
print(f"❌ Migrations directory not found: {migrations_dir}")
return False
migration_files = sorted(migrations_dir.glob("*.sql"))
if not migration_files:
print(f"ℹ️ No migration files found in {migrations_dir}")
return True
async with AsyncSessionLocal() as session:
try:
for migration_file in migration_files:
print(f"📄 Applying {migration_file} ...")
migration_sql = migration_file.read_text()
# NOTE: these migrations must not contain PL/pgSQL blocks with internal semicolons.
statements = [stmt.strip() for stmt in migration_sql.split(";") if stmt.strip()]
for i, statement in enumerate(statements, 1):
if statement.startswith("--") or not statement:
continue
print(f"📝 Executing statement {i}/{len(statements)} from {migration_file.name}...")
await session.execute(statement)
await session.commit()
print("✅ Migrations applied successfully!")
return True
except Exception as e:
print(f"❌ Migration failed: {e}")
await session.rollback()
return False
if __name__ == "__main__":
success = asyncio.run(apply_migration())
sys.exit(0 if success else 1)