主要更新: - 更新代理商端文档,明确项目由品牌方分配流程 - 新增Brief配置详情页(已配置)设计稿 - 完善工作台紧急待办中品牌新任务功能 - 整理Pencil设计文件中代理商端页面顺序 - 新增后端FastAPI框架及核心API - 新增前端Next.js页面和组件库 - 添加.gitignore排除构建和缓存文件 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
93 lines
2.1 KiB
Python
93 lines
2.1 KiB
Python
"""
|
|
Alembic 环境配置
|
|
支持异步数据库迁移
|
|
"""
|
|
import asyncio
|
|
from logging.config import fileConfig
|
|
|
|
from sqlalchemy import pool
|
|
from sqlalchemy.engine import Connection
|
|
from sqlalchemy.ext.asyncio import async_engine_from_config
|
|
|
|
from alembic import context
|
|
|
|
# 导入配置和模型
|
|
from app.config import settings
|
|
from app.models.base import Base
|
|
from app.models import (
|
|
Tenant,
|
|
AIConfig,
|
|
ReviewTask,
|
|
ManualTask,
|
|
ForbiddenWord,
|
|
WhitelistItem,
|
|
Competitor,
|
|
RiskException,
|
|
)
|
|
|
|
# Alembic Config 对象
|
|
config = context.config
|
|
|
|
# 设置数据库 URL
|
|
config.set_main_option("sqlalchemy.url", settings.DATABASE_URL)
|
|
|
|
# 日志配置
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
# MetaData 对象用于 autogenerate
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
"""
|
|
离线模式运行迁移
|
|
不需要数据库连接,只生成 SQL 脚本
|
|
"""
|
|
url = config.get_main_option("sqlalchemy.url")
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def do_run_migrations(connection: 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()
|