Files
live-hub-py/web/backend/migrations/versions/20261108_0072_weibo_workbench.py
T

65 lines
2.0 KiB
Python

"""增加微博个人工作台账号映射。"""
from __future__ import annotations
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "20261108_0072"
down_revision: str | None = "20261108_0071"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table("weibo_workbench_accounts"):
op.create_table(
"weibo_workbench_accounts",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(
["account_id"],
["weibo_accounts.id"],
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
ondelete="CASCADE",
),
sa.UniqueConstraint(
"user_id",
"account_id",
name="uq_weibo_workbench_account",
),
)
inspector = sa.inspect(op.get_bind())
existing_indexes = {
index["name"] for index in inspector.get_indexes("weibo_workbench_accounts")
}
if "ix_weibo_workbench_accounts_user_id" not in existing_indexes:
op.create_index(
"ix_weibo_workbench_accounts_user_id",
"weibo_workbench_accounts",
["user_id"],
)
if "ix_weibo_workbench_accounts_account_id" not in existing_indexes:
op.create_index(
"ix_weibo_workbench_accounts_account_id",
"weibo_workbench_accounts",
["account_id"],
)
def downgrade() -> None:
inspector = sa.inspect(op.get_bind())
if not inspector.has_table("weibo_workbench_accounts"):
return
op.drop_table("weibo_workbench_accounts")