From 2d6670145e39bb4d8154d559e08e66493afe47e1 Mon Sep 17 00:00:00 2001 From: mio <455457521@qq.com> Date: Thu, 17 Apr 2025 14:12:29 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=20=E4=BF=AE=E5=A4=8D=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E6=97=B6=E6=95=B0=E6=8D=AE=E7=9A=84=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zhenxun/builtin_plugins/init/init_plugin.py | 113 ++++++++++++-------- 1 file changed, 68 insertions(+), 45 deletions(-) diff --git a/zhenxun/builtin_plugins/init/init_plugin.py b/zhenxun/builtin_plugins/init/init_plugin.py index dbeddb54..5de78a96 100644 --- a/zhenxun/builtin_plugins/init/init_plugin.py +++ b/zhenxun/builtin_plugins/init/init_plugin.py @@ -95,11 +95,34 @@ async def _handle_setting( ) +async def fix_db_schema(): + """修复数据库架构问题""" + from tortoise import connections + conn = connections.get("default") + # 检查是否存在superuser列并处理 + try: + await conn.execute_query(""" + DO $$ + BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns + WHERE table_name='plugin_info' AND column_name='superuser') THEN + ALTER TABLE plugin_info DROP COLUMN superuser; + END IF; + END $$; + """) + logger.info("数据库架构检查完成") + except Exception as e: + logger.error(f"数据库架构修复失败: {e}") + + @driver.on_startup async def _(): """ 初始化插件数据配置 """ + # 修复数据库架构问题 + await fix_db_schema() + plugin_list: list[PluginInfo] = [] limit_list: list[PluginLimit] = [] module2id = {} @@ -116,6 +139,7 @@ async def _(): create_list.append(plugin) else: plugin.id = module2id[plugin.module_path] + # 确保menu_type字段包含在更新列表中 await plugin.save( update_fields=[ "name", @@ -124,39 +148,26 @@ async def _(): "admin_level", "plugin_type", "is_show", + "menu_type", # 添加menu_type到更新列表 ] ) + + # 验证更新是否成功 + updated_plugin = await PluginInfo.get(id=plugin.id) + if updated_plugin.menu_type != plugin.menu_type: + logger.warning(f"插件 {plugin.name} 的menu_type更新失败: 期望值 '{plugin.menu_type}', 实际值 '{updated_plugin.menu_type}'") + # 尝试单独更新menu_type + updated_plugin.menu_type = plugin.menu_type + await updated_plugin.save(update_fields=["menu_type"]) + update_list.append(plugin) + if create_list: await PluginInfo.bulk_create(create_list, 10) - # if update_list: - # # TODO: 批量更新无法更新plugin_type: tortoise.exceptions.OperationalError: - # column "superuser" does not exist - # pass - # await PluginInfo.bulk_update( - # update_list, - # ["name", "author", "version", "admin_level", "plugin_type"], - # 10, - # ) - # for limit in limit_list: - # limit_create = [] - # plugins = [] - # if module_path_list := [limit.module_path for limit in limit_list]: - # plugins = await PluginInfo.get_plugins(module_path__in=module_path_list) - # if plugins: - # for limit in limit_list: - # if lmt := [p for p in plugins if p.module_path == limit.module_path]: - # plugin = lmt[0] - # """不在数据库中""" - # limit_type_list = [ - # _limit.limit_type - # for _limit in await plugin.plugin_limit.all() # type: ignore - # ] - # if limit.limit_type not in limit_type_list: - # limit.plugin = plugin - # limit_create.append(limit) - # if limit_create: - # await PluginLimit.bulk_create(limit_create, 10) + + # 对于批量更新操作,逐个更新替代批量操作 + # 这里不使用被注释的批量更新代码,而是在上面的循环中已经处理 + await data_migration() await PluginInfo.filter(module_path__in=load_plugin).update(load_status=True) await PluginInfo.filter(module_path__not_in=load_plugin).update(load_status=False) @@ -241,6 +252,7 @@ async def limit_migration(): limit.cd = _limit["cd"] if limit.watch_type == PluginLimitType.COUNT: limit.max_count = _limit["count"] + # 改为逐个保存 await limit.save() update_list.append(limit) for s in [e for e in exits_limit if e not in _not_create_type]: @@ -275,8 +287,7 @@ async def limit_migration(): max_count=_limit.get("max_count"), ) ) - # TODO: 批量错误 tortoise.exceptions.OperationalError: - # syntax error at or near "ALL" + # 注释掉批量更新,使用单个保存方式 # if update_list: # await PluginLimit.bulk_update( # update_list, @@ -317,17 +328,28 @@ async def plugin_migration(): ) plugin.menu_type = plugin_data.get("plugin_type", ["功能"])[0] plugin.cost_gold = plugin_data.get("cost_gold", 0) - await PluginInfo.bulk_update( - plugins, - [ - "default_status", - "level", - "limit_superuser", - "menu_type", - "cost_gold", - ], - 10, - ) + # 逐个保存替代批量更新 + await plugin.save( + update_fields=[ + "default_status", + "level", + "limit_superuser", + "menu_type", + "cost_gold", + ] + ) + # 注释掉批量更新,已在循环中处理 + # await PluginInfo.bulk_update( + # plugins, + # [ + # "default_status", + # "level", + # "limit_superuser", + # "menu_type", + # "cost_gold", + # ], + # 10, + # ) setting_file.unlink() logger.info("迁移插件setting数据完成!") if plugin_file.exists(): @@ -347,9 +369,9 @@ async def plugin_migration(): elif get_block == "group": block_type = BlockType.GROUP plugin.block_type = block_type + # 逐个保存替代批量更新 await plugin.save(update_fields=["status", "block_type"]) - # TODO: tortoise.exceptions.OperationalError: syntax error at - # or near "ALL" + # 注释掉批量更新,已在循环中处理 # await PluginInfo.bulk_update(plugins, ["status", "block_type"], 10) plugin_file.unlink() logger.info("迁移插件数据完成!") @@ -408,12 +430,13 @@ async def group_migration(): ) ) if update_list: + # 使用批量更新,因为这里的字段类型不会导致SQL错误 await GroupConsole.bulk_update( update_list, - ["is_super", "status", "block_plugin", "block_task"], + ["is_super", "status", "block_plugin", "block_task", "level"], 10, ) if create_list: await GroupConsole.bulk_create(create_list, 10) group_file.unlink() - logger.info("迁移群组数据完成!") + logger.info("迁移群组数据完成!") \ No newline at end of file