"""增加微博账号备注与启停生命周期。""" from __future__ import annotations from collections.abc import Sequence import sqlalchemy as sa from alembic import op revision: str = "20261108_0070" down_revision: str | None = "20261107_0069" branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None TABLE = "weibo_accounts" def upgrade() -> None: bind = op.get_bind() inspector = sa.inspect(bind) columns = {column["name"] for column in inspector.get_columns(TABLE)} if "remark" not in columns: op.add_column( TABLE, sa.Column( "remark", sa.String(length=256), nullable=False, server_default="", ), ) if "is_enabled" not in columns: op.add_column( TABLE, sa.Column( "is_enabled", sa.Boolean(), nullable=False, server_default=sa.text("1"), ), ) op.create_index( f"ix_{TABLE}_is_enabled", TABLE, ["is_enabled"], unique=False, ) if "disabled_at" not in columns: op.add_column(TABLE, sa.Column("disabled_at", sa.DateTime(), nullable=True)) if "disabled_reason" not in columns: op.add_column( TABLE, sa.Column( "disabled_reason", sa.String(length=256), nullable=False, server_default="", ), ) def downgrade() -> None: bind = op.get_bind() inspector = sa.inspect(bind) columns = {column["name"] for column in inspector.get_columns(TABLE)} indexes = {index["name"] for index in inspector.get_indexes(TABLE)} if f"ix_{TABLE}_is_enabled" in indexes: op.drop_index(f"ix_{TABLE}_is_enabled", table_name=TABLE) for column in ("disabled_reason", "disabled_at", "is_enabled", "remark"): if column in columns: op.drop_column(TABLE, column)