Skip to content

Automate browser based workflows with AI

fix(alembic): add nonterminal_status indexes to unblock sync PRs (SKY-9746) (#5961)

pedrohsdb6d14dee5338d
diff --git a/alembic/versions/2026_05_12_2000-5b15948c8d68_add_status_indexes_for_workflow_runs_and_tasks.py b/alembic/versions/2026_05_12_2000-5b15948c8d68_add_status_indexes_for_workflow_runs_and_tasks.py
new file mode 100644
index 000000000..cbdae016a
--- /dev/null
+++ b/alembic/versions/2026_05_12_2000-5b15948c8d68_add_status_indexes_for_workflow_runs_and_tasks.py
@@ -0,0 +1,45 @@
+"""add status indexes for workflow_runs and tasks to prevent statement timeouts
+
+Revision ID: 5b15948c8d68
+Revises: 78a8db531e69
+Create Date: 2026-05-12 20:00:00.000000+00:00
+
+Mirrors the cloud-side index addition for the equivalent declarations in
+skyvern/forge/sdk/db/models.py. Without this migration, alembic check fails
+because the synced models reference indexes that don't exist in OSS migrations.
+
+"""
+
+from typing import Sequence, Union
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "5b15948c8d68"
+down_revision: Union[str, None] = "78a8db531e69"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ with op.get_context().autocommit_block():
+ op.execute("SET statement_timeout = '3h';")
+ op.execute("""
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_workflow_runs_nonterminal_status
+ ON workflow_runs (status, modified_at, created_at)
+ WHERE status IN ('created', 'queued', 'running', 'paused')
+ """)
+ op.execute("""
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_tasks_nonterminal_status
+ ON tasks (status, modified_at, created_at)
+ WHERE status IN ('created', 'queued', 'running')
+ """)
+ op.execute("RESET statement_timeout;")
+
+
+def downgrade() -> None:
+ with op.get_context().autocommit_block():
+ op.execute("SET statement_timeout = '3h';")
+ op.execute("DROP INDEX CONCURRENTLY IF EXISTS ix_tasks_nonterminal_status")
+ op.execute("DROP INDEX CONCURRENTLY IF EXISTS ix_workflow_runs_nonterminal_status")
+ op.execute("RESET statement_timeout;")
diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.py
index d8bab6e52..69f2992b3 100644
--- a/skyvern/forge/sdk/db/models.py
+++ b/skyvern/forge/sdk/db/models.py
@@ -77,7 +77,16 @@ class Base(AsyncAttrs, DeclarativeBase):
class TaskModel(Base):
__tablename__ = "tasks"
- __table_args__ = (Index("idx_tasks_org_created", "organization_id", "created_at"),)
+ __table_args__ = (
+ Index("idx_tasks_org_created", "organization_id", "created_at"),
+ Index(
+ "ix_tasks_nonterminal_status",
+ "status",
+ "modified_at",
+ "created_at",
+ postgresql_where=text("status IN ('created', 'queued', 'running')"),
+ ),
+ )
task_id = Column(String, primary_key=True, default=generate_task_id)
organization_id = Column(String, ForeignKey("organizations.organization_id"))
@@ -406,7 +415,16 @@ class WorkflowTemplateModel(Base):
class WorkflowRunModel(Base):
__tablename__ = "workflow_runs"
- __table_args__ = (Index("idx_workflow_runs_org_created", "organization_id", "created_at"),)
+ __table_args__ = (
+ Index("idx_workflow_runs_org_created", "organization_id", "created_at"),
+ Index(
+ "ix_workflow_runs_nonterminal_status",
+ "status",
+ "modified_at",
+ "created_at",
+ postgresql_where=text("status IN ('created', 'queued', 'running', 'paused')"),
+ ),
+ )
workflow_run_id = Column(String, primary_key=True, default=generate_workflow_run_id)
workflow_id = Column(String, nullable=False)