feat(fulfillment): 实时刷新斗鱼账号并支持租约时长设置
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
"""直播账号履约租约时长设置
|
||||
|
||||
默认账号租约无期限;运营可切换为按分钟或小时自动过期。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261117_0085"
|
||||
down_revision: str | None = "20261117_0084"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
|
||||
def _table_exists(table: str) -> bool:
|
||||
return bool(
|
||||
op.get_bind()
|
||||
.execute(
|
||||
sa.text(
|
||||
"SELECT COUNT(*) FROM information_schema.tables "
|
||||
"WHERE table_schema = DATABASE() AND table_name = :table"
|
||||
),
|
||||
{"table": table},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.alter_column(
|
||||
"account_leases",
|
||||
"expires_at",
|
||||
existing_type=sa.DateTime(),
|
||||
nullable=True,
|
||||
)
|
||||
if not _table_exists("platform_fulfillment_settings"):
|
||||
op.create_table(
|
||||
"platform_fulfillment_settings",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("lease_duration_minutes", sa.Integer(), nullable=True),
|
||||
sa.Column("updated_by", sa.Integer(), nullable=True),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(
|
||||
["updated_by"],
|
||||
["users.id"],
|
||||
name="fk_platform_fulfillment_settings_updated_by",
|
||||
ondelete="SET NULL",
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
INSERT INTO platform_fulfillment_settings (
|
||||
id, lease_duration_minutes, updated_by, updated_at
|
||||
) VALUES (1, NULL, NULL, NOW())
|
||||
ON DUPLICATE KEY UPDATE id = id
|
||||
"""
|
||||
)
|
||||
)
|
||||
# 设置上线即默认无限期;正在使用的租约也立即转为无限期,避免几分钟后中断。
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE account_leases
|
||||
SET expires_at = NULL
|
||||
WHERE purpose = 'platform_fulfillment' AND status = 'active'
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE account_leases
|
||||
SET expires_at = DATE_ADD(NOW(), INTERVAL 5 MINUTE)
|
||||
WHERE purpose = 'platform_fulfillment'
|
||||
AND status = 'active'
|
||||
AND expires_at IS NULL
|
||||
"""
|
||||
)
|
||||
)
|
||||
if _table_exists("platform_fulfillment_settings"):
|
||||
op.drop_table("platform_fulfillment_settings")
|
||||
op.alter_column(
|
||||
"account_leases",
|
||||
"expires_at",
|
||||
existing_type=sa.DateTime(),
|
||||
nullable=False,
|
||||
)
|
||||
Reference in New Issue
Block a user