mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +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
@@ -4,6 +4,7 @@ from typing_extensions import Self
|
||||
from tortoise import fields
|
||||
from tortoise.backends.base.client import BaseDBAsyncClient
|
||||
|
||||
from zhenxun.configs.config import BotConfig
|
||||
from zhenxun.models.plugin_info import PluginInfo
|
||||
from zhenxun.models.task_info import TaskInfo
|
||||
from zhenxun.services.cache import CacheRoot
|
||||
@@ -514,11 +515,52 @@ class GroupConsole(Model):
|
||||
|
||||
@classmethod
|
||||
def _run_script(cls):
|
||||
return [
|
||||
db_type = (BotConfig.get_sql_type() or "").lower()
|
||||
|
||||
scripts = [
|
||||
"ALTER TABLE group_console ADD superuser_block_plugin"
|
||||
" character varying(255) NOT NULL DEFAULT '';",
|
||||
" Text NOT NULL DEFAULT '';",
|
||||
"ALTER TABLE group_console ADD superuser_block_task"
|
||||
" character varying(255) NOT NULL DEFAULT '';",
|
||||
" Text NOT NULL DEFAULT '';",
|
||||
"CREATE INDEX idx_group_console_group_id ON group_console(group_id);",
|
||||
"CREATE INDEX idx_group_console_group_null_channel ON group_console(group_id) WHERE channel_id IS NULL;", # 单独创建channel为空的索引 # noqa: E501
|
||||
(
|
||||
"CREATE INDEX idx_group_console_group_null_channel ON "
|
||||
"group_console(group_id) WHERE channel_id IS NULL;"
|
||||
),
|
||||
]
|
||||
|
||||
if "postgres" in db_type:
|
||||
scripts.extend(
|
||||
[
|
||||
(
|
||||
"ALTER TABLE group_console ALTER COLUMN "
|
||||
"block_plugin TYPE TEXT;"
|
||||
),
|
||||
(
|
||||
"ALTER TABLE group_console ALTER COLUMN "
|
||||
"superuser_block_plugin TYPE TEXT;"
|
||||
),
|
||||
("ALTER TABLE group_console ALTER COLUMN " "block_task TYPE TEXT;"),
|
||||
(
|
||||
"ALTER TABLE group_console ALTER COLUMN "
|
||||
"superuser_block_task TYPE TEXT;"
|
||||
),
|
||||
]
|
||||
)
|
||||
elif "mysql" in db_type:
|
||||
scripts.extend(
|
||||
[
|
||||
("ALTER TABLE group_console MODIFY COLUMN " "block_plugin TEXT;"),
|
||||
(
|
||||
"ALTER TABLE group_console MODIFY COLUMN "
|
||||
"superuser_block_plugin TEXT;"
|
||||
),
|
||||
("ALTER TABLE group_console MODIFY COLUMN " "block_task TEXT;"),
|
||||
(
|
||||
"ALTER TABLE group_console MODIFY COLUMN "
|
||||
"superuser_block_task TEXT;"
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
return scripts
|
||||
|
||||
Reference in New Issue
Block a user