Files
server-core/alembic/env.py
T
Pine 8b0bca36a1 feat(db): P1 Alembic+模型整合 — 唯一总库基线迁移 + 培训/园区 ORM 模型
- 新增 alembic(ini/env async/script.mako/versions/0001_initial)。
- models.py 并入培训表(training_accounts/events/bookings/tests/courses/policy/plan/survey_logs)
  与园区表(park_tenants/companies/kb_docs/screens),与平台同库、命名加域前缀避免撞名。
- baseline 迁移用 create_all(幂等):既有平台表保留,新增培训/园区表被建。
- 验证:对临时库 uv run alembic upgrade head 建出全部新表。
2026-08-24 19:28:18 +08:00

64 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""Alembic 迁移环境(async enginetarget_metadata=Base.metadata)。
数据库 URL 取服务端配置 config.DATABASE_URL(同应用唯一总库),
迁移/建表均通过 alembic 运行,不在应用启动时执行。
"""
from __future__ import annotations
import asyncio
import os
import sys
from logging.config import fileConfig
from alembic import context
from sqlalchemy import pool
from sqlalchemy.ext.asyncio import async_engine_from_config
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app import config as app_config
from app.infrastructure.db import Base
import app.infrastructure.models # noqa: F401 确保模型注册到 Base.metadata
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# 用应用统一 DATABASE_URL(可被 env PINEAGENTS_DEMO_DATABASE_URL 覆盖)
config.set_main_option("sqlalchemy.url", app_config.DATABASE_URL)
target_metadata = Base.metadata
def run_migrations_offline() -> None:
context.configure(url=app_config.DATABASE_URL, target_metadata=target_metadata,
literal_binds=True, dialect_opts={"paramstyle": "named"})
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.", poolclass=pool.NullPool,
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def run_migrations_online() -> None:
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()