118 lines
4.1 KiB
Python
118 lines
4.1 KiB
Python
# +======================================================================
|
||
# | 模块: Alembic 迁移环境
|
||
# | 说明: 配置数据库连接与模型 metadata,支持离线/在线两种迁移模式
|
||
# +======================================================================
|
||
"""Alembic 迁移环境。
|
||
|
||
- 数据库 URL 取自 src.config.database.DATABASE_URL
|
||
- 自动聚合 src.modules.*.models 中注册的模型 metadata,支持 autogenerate
|
||
"""
|
||
|
||
# ----------------------------------------------------------------------
|
||
# 导入依赖
|
||
# 涵盖:模块搜索路径、日志配置、路径处理、SQLAlchemy 引擎与连接池、
|
||
# Alembic 上下文
|
||
# ----------------------------------------------------------------------
|
||
import sys
|
||
from logging.config import fileConfig
|
||
from pathlib import Path
|
||
|
||
from sqlalchemy import create_engine, pool
|
||
|
||
from alembic import context
|
||
|
||
# ============================================================
|
||
# 将 src 目录加入 sys.path
|
||
# ============================================================
|
||
# 将 src 目录加入 sys.path,确保可直接 import 项目代码
|
||
ROOT = Path(__file__).resolve().parent.parent
|
||
SRC = ROOT / "src"
|
||
if str(SRC) not in sys.path:
|
||
sys.path.insert(0, str(SRC))
|
||
|
||
from config.database import DATABASE_URL
|
||
|
||
# ============================================================
|
||
# 聚合全部模型 metadata
|
||
# ============================================================
|
||
# 聚合全部模型 metadata(导入各模块 models 即完成注册)
|
||
from extensions.sqlalchemy import Base
|
||
import modules.auth.models
|
||
import modules.user.models
|
||
import modules.user_role.models
|
||
import modules.role.models
|
||
import modules.role_menu.models
|
||
import modules.menu.models
|
||
import modules.dept.models
|
||
import modules.category.models
|
||
import modules.article.models
|
||
import modules.param.models
|
||
import modules.file_template.models
|
||
import modules.job.models
|
||
import modules.dictionary.dict.models
|
||
import modules.dictionary.dict_item.models
|
||
import modules.configuration.config.models
|
||
import modules.configuration.config_item.models
|
||
import modules.level.models
|
||
import modules.position.models
|
||
import modules.city.models
|
||
import modules.notice.models
|
||
import modules.link.models
|
||
import modules.operation_log.models
|
||
import modules.login_log.models
|
||
import modules.job_log.models
|
||
import modules.example.models
|
||
|
||
# ============================================================
|
||
# 读取 Alembic 配置
|
||
# ============================================================
|
||
# 读取 alembic.ini 中的配置
|
||
config = context.config
|
||
|
||
# 存在配置文件时加载其日志配置
|
||
if config.config_file_name is not None:
|
||
fileConfig(config.config_file_name)
|
||
|
||
# 目标 metadata(autogenerate 依据)
|
||
target_metadata = Base.metadata
|
||
|
||
# 数据库 URL
|
||
database_url = DATABASE_URL
|
||
|
||
|
||
# ======================================================================
|
||
# 离线模式:仅生成 SQL 脚本
|
||
# ======================================================================
|
||
def run_migrations_offline() -> None:
|
||
"""离线模式:仅生成 SQL 脚本。"""
|
||
context.configure(
|
||
url=database_url,
|
||
target_metadata=target_metadata,
|
||
literal_binds=True,
|
||
dialect_opts={"paramstyle": "named"},
|
||
)
|
||
with context.begin_transaction():
|
||
context.run_migrations()
|
||
|
||
|
||
# ======================================================================
|
||
# 在线模式:直接执行迁移
|
||
# ======================================================================
|
||
def run_migrations_online() -> None:
|
||
"""在线模式:直接执行迁移。"""
|
||
# NullPool:迁移结束后即释放连接,无需连接池
|
||
connectable = create_engine(database_url, poolclass=pool.NullPool)
|
||
with connectable.connect() as connection:
|
||
context.configure(connection=connection, target_metadata=target_metadata)
|
||
with context.begin_transaction():
|
||
context.run_migrations()
|
||
|
||
|
||
# ============================================================
|
||
# 入口:按模式执行
|
||
# ============================================================
|
||
if context.is_offline_mode():
|
||
run_migrations_offline()
|
||
else:
|
||
run_migrations_online()
|