-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_database.py
More file actions
62 lines (50 loc) · 2.21 KB
/
Copy pathmigrate_database.py
File metadata and controls
62 lines (50 loc) · 2.21 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
import os
import logging
import sqlite3
from pathlib import Path
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def migrate_database():
"""Update the database schema to add missing columns"""
try:
# Get the database path
db_path = os.getenv("DATABASE_URL", "sqlite:///flows.db")
# For SQLite, extract the file path
if db_path.startswith("sqlite:///"):
db_path = db_path[10:]
logger.info(f"Migrating database at: {db_path}")
# Connect to the database
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Get all columns in the api_keys table
columns = [row[1] for row in cursor.execute("PRAGMA table_info(api_keys)")]
# Add missing columns
if "user_id" not in columns:
logger.info("Adding user_id column to api_keys table")
cursor.execute("ALTER TABLE api_keys ADD COLUMN user_id INTEGER REFERENCES users(id)")
conn.commit()
logger.info("Added user_id column successfully")
else:
logger.info("user_id column already exists in api_keys table")
if "masked_key" not in columns:
logger.info("Adding masked_key column to api_keys table")
cursor.execute("ALTER TABLE api_keys ADD COLUMN masked_key TEXT")
conn.commit()
logger.info("Added masked_key column successfully")
else:
logger.info("masked_key column already exists in api_keys table")
if "last_used" not in columns:
logger.info("Adding last_used column to api_keys table")
cursor.execute("ALTER TABLE api_keys ADD COLUMN last_used DATETIME")
conn.commit()
logger.info("Added last_used column successfully")
else:
logger.info("last_used column already exists in api_keys table")
# Close the connection
conn.close()
logger.info("Database migration completed successfully")
except Exception as e:
logger.error(f"Error during database migration: {str(e)}")
raise
if __name__ == "__main__":
migrate_database()