-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_sqlite_to_pg.py
More file actions
305 lines (258 loc) · 10.3 KB
/
migrate_sqlite_to_pg.py
File metadata and controls
305 lines (258 loc) · 10.3 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
Migrate data from SQLite to PostgreSQL.
Run inside the pod:
kubectl exec -it <pod> -n trader-tools -- python /app/migrate_sqlite_to_pg.py
Or with explicit args:
python migrate_sqlite_to_pg.py [sqlite_path] [postgresql_url]
Defaults:
sqlite_path = /data/financial_analysis.db
postgresql_url = DATABASE_URL env var
"""
import os
import sys
import sqlite3
import psycopg2
from urllib.parse import quote_plus
SQLITE_PATH = os.getenv('SQLITE_PATH', '/data/financial_analysis.db')
# Tables in dependency order (parents before children)
TABLES = [
'users',
'user_sessions',
'watchlist',
'portfolio_accounts',
'portfolio',
'transactions',
'options_positions',
'alerts',
'analysis_history',
'ml_patterns',
'ml_predictions',
'monitoring_log',
'market_conditions',
'portfolio_snapshots',
'alert_suggestions',
'dividends',
'discussion_threads',
'thread_replies',
'thread_votes',
'copy_trading_follows',
]
def get_sqlite_tables(sqlite_conn):
"""Get list of tables that actually exist in SQLite."""
cursor = sqlite_conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")
return {row[0] for row in cursor.fetchall()}
def get_columns(sqlite_conn, table):
"""Get column names for a table."""
cursor = sqlite_conn.execute(f"PRAGMA table_info(\"{table}\")")
return [row[1] for row in cursor.fetchall()]
def _build_pg_url(raw):
"""Build psycopg2-compatible URL with encoded password."""
if raw.startswith('postgres://'):
raw = 'postgresql://' + raw[len('postgres://'):]
rest = raw.split('://', 1)[1]
creds, hostpart = rest.rsplit('@', 1)
user, password = creds.split(':', 1)
if '/' in hostpart:
host_port, db = hostpart.split('/', 1)
else:
host_port, db = hostpart, 'postgres'
if ':' in host_port:
host, port = host_port.rsplit(':', 1)
else:
host, port = host_port, '5432'
return f"postgresql://{user}:{quote_plus(password)}@{host}:{port}/{db}"
def migrate_table(sqlite_conn, pg_conn, table):
"""Migrate a single table from SQLite to PostgreSQL."""
columns = get_columns(sqlite_conn, table)
if not columns:
print(f" ⚠ No columns found for {table}, skipping")
return 0
# Read all rows from SQLite
cursor = sqlite_conn.execute(f"SELECT * FROM \"{table}\"")
rows = cursor.fetchall()
if not rows:
print(f" ℹ {table}: 0 rows (empty)")
return 0
# Check which columns exist in PostgreSQL and their types
pg_cursor = pg_conn.cursor()
pg_cursor.execute(
"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = %s AND table_schema = 'public'",
(table,)
)
pg_col_types = {row[0]: row[1] for row in pg_cursor.fetchall()}
pg_columns = set(pg_col_types.keys())
# Only use columns that exist in both
common_cols = [c for c in columns if c in pg_columns]
col_indices = [columns.index(c) for c in common_cols]
if not common_cols:
print(f" ⚠ No matching columns for {table}, skipping")
return 0
# Print detected column types for debugging
print(f" [DEBUG] {table} column types: {pg_col_types}")
# Fallback: explicit known boolean columns by table name
explicit_bool_map = {
'users': {'is_active', 'copy_trading_enabled'},
'alerts': {'triggered', 'enabled'},
'dividends': {'reinvested'},
'discussion_threads': {'pinned', 'locked'},
}
bool_cols = [col for col in common_cols if pg_col_types.get(col) == 'boolean']
# Add explicit known bools if present
for col in explicit_bool_map.get(table, set()):
if col in common_cols and col not in bool_cols:
bool_cols.append(col)
# Filter and convert rows to only include common columns, with bool conversion
filtered_rows = []
for row in rows:
new_row = list(row[i] for i in col_indices)
for idx, col in enumerate(common_cols):
if col in bool_cols:
val = new_row[idx]
if val is None:
new_row[idx] = None
elif isinstance(val, bool):
new_row[idx] = val
elif isinstance(val, int):
new_row[idx] = bool(val)
elif isinstance(val, str):
if val in ('0', '1'):
new_row[idx] = val == '1'
elif val.lower() in ('true', 'false'):
new_row[idx] = val.lower() == 'true'
filtered_rows.append(tuple(new_row))
# Build INSERT with ON CONFLICT DO NOTHING to skip duplicates
col_list = ', '.join(f'"{c}"' for c in common_cols)
placeholders = ', '.join(['%s'] * len(common_cols))
insert_sql = f'INSERT INTO "{table}" ({col_list}) VALUES ({placeholders}) ON CONFLICT DO NOTHING'
# Insert in batches
batch_size = 500
inserted = 0
for i in range(0, len(filtered_rows), batch_size):
batch = filtered_rows[i:i + batch_size]
for row in batch:
try:
pg_cursor.execute(insert_sql, row)
inserted += 1
except Exception as e:
pg_conn.rollback()
print(f" ⚠ Error inserting row in {table}: {e}")
continue
pg_conn.commit()
# Reset sequence to max id
if 'id' in common_cols:
seq_name = f"{table}_id_seq"
try:
pg_cursor.execute(f"SELECT setval('{seq_name}', COALESCE((SELECT MAX(id) FROM \"{table}\"), 0) + 1, false)")
pg_conn.commit()
except Exception:
pg_conn.rollback()
print(f" ✓ {table}: {inserted}/{len(rows)} rows migrated")
return inserted
def main():
sqlite_path = sys.argv[1] if len(sys.argv) > 1 else SQLITE_PATH
pg_raw = sys.argv[2] if len(sys.argv) > 2 else os.getenv('DATABASE_URL', '')
if not pg_raw:
print("Error: No PostgreSQL URL. Provide as arg or set DATABASE_URL env var.")
sys.exit(1)
if not os.path.exists(sqlite_path):
print(f"Error: SQLite file not found: {sqlite_path}")
sys.exit(1)
pg_url = _build_pg_url(pg_raw)
print(f"SQLite source: {sqlite_path}")
print(f"PostgreSQL target: {pg_url.split('@')[1] if '@' in pg_url else pg_url}")
print()
# Connect to SQLite
sqlite_conn = sqlite3.connect(sqlite_path)
def migrate_table(sqlite_conn, pg_conn, table):
"""Migrate a single table from SQLite to PostgreSQL."""
columns = get_columns(sqlite_conn, table)
if not columns:
print(f" ⚠ No columns found for {table}, skipping")
return 0
# Read all rows from SQLite
cursor = sqlite_conn.execute(f"SELECT * FROM \"{table}\"")
rows = cursor.fetchall()
if not rows:
print(f" ℹ {table}: 0 rows (empty)")
return 0
# Check which columns exist in PostgreSQL
pg_cursor = pg_conn.cursor()
pg_cursor.execute(
"SELECT column_name FROM information_schema.columns WHERE table_name = %s AND table_schema = 'public'",
(table,)
)
pg_columns = {row[0] for row in pg_cursor.fetchall()}
# Only use columns that exist in both
common_cols = [c for c in columns if c in pg_columns]
col_indices = [columns.index(c) for c in common_cols]
if not common_cols:
print(f" ⚠ No matching columns for {table}, skipping")
return 0
# Filter rows to only include common columns
filtered_rows = []
for row in rows:
filtered_rows.append(tuple(row[i] for i in col_indices))
# Build INSERT with ON CONFLICT DO NOTHING to skip duplicates
col_list = ', '.join(f'"{c}"' for c in common_cols)
placeholders = ', '.join(['%s'] * len(common_cols))
insert_sql = f'INSERT INTO "{table}" ({col_list}) VALUES ({placeholders}) ON CONFLICT DO NOTHING'
# Insert in batches
batch_size = 500
inserted = 0
for i in range(0, len(filtered_rows), batch_size):
batch = filtered_rows[i:i + batch_size]
for row in batch:
try:
pg_cursor.execute(insert_sql, row)
inserted += 1
except Exception as e:
pg_conn.rollback()
print(f" ⚠ Error inserting row in {table}: {e}")
continue
pg_cursor.execute(
"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = %s AND table_schema = 'public'",
(table,)
)
pg_col_types = {row[0]: row[1] for row in pg_cursor.fetchall()}
pg_columns = set(pg_col_types.keys())
# Only use columns that exist in both
common_cols = [c for c in columns if c in pg_columns]
col_indices = [columns.index(c) for c in common_cols]
if not common_cols:
print(f" ⚠ No matching columns for {table}, skipping")
return 0
# Print detected column types for debugging
print(f" [DEBUG] {table} column types: {pg_col_types}")
# Fallback: explicit known boolean columns by table name
explicit_bool_map = {
'users': {'is_active', 'copy_trading_enabled'},
'alerts': {'triggered', 'enabled'},
'dividends': {'reinvested'},
'discussion_threads': {'pinned', 'locked'},
}
bool_cols = [col for col in common_cols if pg_col_types.get(col) == 'boolean']
# Add explicit known bools if present
for col in explicit_bool_map.get(table, set()):
if col in common_cols and col not in bool_cols:
bool_cols.append(col)
# Filter and convert rows to only include common columns, with bool conversion
filtered_rows = []
for row in rows:
new_row = list(row[i] for i in col_indices)
for idx, col in enumerate(common_cols):
if col in bool_cols:
val = new_row[idx]
if val is None:
new_row[idx] = None
elif isinstance(val, bool):
new_row[idx] = val
elif isinstance(val, int):
new_row[idx] = bool(val)
elif isinstance(val, str):
if val in ('0', '1'):
new_row[idx] = val == '1'
elif val.lower() in ('true', 'false'):
new_row[idx] = val.lower() == 'true'
filtered_rows.append(tuple(new_row))
if __name__ == '__main__':
main()