49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
"""数据库初始化模块
|
|
|
|
独立部署时使用的数据库初始化,不依赖 Dify 扩展。
|
|
"""
|
|
import logging
|
|
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# 创建数据库实例
|
|
db = SQLAlchemy()
|
|
|
|
|
|
def init_db(app: Flask) -> None:
|
|
"""初始化数据库连接(配置连接池)。"""
|
|
# 配置连接池参数
|
|
app.config['SQLALCHEMY_POOL_SIZE'] = int(app.config.get('SQLALCHEMY_POOL_SIZE', 10))
|
|
app.config['SQLALCHEMY_POOL_TIMEOUT'] = int(app.config.get('SQLALCHEMY_POOL_TIMEOUT', 30))
|
|
app.config['SQLALCHEMY_POOL_RECYCLE'] = int(app.config.get('SQLALCHEMY_POOL_RECYCLE', 1800))
|
|
app.config['SQLALCHEMY_MAX_OVERFLOW'] = int(app.config.get('SQLALCHEMY_MAX_OVERFLOW', 20))
|
|
|
|
db.init_app(app)
|
|
|
|
# 创建所有表
|
|
with app.app_context():
|
|
# 导入所有模型以确保它们被注册
|
|
from insurance.models import (
|
|
wecom_user, recommendation, operation_log,
|
|
chat_record, chat_session, role, prompt,
|
|
template, notification, department,
|
|
wecom_group, email_verification,
|
|
ppt_session, ppt_config, ppt_history,
|
|
poster_case_upload, poster_template_model,
|
|
poster_copy_template, poster_record,
|
|
system_setting,
|
|
)
|
|
db.create_all()
|
|
logger.info("数据库初始化完成")
|
|
|
|
# 执行数据库迁移
|
|
from insurance.db import run_migrations
|
|
run_migrations()
|
|
|
|
|
|
def get_db() -> SQLAlchemy:
|
|
"""获取数据库实例。"""
|
|
return db
|