-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_db.py
More file actions
85 lines (64 loc) · 3.36 KB
/
migrate_db.py
File metadata and controls
85 lines (64 loc) · 3.36 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
#!/usr/bin/env python3
"""
Database migration script to add error handling fields to the Presentation model
"""
import sqlite3
import sys
import os
def migrate_database():
db_path = 'instance/presentations.db'
if not os.path.exists(db_path):
print(f"Database file {db_path} does not exist. No migration needed.")
return
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Check if the new columns already exist
cursor.execute("PRAGMA table_info(presentation)")
columns = [column[1] for column in cursor.fetchall()]
new_columns = ['last_error_message', 'last_error_time', 'failed_context', 'retry_after', 'is_deleted', 'deleted_at', 'deleted_by_user_id', 'additional_info', 'live_info_visible', 'feedback_disabled']
columns_to_add = [col for col in new_columns if col not in columns]
if not columns_to_add:
print("Database already up to date. No migration needed.")
return
print(f"Adding new columns: {columns_to_add}")
# Add new columns for error handling
if 'last_error_message' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN last_error_message TEXT")
print("Added last_error_message column")
if 'last_error_time' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN last_error_time DATETIME")
print("Added last_error_time column")
if 'failed_context' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN failed_context TEXT")
print("Added failed_context column")
if 'retry_after' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN retry_after DATETIME")
print("Added retry_after column")
if 'is_deleted' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN is_deleted BOOLEAN DEFAULT 0 NOT NULL")
print("Added is_deleted column")
if 'deleted_at' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN deleted_at DATETIME")
print("Added deleted_at column")
if 'deleted_by_user_id' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN deleted_by_user_id INTEGER")
print("Added deleted_by_user_id column")
if 'additional_info' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN additional_info TEXT")
print("Added additional_info column")
if 'live_info_visible' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN live_info_visible BOOLEAN DEFAULT 0 NOT NULL")
print("Added live_info_visible column")
if 'feedback_disabled' in columns_to_add:
cursor.execute("ALTER TABLE presentation ADD COLUMN feedback_disabled BOOLEAN DEFAULT 0 NOT NULL")
print("Added feedback_disabled column")
conn.commit()
print("Database migration completed successfully!")
except sqlite3.Error as e:
print(f"Database migration failed: {e}")
sys.exit(1)
finally:
conn.close()
if __name__ == "__main__":
migrate_database()