f032152eaa
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 2s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 3s
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 - Style (pull_request) Has been skipped
CI/CD Pipeline / Validate - Security (pull_request) Has been skipped
CI/CD Pipeline / Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 42s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 48s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 2m23s
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 / 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 / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 3s
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m17s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m24s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been skipped
AI Code Review / AI Code Review (pull_request) Successful in 6m51s
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 1s
CI/CD Pipeline / Check push changed paths (push) Successful in 2s
CI/CD Pipeline / Frontend Lint (push) Has been skipped
CI/CD Pipeline / PR Build API Image (push) Has been skipped
CI/CD Pipeline / PR Build Web Image (push) Has been skipped
CI/CD Pipeline / PR Build Worker Image (push) Has been skipped
CI/CD Pipeline / Integration Tests (push) Successful in 4m7s
CI/CD Pipeline / Build Staging API Image (push) Successful in 4m14s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 4m28s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 4m47s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 4m49s
CI/CD Pipeline / Retag skipped Staging API Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Has been skipped
CI/CD Pipeline / Validate - Style (push) Successful in 5m7s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 6m58s
CI/CD Pipeline / Unit Tests (push) Successful in 12m19s
CI/CD Pipeline / Validate - Security (push) Successful in 13m1s
CI/CD Pipeline / Build Production Web Image (push) Has been skipped
CI/CD Pipeline / Build Production API Image (push) Has been skipped
CI/CD Pipeline / Build Production Worker Image (push) Has been skipped
CI/CD Pipeline / CI Gate (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Failing after 10m9s
CI/CD Pipeline / Staging E2E Tests (push) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (push) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (push) Has been skipped
CI/CD Pipeline / Canary Release to Production (push) Has been skipped
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
104 lines
3.2 KiB
Python
104 lines
3.2 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,
|
||
):
|
||
# SQLite 不支持 QueuePool 的 pool_size/max_overflow/pool_timeout,
|
||
# 传了会在 create_engine 阶段直接 TypeError,这里只对非 SQLite 传连接池参数。
|
||
if _is_sqlite(database_url):
|
||
return create_engine(database_url, pool_recycle=pool_recycle)
|
||
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()
|