优化数据库初始化配置

This commit is contained in:
ATTomatoo 2025-07-02 16:13:47 +08:00
parent 75294bba44
commit daad4c718b

View File

@ -1,8 +1,8 @@
from asyncio import Semaphore from asyncio import Semaphore
from collections.abc import Iterable from collections.abc import Iterable
from typing import Any, ClassVar from typing import Any, ClassVar
from urllib.parse import urlparse
from typing_extensions import Self from typing_extensions import Self
from urllib.parse import urlparse
import nonebot import nonebot
from nonebot.utils import is_coroutine_callable from nonebot.utils import is_coroutine_callable
@ -13,6 +13,8 @@ from tortoise.models import Model as TortoiseModel
from zhenxun.configs.config import BotConfig from zhenxun.configs.config import BotConfig
from zhenxun.utils.enum import DbLockType from zhenxun.utils.enum import DbLockType
from zhenxun.utils.exception import HookPriorityException
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
from .cache import CacheRoot from .cache import CacheRoot
from .log import logger from .log import logger
@ -167,7 +169,7 @@ class Model(TortoiseModel):
await CacheRoot.reload(cache_type) await CacheRoot.reload(cache_type)
class DbUrlMissing(Exception): class DbUrlIsNode(HookPriorityException):
""" """
数据库链接地址为空 数据库链接地址为空
""" """
@ -183,9 +185,18 @@ class DbConnectError(Exception):
pass pass
@PriorityLifecycle.on_startup(priority=1)
async def init(): async def init():
if not BotConfig.db_url: if not BotConfig.db_url:
raise DbUrlMissing("数据库配置为空,请在.env.dev中配置DB_URL...") error = f"""
**********************************************************************
🌟 **************************** 配置为空 ************************* 🌟
🚀 请打开 WebUi 进行基础配置 🚀
🌐 配置地址http://{driver.config.host}:{driver.config.port}/#/configure 🌐
***********************************************************************
***********************************************************************
"""
raise DbUrlIsNode("\n" + error.strip())
try: try:
db_url = BotConfig.db_url db_url = BotConfig.db_url
url_scheme = db_url.split(":", 1)[0] url_scheme = db_url.split(":", 1)[0]
@ -271,7 +282,6 @@ async def init():
logger.debug(f"执行SQL: {sql}") logger.debug(f"执行SQL: {sql}")
try: try:
await db.execute_query_dict(sql) await db.execute_query_dict(sql)
# await TestSQL.raw(sql)
except Exception as e: except Exception as e:
logger.debug(f"执行SQL: {sql} 错误...", e=e) logger.debug(f"执行SQL: {sql} 错误...", e=e)
if sql_list: if sql_list:
@ -283,4 +293,4 @@ async def init():
async def disconnect(): async def disconnect():
await connections.close_all() await connections.close_all()