feat(weibo): 支持管理员强制转入工作台且不影响原使用人
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
"""允许超级管理员在不影响原使用人的情况下附加微博工作台。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261112_0077"
|
||||
down_revision: str | None = "20261111_0076"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
TABLE = "weibo_workbench_accounts"
|
||||
OLD_UNIQUE = "uq_weibo_workbench_account_owner"
|
||||
NEW_UNIQUE = "uq_weibo_workbench_account_exclusive"
|
||||
CHECK_CONSTRAINT = "ck_weibo_workbench_exclusive_slot"
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
if not inspector.has_table(TABLE):
|
||||
return
|
||||
|
||||
columns = {column["name"] for column in inspector.get_columns(TABLE)}
|
||||
if "exclusive_slot" not in columns:
|
||||
op.add_column(
|
||||
TABLE,
|
||||
sa.Column(
|
||||
"exclusive_slot",
|
||||
sa.Integer(),
|
||||
nullable=True,
|
||||
server_default="1",
|
||||
),
|
||||
)
|
||||
|
||||
constraints = {item["name"] for item in inspector.get_unique_constraints(TABLE)}
|
||||
if OLD_UNIQUE in constraints:
|
||||
op.drop_constraint(OLD_UNIQUE, TABLE, type_="unique")
|
||||
|
||||
# 迁移前超级管理员已有映射时,改为并行挂载,不能继续占用唯一槽位。
|
||||
op.execute(
|
||||
sa.text(
|
||||
"UPDATE weibo_workbench_accounts AS w "
|
||||
"JOIN users AS u ON u.id = w.user_id "
|
||||
"SET w.exclusive_slot = NULL "
|
||||
"WHERE u.role = 'super_admin'"
|
||||
)
|
||||
)
|
||||
|
||||
constraints = {
|
||||
item["name"] for item in sa.inspect(op.get_bind()).get_unique_constraints(TABLE)
|
||||
}
|
||||
if NEW_UNIQUE not in constraints:
|
||||
op.create_unique_constraint(
|
||||
NEW_UNIQUE,
|
||||
TABLE,
|
||||
["account_id", "exclusive_slot"],
|
||||
)
|
||||
checks = {
|
||||
item["name"] for item in sa.inspect(op.get_bind()).get_check_constraints(TABLE)
|
||||
}
|
||||
if CHECK_CONSTRAINT not in checks:
|
||||
op.create_check_constraint(
|
||||
CHECK_CONSTRAINT,
|
||||
TABLE,
|
||||
"exclusive_slot IS NULL OR exclusive_slot = 1",
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
if not inspector.has_table(TABLE):
|
||||
return
|
||||
|
||||
constraints = {item["name"] for item in inspector.get_unique_constraints(TABLE)}
|
||||
if NEW_UNIQUE in constraints:
|
||||
op.drop_constraint(NEW_UNIQUE, TABLE, type_="unique")
|
||||
checks = {item["name"] for item in inspector.get_check_constraints(TABLE)}
|
||||
if CHECK_CONSTRAINT in checks:
|
||||
op.drop_constraint(CHECK_CONSTRAINT, TABLE, type_="check")
|
||||
|
||||
# 旧结构无法表达多个并行映射,降级时只保留普通用户唯一占用。
|
||||
op.execute(sa.text(f"DELETE FROM {TABLE} WHERE exclusive_slot IS NULL"))
|
||||
|
||||
columns = {column["name"] for column in inspector.get_columns(TABLE)}
|
||||
if "exclusive_slot" in columns:
|
||||
op.drop_column(TABLE, "exclusive_slot")
|
||||
|
||||
op.execute(
|
||||
sa.text(
|
||||
"DELETE newer FROM weibo_workbench_accounts AS newer "
|
||||
"JOIN weibo_workbench_accounts AS older "
|
||||
"ON newer.account_id = older.account_id AND newer.id > older.id"
|
||||
)
|
||||
)
|
||||
constraints = {
|
||||
item["name"] for item in sa.inspect(op.get_bind()).get_unique_constraints(TABLE)
|
||||
}
|
||||
if OLD_UNIQUE not in constraints:
|
||||
op.create_unique_constraint(
|
||||
OLD_UNIQUE,
|
||||
TABLE,
|
||||
["account_id"],
|
||||
)
|
||||
Reference in New Issue
Block a user