2ad70c7da3
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 38s
CI/CD Pipeline / Frontend Lint (pull_request) Failing after 1m14s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 1m44s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 2m6s
CI/CD Pipeline / Validate - Code Quality (pull_request) Failing after 2m54s
Preview Deploy / Deploy Preview Environment (pull_request) Failing after 55s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 3m47s
CI/CD Pipeline / PR Build Web Image (pull_request) Failing after 3m33s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 3m7s
AI Code Review / AI Code Review (pull_request) Failing after 3m39s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 4m5s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Unit Tests (pull_request) Failing after 4m50s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 4m33s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 46m15s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 10s
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
ACR Cleanup / ACR Image Cleanup (pull_request_target) Has been cancelled
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 10s
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
- pytest.ini: 添加 --import-mode=importlib 解决同名测试文件模块冲突 - session.py: 添加 SQLite 检测,ensure_database_exists/initialize_database 跳过PG特定逻辑 - base.py: SharedSettings 新增 use_in_memory_db 配置和 effective_database_url 属性 - worker db.py: 使用 effective_database_url 替代直接读 database_url
100 lines
2.9 KiB
Python
100 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
from sqlalchemy import create_engine, text
|
|
from sqlalchemy.engine import URL, make_url
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from packages.adapters.sqlalchemy_impl.models import Base
|
|
|
|
SCHEMA_INIT_LOCK_ID = 2026061501
|
|
SessionLocal = None
|
|
|
|
|
|
def build_engine(
|
|
database_url: str,
|
|
*,
|
|
pool_size: int = 20,
|
|
max_overflow: int = 40,
|
|
pool_timeout: int = 30,
|
|
pool_recycle: int = 3600,
|
|
):
|
|
return create_engine(
|
|
database_url,
|
|
pool_size=pool_size,
|
|
max_overflow=max_overflow,
|
|
pool_timeout=pool_timeout,
|
|
pool_recycle=pool_recycle,
|
|
)
|
|
|
|
|
|
def build_session_factory(
|
|
database_url: str,
|
|
*,
|
|
pool_size: int = 20,
|
|
max_overflow: int = 40,
|
|
pool_timeout: int = 30,
|
|
pool_recycle: int = 3600,
|
|
):
|
|
engine = build_engine(
|
|
database_url,
|
|
pool_size=pool_size,
|
|
max_overflow=max_overflow,
|
|
pool_timeout=pool_timeout,
|
|
pool_recycle=pool_recycle,
|
|
)
|
|
session_factory = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
global SessionLocal
|
|
SessionLocal = session_factory
|
|
return engine, session_factory
|
|
|
|
|
|
def _is_sqlite(database_url: str) -> bool:
|
|
"""检测是否为 SQLite 数据库 URL."""
|
|
return database_url.startswith("sqlite")
|
|
|
|
|
|
def _build_admin_url(database_url: str) -> URL:
|
|
url = make_url(database_url)
|
|
return url.set(database="postgres")
|
|
|
|
|
|
def ensure_database_exists(database_url: str) -> None:
|
|
"""确保数据库存在(仅 PostgreSQL 需要,SQLite 自动创建)."""
|
|
if _is_sqlite(database_url):
|
|
return
|
|
target_url = make_url(database_url)
|
|
admin_engine = create_engine(_build_admin_url(database_url), isolation_level="AUTOCOMMIT")
|
|
try:
|
|
with admin_engine.connect() as connection:
|
|
exists = connection.execute(
|
|
text("SELECT 1 FROM pg_database WHERE datname = :database_name"),
|
|
{"database_name": target_url.database},
|
|
).scalar()
|
|
if exists:
|
|
return
|
|
connection.execute(text(f'CREATE DATABASE "{target_url.database}"'))
|
|
finally:
|
|
admin_engine.dispose()
|
|
|
|
|
|
def initialize_database(engine) -> None:
|
|
"""初始化数据库 schema。
|
|
|
|
PostgreSQL 使用 advisory lock 防止并发初始化冲突;
|
|
SQLite 直接 create_all(单文件,无并发风险)。
|
|
"""
|
|
if _is_sqlite(str(engine.url)):
|
|
Base.metadata.create_all(bind=engine)
|
|
return
|
|
with engine.connect() as connection:
|
|
connection.execute(text("SELECT pg_advisory_lock(:lock_id)"), {"lock_id": SCHEMA_INIT_LOCK_ID})
|
|
try:
|
|
Base.metadata.create_all(bind=connection)
|
|
connection.commit()
|
|
finally:
|
|
connection.execute(
|
|
text("SELECT pg_advisory_unlock(:lock_id)"),
|
|
{"lock_id": SCHEMA_INIT_LOCK_ID},
|
|
)
|
|
connection.commit()
|