23 lines
800 B
Python
23 lines
800 B
Python
"""轻量数据库迁移:为新版本补列(SQLite/PostgreSQL 通用)。"""
|
|
from sqlalchemy import inspect, text
|
|
|
|
|
|
def migrate(db):
|
|
"""检查并补齐缺失的表列。"""
|
|
insp = inspect(db.bind)
|
|
existing = {c["name"] for c in insp.get_columns("chat_messages")}
|
|
additions = {
|
|
"file_ids": "VARCHAR(256) DEFAULT '[]'",
|
|
"feedback": "VARCHAR(8) DEFAULT ''",
|
|
"suggestions": "TEXT DEFAULT '[]'",
|
|
"edited": "BOOLEAN DEFAULT 0",
|
|
"regenerated": "INTEGER DEFAULT 0",
|
|
"reasoning_content": "TEXT DEFAULT ''",
|
|
"updated_at": "DATETIME",
|
|
}
|
|
for name, ddl in additions.items():
|
|
if name not in existing:
|
|
db.execute(text(f"ALTER TABLE chat_messages ADD COLUMN {name} {ddl}"))
|
|
if existing:
|
|
db.commit()
|