-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_db.py
More file actions
145 lines (120 loc) · 5.47 KB
/
Copy pathmigrate_db.py
File metadata and controls
145 lines (120 loc) · 5.47 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from sqlalchemy import create_engine, Column, String, Integer, Boolean, DateTime, ForeignKey, Float, JSON, text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
import os
import sys
# Database URL
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./nexusai.db")
# Create engine and session
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Get a session
db = SessionLocal()
try:
print("Starting database migration...")
# Check if we're using PostgreSQL or SQLite
if 'postgres' in DATABASE_URL:
# PostgreSQL version
print("Using PostgreSQL migration...")
# Check if user_id column exists in api_keys table
check_user_id = """
SELECT column_name
FROM information_schema.columns
WHERE table_name='api_keys' AND column_name='user_id'
"""
result = db.execute(text(check_user_id)).fetchone()
if not result:
print("Adding user_id column to api_keys table...")
db.execute(text("ALTER TABLE api_keys ADD COLUMN user_id INTEGER REFERENCES users(id)"))
# Check if masked_key column exists in api_keys table
check_masked_key = """
SELECT column_name
FROM information_schema.columns
WHERE table_name='api_keys' AND column_name='masked_key'
"""
result = db.execute(text(check_masked_key)).fetchone()
if not result:
print("Adding masked_key column to api_keys table...")
db.execute(text("ALTER TABLE api_keys ADD COLUMN masked_key VARCHAR"))
# Check if last_used column exists in api_keys table
check_last_used = """
SELECT column_name
FROM information_schema.columns
WHERE table_name='api_keys' AND column_name='last_used'
"""
result = db.execute(text(check_last_used)).fetchone()
if not result:
print("Adding last_used column to api_keys table...")
db.execute(text("ALTER TABLE api_keys ADD COLUMN last_used TIMESTAMP"))
# Check if customer_id is NULLable
check_nullable = """
SELECT is_nullable
FROM information_schema.columns
WHERE table_name='api_keys' AND column_name='customer_id'
"""
result = db.execute(text(check_nullable)).fetchone()
if result and result[0] == 'NO': # 'NO' means NOT NULL
print("Making customer_id column nullable...")
db.execute(text("ALTER TABLE api_keys ALTER COLUMN customer_id DROP NOT NULL"))
# Check if usage_records table exists
check_table = """
SELECT table_name
FROM information_schema.tables
WHERE table_name='usage_records'
"""
result = db.execute(text(check_table)).fetchone()
if not result:
print("Creating usage_records table...")
db.execute(text("""
CREATE TABLE usage_records (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
api_key_id INTEGER REFERENCES api_keys(id),
service VARCHAR(100) NOT NULL,
request_count INTEGER DEFAULT 1,
cost FLOAT DEFAULT 0.0,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
else:
# SQLite version
print("Using SQLite migration...")
# Check if user_id column exists in api_keys table
check_user_id = "SELECT COUNT(*) FROM pragma_table_info('api_keys') WHERE name='user_id'"
result = db.execute(text(check_user_id)).scalar()
if result == 0:
print("Adding user_id column to api_keys table...")
db.execute(text("ALTER TABLE api_keys ADD COLUMN user_id INTEGER REFERENCES users(id)"))
# Check if masked_key column exists in api_keys table
check_masked_key = "SELECT COUNT(*) FROM pragma_table_info('api_keys') WHERE name='masked_key'"
result = db.execute(text(check_masked_key)).scalar()
if result == 0:
print("Adding masked_key column to api_keys table...")
db.execute(text("ALTER TABLE api_keys ADD COLUMN masked_key VARCHAR"))
# Check if last_used column exists in api_keys table
check_last_used = "SELECT COUNT(*) FROM pragma_table_info('api_keys') WHERE name='last_used'"
result = db.execute(text(check_last_used)).scalar()
if result == 0:
print("Adding last_used column to api_keys table...")
db.execute(text("ALTER TABLE api_keys ADD COLUMN last_used TIMESTAMP"))
# Create usage_records table if it doesn't exist
db.execute(text("""
CREATE TABLE IF NOT EXISTS usage_records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL REFERENCES users(id),
api_key_id INTEGER REFERENCES api_keys(id),
service VARCHAR(100) NOT NULL,
request_count INTEGER DEFAULT 1,
cost FLOAT DEFAULT 0.0,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""))
# Commit the transaction
db.commit()
print("Database migration completed successfully!")
except Exception as e:
db.rollback()
print(f"Error during migration: {str(e)}")
sys.exit(1)
finally:
db.close()