-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_sqlite.py
More file actions
55 lines (45 loc) · 1.67 KB
/
Copy pathmigrate_sqlite.py
File metadata and controls
55 lines (45 loc) · 1.67 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
import sqlite3
import sys
db_path = "cruk_datahub.db"
def migrate():
print(f"Connecting to {db_path}...")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
try:
print("Disabling foreign keys...")
cursor.execute("PRAGMA foreign_keys=OFF;")
cursor.execute("BEGIN TRANSACTION;")
print("Creating users_new table without team_id...")
cursor.execute("""
CREATE TABLE users_new (
id INTEGER NOT NULL,
email VARCHAR,
name VARCHAR,
hashed_password VARCHAR,
PRIMARY KEY (id)
);
""")
print("Copying data from users to users_new...")
cursor.execute("""
INSERT INTO users_new (id, email, name, hashed_password)
SELECT id, email, name, hashed_password FROM users;
""")
print("Dropping legacy users table...")
cursor.execute("DROP TABLE users;")
print("Renaming users_new to users...")
cursor.execute("ALTER TABLE users_new RENAME TO users;")
print("Recreating indexes...")
cursor.execute("CREATE INDEX ix_users_id ON users (id);")
cursor.execute("CREATE UNIQUE INDEX ix_users_email ON users (email);")
cursor.execute("COMMIT;")
print("✅ Successfully migrated local SQLite database! legacy team_id column removed.")
except Exception as e:
cursor.execute("ROLLBACK;")
print(f"❌ Migration failed: {e}")
sys.exit(1)
finally:
print("Re-enabling foreign keys...")
cursor.execute("PRAGMA foreign_keys=ON;")
conn.close()
if __name__ == "__main__":
migrate()