feat(ci,alembic): Hub Docker镜像构建发布,数据库迁移并发安全加固

- build-hub.yml 新增 Docker job,构建多架构镜像推送至 GHCR 和 Docker Hub
- build-hub/build-proxy Release 名称简化为 tag 名
- alembic/env.py 使用 PostgreSQL advisory lock 防止多进程并发迁移竞态
- 迁移脚本改用 ADD/DROP COLUMN IF NOT EXISTS 替代 inspector 检查
This commit is contained in:
fawney19
2026-03-02 13:24:58 +08:00
parent 68bae686da
commit 01df063cc1
4 changed files with 128 additions and 35 deletions

View File

@@ -10,8 +10,7 @@ from __future__ import annotations
from collections.abc import Sequence
import sqlalchemy as sa
from sqlalchemy import inspect
from sqlalchemy import text
from alembic import op
@@ -24,33 +23,24 @@ depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
conn = op.get_bind()
inspector = inspect(conn)
existing_columns = {col["name"] for col in inspector.get_columns("usage")}
if "provider_request_body" not in existing_columns:
op.add_column("usage", sa.Column("provider_request_body", sa.JSON(), nullable=True))
if "provider_request_body_compressed" not in existing_columns:
op.add_column(
"usage", sa.Column("provider_request_body_compressed", sa.LargeBinary(), nullable=True)
)
if "client_response_body" not in existing_columns:
op.add_column("usage", sa.Column("client_response_body", sa.JSON(), nullable=True))
if "client_response_body_compressed" not in existing_columns:
op.add_column(
"usage", sa.Column("client_response_body_compressed", sa.LargeBinary(), nullable=True)
)
# Use PostgreSQL native IF NOT EXISTS to avoid duplicate-column races
# when migrations are triggered concurrently (e.g. startup + manual run).
conn.execute(text("ALTER TABLE usage ADD COLUMN IF NOT EXISTS provider_request_body JSON"))
conn.execute(
text("ALTER TABLE usage ADD COLUMN IF NOT EXISTS provider_request_body_compressed BYTEA")
)
conn.execute(text("ALTER TABLE usage ADD COLUMN IF NOT EXISTS client_response_body JSON"))
conn.execute(
text("ALTER TABLE usage ADD COLUMN IF NOT EXISTS client_response_body_compressed BYTEA")
)
def downgrade() -> None:
conn = op.get_bind()
inspector = inspect(conn)
existing_columns = {col["name"] for col in inspector.get_columns("usage")}
for col in (
"client_response_body_compressed",
"client_response_body",
"provider_request_body_compressed",
"provider_request_body",
):
if col in existing_columns:
op.drop_column("usage", col)
conn.execute(text(f"ALTER TABLE usage DROP COLUMN IF EXISTS {col}"))