定时任务订阅模式

This commit is contained in:
吉鑫升 committed 2026-05-13 14:59:27 +08:00
1 parent 839386f0b4
commit aa034f8669
14 files changed
+1143 -98

No files matched your search

@@ -0,0 +1,66 @@
"""Publish/subscribe support for scheduled tasks.
Revision ID: 20260513_02
Revises: 20260513_01
Create Date: 2026-05-13
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20260513_02"
down_revision = "20260513_01"
branch_labels = None
depends_on = None
def _has_column(table_name: str, column_name: str) -> bool:
inspector = inspect(op.get_bind())
return any(column["name"] == column_name for column in inspector.get_columns(table_name))
def _has_table(table_name: str) -> bool:
inspector = inspect(op.get_bind())
return table_name in inspector.get_table_names()
def upgrade() -> None:
if not _has_column("scheduled_tasks", "published"):
op.add_column(
"scheduled_tasks",
sa.Column("published", sa.Boolean(), nullable=False, server_default=sa.false()),
)
op.create_index("ix_scheduled_tasks_published", "scheduled_tasks", ["published"])
if not _has_column("scheduled_tasks", "published_at"):
op.add_column(
"scheduled_tasks",
sa.Column("published_at", sa.DateTime(timezone=True), nullable=True),
)
if not _has_table("scheduled_task_subscriptions"):
op.create_table(
"scheduled_task_subscriptions",
sa.Column("id", sa.String(64), primary_key=True),
sa.Column("task_id", sa.String(64), nullable=False),
sa.Column("user_id", sa.String(64), nullable=False),
sa.Column("notify_element", sa.Boolean(), nullable=False, server_default=sa.false()),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("task_id", "user_id", name="uq_scheduled_task_subscriptions_task_user"),
)
op.create_index("ix_scheduled_task_subscriptions_task_id", "scheduled_task_subscriptions", ["task_id"])
op.create_index("ix_scheduled_task_subscriptions_user_id", "scheduled_task_subscriptions", ["user_id"])
def downgrade() -> None:
if _has_table("scheduled_task_subscriptions"):
op.drop_table("scheduled_task_subscriptions")
if _has_column("scheduled_tasks", "published_at"):
op.drop_column("scheduled_tasks", "published_at")
if _has_column("scheduled_tasks", "published"):
op.drop_index("ix_scheduled_tasks_published", table_name="scheduled_tasks")
op.drop_column("scheduled_tasks", "published")