-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_migrate.py
36 lines (30 loc) · 1.28 KB
/
db_migrate.py
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
import subprocess
import sys
def run_migrations():
try:
# Check current revision
result = subprocess.run(['alembic', 'current'], capture_output=True, text=True)
print("Current database revision:")
print(result.stdout)
# Check for pending migrations
result = subprocess.run(['alembic', 'heads'], capture_output=True, text=True)
latest_revision = result.stdout.strip()
result = subprocess.run(['alembic', 'current'], capture_output=True, text=True)
current_revision = result.stdout.strip().split(' ')[0]
if latest_revision != current_revision:
print("Pending migrations found. Applying migrations...")
result = subprocess.run(['alembic', 'upgrade', 'head'], check=True)
print("Migrations applied successfully.")
else:
print("Database is up to date. No migrations to apply.")
return True
except subprocess.CalledProcessError as e:
print(f"Error occurred while running migrations: {e}")
return False
if __name__ == "__main__":
success = run_migrations()
if not success:
print("Migration script failed. Please check the logs and try again.")
sys.exit(1)
else:
print("Migration script completed successfully.")