mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-09 22:00:01 +08:00
🐛 fix(db): 优化多数据库脚本兼容性与初始化错误处理 (#2095)
* 🐛 fix(db): 优化多数据库脚本兼容性与初始化错误处理 - 为 `BotConsole`、`GroupConsole` 和 `GroupInfo` 模型增加针对 PostgreSQL 和 MySQL 的字段类型转换脚本 - 统一将插件和任务相关的禁用/可用字段类型修改为 `TEXT` - 优化数据库初始化逻辑,增加对 `OperationalError` 的细粒度处理,忽略常见的重复执行错误 - 简化 `db_context` 内部配置解析逻辑 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: webjoin111 <455457521@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: HibiKier <45528451+HibiKier@users.noreply.github.com>
This commit is contained in:
co-authored by
webjoin111
pre-commit-ci[bot]
HibiKier
parent
4f152638b0
commit
b95acce800
@@ -7,6 +7,7 @@ import nonebot
|
||||
from nonebot.utils import is_coroutine_callable
|
||||
from tortoise import Tortoise
|
||||
from tortoise.connection import connections
|
||||
from tortoise.exceptions import OperationalError
|
||||
|
||||
from zhenxun.configs.config import BotConfig
|
||||
from zhenxun.services.log import logger
|
||||
@@ -50,11 +51,8 @@ def get_config() -> dict:
|
||||
raise DbUrlIsNode("数据库Url连接字符串为空,请检查配置文件(.env.dev)")
|
||||
parsed = urlparse(BotConfig.db_url)
|
||||
|
||||
# 基础配置
|
||||
config = {
|
||||
"connections": {
|
||||
"default": BotConfig.db_url # 默认直接使用连接字符串
|
||||
},
|
||||
"connections": {"default": BotConfig.db_url},
|
||||
"apps": {
|
||||
"models": {
|
||||
"models": db_model.models,
|
||||
@@ -64,7 +62,6 @@ def get_config() -> dict:
|
||||
"timezone": "Asia/Shanghai",
|
||||
}
|
||||
|
||||
# 根据数据库类型应用高级配置
|
||||
if parsed.scheme.startswith("postgres"):
|
||||
config["connections"]["default"] = {
|
||||
"engine": "tortoise.backends.asyncpg",
|
||||
@@ -143,7 +140,24 @@ async def init():
|
||||
await asyncio.wait_for(
|
||||
db.execute_query_dict(sql), timeout=DB_TIMEOUT_SECONDS
|
||||
)
|
||||
# await TestSQL.raw(sql)
|
||||
except OperationalError as e:
|
||||
err_str = str(e).lower()
|
||||
if any(
|
||||
x in err_str
|
||||
for x in [
|
||||
"already exists",
|
||||
"duplicate column",
|
||||
"已经存在",
|
||||
"已存在",
|
||||
]
|
||||
):
|
||||
pass
|
||||
elif any(
|
||||
x in err_str for x in ["does not exist", "check that", "不存在"]
|
||||
) and ("drop" in sql.lower() or "rename" in sql.lower()):
|
||||
pass
|
||||
else:
|
||||
logger.warning(f"执行SQL警告: {sql} || {e}")
|
||||
except Exception as e:
|
||||
logger.debug(f"执行SQL: {sql} 错误...", e=e)
|
||||
if sql_list:
|
||||
|
||||
Reference in New Issue
Block a user