Repository navigation
Expand file tree
/
Copy pathupdate_db.py
More file actions
45 lines (38 loc) · 1.79 KB
/
Copy pathupdate_db.py
File metadata and controls
45 lines (38 loc) · 1.79 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
from extensions import app, db
from sqlalchemy import inspect
def update_database():
with app.app_context():
inspector = inspect(db.engine)
# 检查 Message 表是否存在 is_recalled 列
if 'message' in inspector.get_table_names():
columns = [col['name'] for col in inspector.get_columns('message')]
if 'is_recalled' not in columns:
print("检测到 Message 表缺少 is_recalled 列,正在添加...")
try:
db.session.execute(db.text('ALTER TABLE message ADD COLUMN is_recalled BOOLEAN DEFAULT 0'))
db.session.commit()
print("✓ is_recalled 列已成功添加")
except Exception as e:
print(f"添加列时出错: {e}")
db.session.rollback()
else:
print("Message 表结构正常,is_recalled 列已存在")
# 检查其他可能缺失的列
tables_to_check = {
'user': ['bio', 'updated_at'],
'message': ['reply_to'],
'chat_room': ['updated_at'],
'chat_room_member': ['joined_at'],
'forbidden_word': ['created_at'],
'chat_room_link_setting': ['updated_at'],
'quick_phrase': ['updated_at']
}
for table, columns in tables_to_check.items():
if table in inspector.get_table_names():
existing_columns = [col['name'] for col in inspector.get_columns(table)]
for col in columns:
if col not in existing_columns:
print(f"警告: {table} 表可能缺少 {col} 列")
print("\n数据库结构检查完成")
if __name__ == '__main__':
update_database()