- uv(workspace) + pnpm monorepo;docker-compose(pg+api+web) - SQLAlchemy 16 MVP 表 + Alembic 初版迁移(无漂移,users stub) - FastAPI 骨架:统一错误信封(带 request_id) + structlog + /jobs/:id + OpenAPI - Next.js 骨架:纸感主题 token + OpenAPI→TS 客户端代码生成(gen:api) - CI(ruff/mypy/pytest + pg service + alembic 漂移校验) - 四份设计规格(PRODUCT/UX/ARCHITECTURE/DEV_PLAN) + CLAUDE.md
55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
"""Alembic async env(CLAUDE.md:checkpointer.setup 在迁移/CI;此处仅领域表)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
from logging.config import fileConfig
|
||
|
||
import ww_db.models # noqa: F401 注册所有表到 metadata
|
||
from alembic import context
|
||
from sqlalchemy import Connection, pool
|
||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||
from ww_config import get_settings
|
||
from ww_db.base import Base
|
||
|
||
config = context.config
|
||
if config.config_file_name is not None:
|
||
fileConfig(config.config_file_name)
|
||
|
||
# 用 async URL(asyncpg)运行迁移
|
||
config.set_main_option("sqlalchemy.url", get_settings().database_url)
|
||
target_metadata = Base.metadata
|
||
|
||
|
||
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_offline() -> None:
|
||
context.configure(
|
||
url=config.get_main_option("sqlalchemy.url"),
|
||
target_metadata=target_metadata,
|
||
literal_binds=True,
|
||
)
|
||
with context.begin_transaction():
|
||
context.run_migrations()
|
||
|
||
|
||
if context.is_offline_mode():
|
||
run_migrations_offline()
|
||
else:
|
||
asyncio.run(run_async_migrations())
|