refactor(core): 调整数据库初始化时机
将数据库的初始化、表结构创建和自动迁移逻辑从配置文件加载时移动到主程序 `raw_main` 函数中。 这一改动旨在解决循环导入问题,并确保数据库在所有配置和核心模块加载完毕后才进行初始化,提高了程序的启动鲁棒性和模块独立性。
This commit is contained in:
committed by
Windpicker-owo
parent
6bc709aa38
commit
27c63380c2
29
bot.py
29
bot.py
@@ -200,6 +200,35 @@ def raw_main():
|
|||||||
logger.info("检查EULA和隐私条款完成")
|
logger.info("检查EULA和隐私条款完成")
|
||||||
|
|
||||||
easter_egg()
|
easter_egg()
|
||||||
|
|
||||||
|
# 在此处初始化数据库
|
||||||
|
from src.config.config import global_config
|
||||||
|
from src.common.database.database import initialize_sql_database
|
||||||
|
from src.common.database.sqlalchemy_models import initialize_database as init_db
|
||||||
|
from src.common.database.db_migration import check_and_migrate_database
|
||||||
|
|
||||||
|
logger.info("正在初始化数据库连接...")
|
||||||
|
try:
|
||||||
|
initialize_sql_database(global_config.database)
|
||||||
|
logger.info(f"数据库连接初始化成功,使用 {global_config.database.database_type} 数据库")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"数据库连接初始化失败: {e}")
|
||||||
|
raise e
|
||||||
|
|
||||||
|
logger.info("正在初始化数据库表结构...")
|
||||||
|
try:
|
||||||
|
init_db()
|
||||||
|
logger.info("数据库表结构初始化完成")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"数据库表结构初始化失败: {e}")
|
||||||
|
raise e
|
||||||
|
|
||||||
|
# 执行数据库自动迁移检查
|
||||||
|
try:
|
||||||
|
check_and_migrate_database()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"数据库自动迁移失败: {e}")
|
||||||
|
raise e
|
||||||
|
|
||||||
# 返回MainSystem实例
|
# 返回MainSystem实例
|
||||||
return MainSystem()
|
return MainSystem()
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ from sqlalchemy.orm import sessionmaker
|
|||||||
from sqlalchemy.pool import QueuePool
|
from sqlalchemy.pool import QueuePool
|
||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
from src.config.config import global_config
|
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
import threading
|
import threading
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
@@ -25,6 +24,7 @@ def get_string_field(max_length=255, **kwargs):
|
|||||||
根据数据库类型返回合适的字符串字段
|
根据数据库类型返回合适的字符串字段
|
||||||
MySQL需要指定长度的VARCHAR用于索引,SQLite可以使用Text
|
MySQL需要指定长度的VARCHAR用于索引,SQLite可以使用Text
|
||||||
"""
|
"""
|
||||||
|
from src.config.config import global_config
|
||||||
if global_config.database.database_type == "mysql":
|
if global_config.database.database_type == "mysql":
|
||||||
return String(max_length, **kwargs)
|
return String(max_length, **kwargs)
|
||||||
else:
|
else:
|
||||||
@@ -436,6 +436,7 @@ _SessionLocal = None
|
|||||||
|
|
||||||
def get_database_url():
|
def get_database_url():
|
||||||
"""获取数据库连接URL"""
|
"""获取数据库连接URL"""
|
||||||
|
from src.config.config import global_config
|
||||||
config = global_config.database
|
config = global_config.database
|
||||||
|
|
||||||
if config.database_type == "mysql":
|
if config.database_type == "mysql":
|
||||||
@@ -482,6 +483,7 @@ def initialize_database():
|
|||||||
return _engine, _SessionLocal
|
return _engine, _SessionLocal
|
||||||
|
|
||||||
database_url = get_database_url()
|
database_url = get_database_url()
|
||||||
|
from src.config.config import global_config
|
||||||
config = global_config.database
|
config = global_config.database
|
||||||
|
|
||||||
# 配置引擎参数
|
# 配置引擎参数
|
||||||
|
|||||||
@@ -481,24 +481,4 @@ logger.info("正在品鉴配置文件...")
|
|||||||
global_config = load_config(config_path=os.path.join(CONFIG_DIR, "bot_config.toml"))
|
global_config = load_config(config_path=os.path.join(CONFIG_DIR, "bot_config.toml"))
|
||||||
model_config = api_ada_load_config(config_path=os.path.join(CONFIG_DIR, "model_config.toml"))
|
model_config = api_ada_load_config(config_path=os.path.join(CONFIG_DIR, "model_config.toml"))
|
||||||
|
|
||||||
# 初始化数据库连接
|
|
||||||
logger.info("正在初始化数据库连接...")
|
|
||||||
from src.common.database.database import initialize_sql_database
|
|
||||||
try:
|
|
||||||
initialize_sql_database(global_config.database)
|
|
||||||
logger.info(f"数据库连接初始化成功,使用 {global_config.database.database_type} 数据库")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"数据库连接初始化失败: {e}")
|
|
||||||
raise e
|
|
||||||
|
|
||||||
# 初始化数据库表结构
|
|
||||||
logger.info("正在初始化数据库表结构...")
|
|
||||||
from src.common.database.sqlalchemy_models import initialize_database as init_db
|
|
||||||
try:
|
|
||||||
init_db()
|
|
||||||
logger.info("数据库表结构初始化完成")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"数据库表结构初始化失败: {e}")
|
|
||||||
raise e
|
|
||||||
|
|
||||||
logger.info("非常的新鲜,非常的美味!")
|
logger.info("非常的新鲜,非常的美味!")
|
||||||
|
|||||||
Reference in New Issue
Block a user