feat: 更新机器人配置并添加数据库迁移脚本

- 将bot_config_template.toml中的版本升级至7.9.0
- 增强数据库配置选项以支持PostgreSQL
- 引入一个新脚本,用于在SQLite、MySQL和PostgreSQL之间迁移数据
- 实现一个方言适配器,用于处理特定于数据库的行为和配置
This commit is contained in:
Windpicker-owo
2025-11-27 18:45:01 +08:00
parent 369639a8f1
commit 43483b934e
30 changed files with 1658 additions and 2226 deletions

View File

@@ -238,6 +238,14 @@ class BatchDatabaseWriter:
stmt = stmt.on_duplicate_key_update(
**{key: value for key, value in update_data.items() if key != "stream_id"}
)
elif global_config.database.database_type == "postgresql":
from sqlalchemy.dialects.postgresql import insert as pg_insert
stmt = pg_insert(ChatStreams).values(stream_id=stream_id, **update_data)
stmt = stmt.on_conflict_do_update(
index_elements=[ChatStreams.stream_id],
set_=update_data
)
else:
# 默认使用SQLite语法
from sqlalchemy.dialects.sqlite import insert as sqlite_insert
@@ -264,6 +272,14 @@ class BatchDatabaseWriter:
stmt = stmt.on_duplicate_key_update(
**{key: value for key, value in update_data.items() if key != "stream_id"}
)
elif global_config.database.database_type == "postgresql":
from sqlalchemy.dialects.postgresql import insert as pg_insert
stmt = pg_insert(ChatStreams).values(stream_id=stream_id, **update_data)
stmt = stmt.on_conflict_do_update(
index_elements=[ChatStreams.stream_id],
set_=update_data
)
else:
from sqlalchemy.dialects.sqlite import insert as sqlite_insert

View File

@@ -4,6 +4,7 @@ import time
from rich.traceback import install
from sqlalchemy.dialects.mysql import insert as mysql_insert
from sqlalchemy.dialects.postgresql import insert as pg_insert
from sqlalchemy.dialects.sqlite import insert as sqlite_insert
from src.common.data_models.database_data_model import DatabaseGroupInfo,DatabaseUserInfo
@@ -663,6 +664,13 @@ class ChatManager:
stmt = stmt.on_duplicate_key_update(
**{key: value for key, value in fields_to_save.items() if key != "stream_id"}
)
elif global_config.database.database_type == "postgresql":
stmt = pg_insert(ChatStreams).values(stream_id=s_data_dict["stream_id"], **fields_to_save)
# PostgreSQL 需要使用 constraint 参数或正确的 index_elements
stmt = stmt.on_conflict_do_update(
index_elements=[ChatStreams.stream_id],
set_=fields_to_save
)
else:
stmt = sqlite_insert(ChatStreams).values(stream_id=s_data_dict["stream_id"], **fields_to_save)
stmt = stmt.on_conflict_do_update(index_elements=["stream_id"], set_=fields_to_save)

View File

@@ -142,8 +142,11 @@ class MessageStorageBatcher:
return None
# 将ORM对象转换为字典只包含列字段
# 排除 id 字段,让数据库自动生成(对于 PostgreSQL SERIAL 类型尤其重要)
message_dict = {}
for column in Messages.__table__.columns:
if column.name == "id":
continue # 跳过自增主键,让数据库自动生成
message_dict[column.name] = getattr(message_obj, column.name)
return message_dict

View File

@@ -1143,7 +1143,6 @@ class Prompt:
Returns:
str: 构建好的跨群聊上下文字符串。
"""
logger.info(f"Building cross context with target_user_info: {target_user_info}")
if not global_config.cross_context.enable:
return ""