-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmigrate_data.py
More file actions
74 lines (56 loc) · 2.63 KB
/
migrate_data.py
File metadata and controls
74 lines (56 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
import sqlite3
import os
import datetime
# --- Configuration ---
OLD_DB_PATH = 'seeDBot_OLD.sqlite'
NEW_DB_PATH = 'seeDBot.sqlite'
# -------------------
def migrate_table(old_cursor, new_conn, new_cursor, table_name, column_names):
print(f"Migrating data for table: {table_name}...")
old_cursor.execute(f"SELECT {', '.join(column_names)} FROM {table_name}")
all_rows = old_cursor.fetchall()
if not all_rows:
print(f"No data found in old '{table_name}' table. Skipping.")
return
# --- FIX: Handle potential NULL values in created_at for presets table ---
if table_name == 'presets':
processed_rows = []
# created_at is the 4th column (index 3)
for row in all_rows:
row_list = list(row)
if not row_list[3]: # If created_at is None or empty
row_list[3] = datetime.datetime.now().strftime("%b %d %Y %H:%M:%S")
processed_rows.append(tuple(row_list))
all_rows = processed_rows
# --- END FIX ---
placeholders = ', '.join(['?'] * len(column_names))
insert_sql = f"INSERT INTO {table_name} ({', '.join(column_names)}) VALUES ({placeholders})"
new_cursor.executemany(insert_sql, all_rows)
new_conn.commit()
print(f"Successfully migrated {len(all_rows)} rows to '{table_name}'.")
def main():
if not os.path.exists(OLD_DB_PATH):
print(f"Error: Old database file not found at '{OLD_DB_PATH}'")
return
if not os.path.exists(NEW_DB_PATH):
print(f"Error: New database file not found at '{NEW_DB_PATH}'. Please run 'python manage.py migrate' first.")
return
old_conn = sqlite3.connect(OLD_DB_PATH)
old_cursor = old_conn.cursor()
new_conn = sqlite3.connect(NEW_DB_PATH)
new_cursor = new_conn.cursor()
try:
preset_cols = ["preset_name", "creator_id", "creator_name", "created_at", "flags", "description", "arguments", "official", "hidden", "gen_count"]
migrate_table(old_cursor, new_conn, new_cursor, "presets", preset_cols)
user_cols = ["user_id", "bot_admin", "git_user", "race_admin"]
migrate_table(old_cursor, new_conn, new_cursor, "users", user_cols)
seedlist_cols = ["creator_id", "creator_name", "seed_type", "share_url", "timestamp", "server_name", "server_id", "channel_name", "channel_id"]
migrate_table(old_cursor, new_conn, new_cursor, "seedlist", seedlist_cols)
print("\nData migration complete!")
except Exception as e:
print(f"\nAn error occurred during migration: {e}")
finally:
old_conn.close()
new_conn.close()
if __name__ == '__main__':
main()