feat: 协作台账支持归档并精简状态列
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
"""虎牙协作台账增加归档字段。
|
||||
|
||||
归档是**可见性状态**,不是业务状态:`status=done`(抢购成功)保持不变,
|
||||
只把 `archived_at` 置为当前时间,默认列表即不再展示;取消归档后恢复。
|
||||
|
||||
只归档终态「抢购成功」;归档不删除 CK、截图、积分快照或审计记录。
|
||||
可重复执行:逐列检查,已存在就跳过。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261019_0060"
|
||||
down_revision: str | None = "20261016_0059"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
TABLE = "huya_external_orders"
|
||||
USERS = "users"
|
||||
INDEX = "ix_huya_external_orders_archived_at"
|
||||
FK = "fk_huya_external_orders_archived_by"
|
||||
|
||||
|
||||
def _columns(bind) -> set[str]:
|
||||
return {column["name"] for column in sa.inspect(bind).get_columns(TABLE)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
if TABLE not in set(sa.inspect(bind).get_table_names()):
|
||||
return
|
||||
columns = _columns(bind)
|
||||
if "archived_at" not in columns:
|
||||
op.add_column(TABLE, sa.Column("archived_at", sa.DateTime(), nullable=True))
|
||||
if "archived_by" not in columns:
|
||||
op.add_column(TABLE, sa.Column("archived_by", sa.Integer(), nullable=True))
|
||||
if "archive_reason" not in columns:
|
||||
op.add_column(
|
||||
TABLE,
|
||||
sa.Column(
|
||||
"archive_reason",
|
||||
sa.String(length=256),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
)
|
||||
|
||||
indexes = {index.get("name") for index in sa.inspect(bind).get_indexes(TABLE)}
|
||||
if INDEX not in indexes:
|
||||
op.create_index(INDEX, TABLE, ["archived_at"])
|
||||
|
||||
foreign_keys = {fk.get("name") for fk in sa.inspect(bind).get_foreign_keys(TABLE)}
|
||||
if FK not in foreign_keys:
|
||||
op.create_foreign_key(FK, TABLE, USERS, ["archived_by"], ["id"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
if TABLE not in set(sa.inspect(bind).get_table_names()):
|
||||
return
|
||||
indexes = {index.get("name") for index in sa.inspect(bind).get_indexes(TABLE)}
|
||||
if INDEX in indexes:
|
||||
op.drop_index(INDEX, table_name=TABLE)
|
||||
foreign_keys = {fk.get("name") for fk in sa.inspect(bind).get_foreign_keys(TABLE)}
|
||||
if FK in foreign_keys:
|
||||
op.drop_constraint(FK, TABLE, type_="foreignkey")
|
||||
columns = _columns(bind)
|
||||
for column in ("archive_reason", "archived_by", "archived_at"):
|
||||
if column in columns:
|
||||
op.drop_column(TABLE, column)
|
||||
Reference in New Issue
Block a user