Merge branch 'develop' - v0.1.117: 补025号迁移文件
CI/CD Pipeline / Frontend Lint (push) Failing after 92h24m4s
CI/CD Pipeline / Validate Code Quality And Tests (push) Failing after 92h24m13s
Deploy / Deploy Staging (push) Failing after 92h23m36s
Deploy / Staging E2E Tests (push) Failing after 92h21m21s
Deploy / Build Production Runtime Images (push) Has been skipped
Deploy / Deploy Production (push) Has been skipped
Deploy / Production Browser E2E (push) Has been skipped

This commit is contained in:
xiaoxia
2026-07-05 16:30:12 +08:00
2 changed files with 75 additions and 0 deletions
@@ -0,0 +1,72 @@
"""Task: Add wechat_openid / wechat_unionid to users
Revision ID: 025
Revises: 024
Create Date: 2026-07-05
补录微信小程序登录所需的 wechat 字段。
生产数据库已手动添加过这些字段和索引,因此 upgrade 做幂等检查,
避免在已有字段的库上执行报错。
"""
import sqlalchemy as sa
from alembic import op
revision = "025"
down_revision = "024"
branch_labels = None
depends_on = None
def _column_exists(table: str, column: str) -> bool:
"""检查 PostgreSQL 表中某列是否已存在。"""
conn = op.get_bind()
result = conn.execute(
sa.text(
"SELECT 1 FROM information_schema.columns "
"WHERE table_name = :table AND column_name = :column"
),
{"table": table, "column": column},
)
return result.scalar() is not None
def _index_exists(index: str) -> bool:
"""检查 PostgreSQL 中某索引是否已存在。"""
conn = op.get_bind()
result = conn.execute(
sa.text("SELECT 1 FROM pg_indexes WHERE indexname = :index"),
{"index": index},
)
return result.scalar() is not None
def upgrade() -> None:
# wechat_openid
if not _column_exists("users", "wechat_openid"):
op.add_column(
"users",
sa.Column("wechat_openid", sa.String(length=128), nullable=True),
)
# wechat_unionid
if not _column_exists("users", "wechat_unionid"):
op.add_column(
"users",
sa.Column("wechat_unionid", sa.String(length=128), nullable=True),
)
# 唯一索引
if not _index_exists("ix_users_wechat_openid"):
op.create_index("ix_users_wechat_openid", "users", ["wechat_openid"], unique=True)
if not _index_exists("ix_users_wechat_unionid"):
op.create_index("ix_users_wechat_unionid", "users", ["wechat_unionid"], unique=True)
def downgrade() -> None:
op.drop_index("ix_users_wechat_unionid", table_name="users")
op.drop_index("ix_users_wechat_openid", table_name="users")
op.drop_column("users", "wechat_unionid")
op.drop_column("users", "wechat_openid")
@@ -30,6 +30,9 @@ class UserModel(Base):
used_storage_gb = Column(Integer, nullable=False, default=0)
# 管理员标识
is_admin = Column(Boolean, nullable=False, default=False)
# 微信登录(小程序端)
wechat_openid = Column(String(128), nullable=True, unique=True, index=True)
wechat_unionid = Column(String(128), nullable=True, unique=True, index=True)
created_at = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc))