-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_production.py
More file actions
76 lines (63 loc) · 2.63 KB
/
migrate_production.py
File metadata and controls
76 lines (63 loc) · 2.63 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
"""
Production database migration script.
This script connects to the production database and runs Alembic migrations.
"""
import os
import sys
from alembic.config import Config
from alembic import command
from sqlalchemy import create_engine, text
def run_production_migration():
"""Run Alembic migration on production database."""
# Get the production database URL
# This should be set via Vercel environment variables
database_url = os.getenv('DATABASE_URL')
if not database_url:
print("❌ DATABASE_URL environment variable not found.")
print("Make sure you have the production database URL set.")
sys.exit(1)
print(f"🔍 Production database URL: {database_url[:50]}...")
try:
# Test database connection first
print("🔗 Testing database connection...")
engine = create_engine(database_url)
with engine.connect() as conn:
result = conn.execute(text("SELECT 1"))
print("✅ Database connection successful!")
# Check current migration status
print("\n📋 Checking current migration status...")
alembic_cfg = Config("alembic.ini")
alembic_cfg.set_main_option("sqlalchemy.url", database_url)
try:
# Show current revision
command.current(alembic_cfg, verbose=True)
except Exception as e:
print(f"⚠️ Could not get current revision: {e}")
print("This might be the first time running migrations on this database.")
# Run migrations
print("\n🚀 Running Alembic migrations...")
command.upgrade(alembic_cfg, "head")
print("✅ Migrations completed successfully!")
# Verify the column exists now
print("\n🔍 Verifying latest_reading_id column...")
with engine.connect() as conn:
result = conn.execute(text("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'api_sensors'
AND column_name = 'latest_reading_id'
"""))
if result.fetchone():
print("✅ latest_reading_id column exists in production database!")
else:
print("❌ latest_reading_id column still not found!")
except Exception as e:
print(f"❌ Error during migration: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
print("🏭 Production Database Migration")
print("=" * 50)
run_production_migration()