mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 17:47:06 +08:00
* stage * stage * refactor: using sqlchemy as ORM framework, switch to async-based sqlite operation - using sqlmodel as ORM(based on sqlchemy and pydantic) - add Persona, Preference, PlatformMessageHistory table * fix: conversation * fix: remove redundant explicit session.commit, and fix some type error * fix: conversation context issue * chore: remove comments * chore: remove exclude_content param
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
import os
|
||
from astrbot.core.utils.astrbot_path import get_astrbot_data_path
|
||
from astrbot.core.db import BaseDatabase
|
||
from astrbot.api import logger
|
||
from .migra_3_to_4 import (
|
||
migration_conversation_table,
|
||
migration_platform_table,
|
||
migration_webchat_data,
|
||
)
|
||
|
||
|
||
async def check_migration_needed_v4(db_helper: BaseDatabase) -> bool:
|
||
"""
|
||
检查是否需要进行数据库迁移
|
||
如果存在 data_v3.db 并且 preference 中没有 migration_done_v4,则需要进行迁移。
|
||
"""
|
||
data_v3_exists = os.path.exists(get_astrbot_data_path())
|
||
if not data_v3_exists:
|
||
return False
|
||
migration_done = await db_helper.get_preference("migration_done_v4")
|
||
if migration_done:
|
||
return False
|
||
return True
|
||
|
||
|
||
async def do_migration_v4(
|
||
db_helper: BaseDatabase, platform_id_map: dict[str, dict[str, str]]
|
||
):
|
||
"""
|
||
执行数据库迁移
|
||
迁移旧的 webchat_conversation 表到新的 conversation 表。
|
||
迁移旧的 platform 到新的 platform_stats 表。
|
||
"""
|
||
if not await check_migration_needed_v4(db_helper):
|
||
return
|
||
|
||
logger.info("开始执行数据库迁移...")
|
||
|
||
# 执行会话表迁移
|
||
await migration_conversation_table(db_helper, platform_id_map)
|
||
|
||
# 执行平台统计表迁移
|
||
await migration_platform_table(db_helper, platform_id_map)
|
||
|
||
# 执行 WebChat 数据迁移
|
||
await migration_webchat_data(db_helper, platform_id_map)
|
||
|
||
# 标记迁移完成
|
||
await db_helper.insert_preference_or_update("migration_done_v4", "true")
|
||
|
||
logger.info("数据库迁移完成。")
|