- 新增 app/baked/algorithm|pipeline,非部署参数不再走 env;Settings 保留 DB/HTTP/RTSP/海康/百度/MinIO/Demo - 移除 init_db_schema 与 reload 配置;main 仅 check_database;start*.sh 在 uvicorn 前执行 alembic upgrade head - 依赖 psycopg[binary] 供 Alembic 同步 URL;alembic/env 注释与预发清单更新 - 撕段门控消费管线、各视频/语音/归档调用改为 baked - 百度环境变量仅 BAIDU_APP_ID、BAIDU_API_KEY、BAIDU_SECRET_KEY 与 BAIDU_* 超时/ASR;人脸脚本与 baidu_speech 文案同步 - 全量单测与 .env.example 更新;.gitignore 忽略 refs/(本地权重/视频不入库) Made-with: Cursor
68 lines
2.0 KiB
Python
68 lines
2.0 KiB
Python
"""Alembic environment.
|
||
|
||
开发与生产均通过 `alembic upgrade head` 应用表结构;应用启动时不再自动建表。
|
||
本文件读取 `app.config.settings`,把 asyncpg URL 转为同步
|
||
``postgresql+psycopg://``(psycopg3)供 Alembic 使用(仅迁移期间)。
|
||
需安装 `psycopg` 包,见 `pyproject.toml`。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from logging.config import fileConfig
|
||
|
||
from alembic import context
|
||
from sqlalchemy import engine_from_config, pool
|
||
|
||
from app.config import settings
|
||
import app.db.models # noqa: F401 - register ORM tables on Base.metadata
|
||
from app.db.base import Base
|
||
|
||
|
||
alembic_config = context.config
|
||
|
||
if alembic_config.config_file_name is not None:
|
||
fileConfig(alembic_config.config_file_name)
|
||
|
||
target_metadata = Base.metadata
|
||
|
||
|
||
def _sync_database_url() -> str:
|
||
"""把 asyncpg URL 转为同步 psycopg3 URL,避免 Alembic 强依赖 async 驱动。"""
|
||
url = settings.sqlalchemy_database_url
|
||
return url.replace("postgresql+asyncpg://", "postgresql+psycopg://", 1)
|
||
|
||
|
||
def run_migrations_offline() -> None:
|
||
context.configure(
|
||
url=_sync_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:
|
||
config_section = alembic_config.get_section(alembic_config.config_ini_section) or {}
|
||
config_section["sqlalchemy.url"] = _sync_database_url()
|
||
connectable = engine_from_config(
|
||
config_section,
|
||
prefix="sqlalchemy.",
|
||
poolclass=pool.NullPool,
|
||
)
|
||
with connectable.connect() as connection:
|
||
context.configure(
|
||
connection=connection,
|
||
target_metadata=target_metadata,
|
||
compare_type=True,
|
||
)
|
||
with context.begin_transaction():
|
||
context.run_migrations()
|
||
|
||
|
||
if context.is_offline_mode():
|
||
run_migrations_offline()
|
||
else:
|
||
run_migrations_online()
|