Add durable workflow triggers and waits

This commit is contained in:
2026-08-01 20:57:26 +02:00
parent 55f98d1b65
commit a1cea1d162
12 changed files with 2213 additions and 275 deletions
@@ -0,0 +1,211 @@
"""v0.1.14 durable Workflow triggers and wait states
Revision ID: b2e4f6a8c0d1
Revises: 0b4e7c9a2d6f
Create Date: 2026-08-01 00:00:00.000000
"""
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
revision = "b2e4f6a8c0d1"
down_revision = "0b4e7c9a2d6f"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table(
"workflow_triggers",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("tenant_id", sa.String(36), nullable=False),
sa.Column("definition_id", sa.String(36), nullable=False),
sa.Column("definition_revision_id", sa.String(36), nullable=False),
sa.Column("node_id", sa.String(120), nullable=False),
sa.Column("kind", sa.String(30), nullable=False),
sa.Column("status", sa.String(30), nullable=False),
sa.Column("config", sa.JSON(), nullable=False),
sa.Column("event_type", sa.String(120), nullable=True),
sa.Column("next_fire_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("last_fire_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("last_status", sa.String(30), nullable=True),
sa.Column("last_error", sa.Text(), nullable=True),
sa.Column("authorization_subject_kind", sa.String(30), nullable=False),
sa.Column("authorization_account_id", sa.String(36), nullable=True),
sa.Column("authorization_membership_id", sa.String(36), nullable=True),
sa.Column("authorization_service_account_id", sa.String(36), nullable=True),
sa.Column("authorization_ref", sa.String(255), nullable=False),
sa.Column("grant_scopes", sa.JSON(), nullable=False),
sa.Column("created_by", sa.String(255), nullable=True),
sa.Column("updated_by", sa.String(255), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(
["definition_id"],
["workflow_definitions.id"],
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["definition_revision_id"],
["workflow_definition_revisions.id"],
ondelete="RESTRICT",
),
sa.UniqueConstraint(
"definition_id",
"node_id",
name="uq_workflow_trigger_definition_node",
),
sa.UniqueConstraint(
"authorization_ref",
name="uq_workflow_triggers_authorization_ref",
),
)
op.create_index(
"ix_workflow_triggers_due",
"workflow_triggers",
["status", "next_fire_at"],
)
op.create_index(
"ix_workflow_triggers_event",
"workflow_triggers",
["tenant_id", "status", "event_type"],
)
for name in (
"tenant_id",
"definition_id",
"definition_revision_id",
"kind",
"status",
"event_type",
"next_fire_at",
"created_by",
"updated_by",
):
op.create_index(
f"ix_workflow_triggers_{name}",
"workflow_triggers",
[name],
)
op.create_table(
"workflow_trigger_deliveries",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("tenant_id", sa.String(36), nullable=False),
sa.Column("trigger_id", sa.String(36), nullable=False),
sa.Column("definition_id", sa.String(36), nullable=False),
sa.Column("definition_revision_id", sa.String(36), nullable=False),
sa.Column("source_key", sa.String(255), nullable=False),
sa.Column("invocation_kind", sa.String(30), nullable=False),
sa.Column("status", sa.String(30), nullable=False),
sa.Column("scheduled_for", sa.DateTime(timezone=True), nullable=True),
sa.Column("event", sa.JSON(), nullable=True),
sa.Column("instance_id", sa.String(36), nullable=True),
sa.Column("attempts", sa.Integer(), nullable=False),
sa.Column("error", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(
["trigger_id"], ["workflow_triggers.id"], ondelete="CASCADE"
),
sa.ForeignKeyConstraint(
["definition_id"],
["workflow_definitions.id"],
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["definition_revision_id"],
["workflow_definition_revisions.id"],
ondelete="RESTRICT",
),
sa.ForeignKeyConstraint(
["instance_id"], ["workflow_instances.id"], ondelete="SET NULL"
),
sa.UniqueConstraint(
"trigger_id",
"source_key",
name="uq_workflow_trigger_delivery_source",
),
)
op.create_index(
"ix_workflow_trigger_deliveries_queue",
"workflow_trigger_deliveries",
["status", "created_at"],
)
op.create_index(
"ix_workflow_trigger_deliveries_tenant_trigger",
"workflow_trigger_deliveries",
["tenant_id", "trigger_id"],
)
for name in (
"tenant_id",
"trigger_id",
"definition_id",
"definition_revision_id",
"status",
"instance_id",
):
op.create_index(
f"ix_workflow_trigger_deliveries_{name}",
"workflow_trigger_deliveries",
[name],
)
op.create_table(
"workflow_wait_states",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("tenant_id", sa.String(36), nullable=False),
sa.Column("instance_id", sa.String(36), nullable=False),
sa.Column("step_id", sa.String(36), nullable=False),
sa.Column("mode", sa.String(30), nullable=False),
sa.Column("status", sa.String(30), nullable=False),
sa.Column("due_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("event_type", sa.String(120), nullable=True),
sa.Column("config", sa.JSON(), nullable=False),
sa.Column("source_event_id", sa.String(128), nullable=True),
sa.Column("event", sa.JSON(), nullable=True),
sa.Column("error", sa.Text(), nullable=True),
sa.Column("revision", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(
["instance_id"], ["workflow_instances.id"], ondelete="CASCADE"
),
sa.ForeignKeyConstraint(
["step_id"], ["workflow_instance_steps.id"], ondelete="CASCADE"
),
sa.UniqueConstraint("step_id", name="uq_workflow_wait_state_step"),
)
op.create_index(
"ix_workflow_wait_states_due",
"workflow_wait_states",
["status", "due_at"],
)
op.create_index(
"ix_workflow_wait_states_event",
"workflow_wait_states",
["tenant_id", "status", "event_type"],
)
for name in (
"tenant_id",
"instance_id",
"step_id",
"mode",
"status",
"due_at",
"event_type",
"source_event_id",
):
op.create_index(
f"ix_workflow_wait_states_{name}",
"workflow_wait_states",
[name],
)
def downgrade() -> None:
op.drop_table("workflow_wait_states")
op.drop_table("workflow_trigger_deliveries")
op.drop_table("workflow_triggers")