-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_user_hub_status.py
More file actions
59 lines (45 loc) · 2 KB
/
Copy pathmigrate_user_hub_status.py
File metadata and controls
59 lines (45 loc) · 2 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
#!/usr/bin/env python3
"""
Migration script to update existing user_hub records to inactive status
and apply the new default status behavior.
Run this script after updating the model to ensure existing data is consistent.
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sqlalchemy import create_engine, text
from app.core.config import settings
def migrate_user_hub_status():
"""Update existing user_hub records to have inactive status by default"""
# Create database connection
engine = create_engine(settings.DATABASE_URL)
try:
with engine.connect() as connection:
# Start a transaction
trans = connection.begin()
try:
# Update existing records to inactive status (False)
# This ensures all existing submissions require admin approval
result = connection.execute(
text("UPDATE user_hub SET status = false WHERE status = true")
)
print(f"Updated {result.rowcount} user_hub records to inactive status")
# Update the default value in the database schema
connection.execute(
text("ALTER TABLE user_hub ALTER COLUMN status SET DEFAULT false")
)
print("Updated user_hub table default status to false")
# Commit the transaction
trans.commit()
print("Migration completed successfully!")
except Exception as e:
# Rollback on error
trans.rollback()
print(f"Error during migration: {e}")
raise
except Exception as e:
print(f"Failed to connect to database: {e}")
sys.exit(1)
if __name__ == "__main__":
print("Starting user_hub status migration...")
migrate_user_hub_status()