Skip to content

Automate browser based workflows with AI

add workflow_permanent_id unique constraint and index to workflows table (#309)

Shuchang Zhenga4ed6de34cf4
diff --git a/alembic/versions/2024_05_14_0245-baec12642d77_add_workflow_permanent_id_constraint_.py b/alembic/versions/2024_05_14_0245-baec12642d77_add_workflow_permanent_id_constraint_.py
new file mode 100644
index 000000000..ecd13a0d0
--- /dev/null
+++ b/alembic/versions/2024_05_14_0245-baec12642d77_add_workflow_permanent_id_constraint_.py
@@ -0,0 +1,40 @@
+"""Add workflow_permanent_id constraint and index to workflows table
+
+Revision ID: baec12642d77
+Revises: bf561125112f
+Create Date: 2024-05-14 02:45:11.284376+00:00
+
+"""
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "baec12642d77"
+down_revision: Union[str, None] = "bf561125112f"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.alter_column("workflows", "workflow_permanent_id", existing_type=sa.VARCHAR(), nullable=False)
+ op.alter_column("workflows", "version", existing_type=sa.INTEGER(), nullable=False)
+ op.create_index(op.f("ix_workflows_workflow_permanent_id"), "workflows", ["workflow_permanent_id"], unique=False)
+ op.create_index("permanent_id_version_idx", "workflows", ["workflow_permanent_id", "version"], unique=False)
+ op.create_unique_constraint(
+ "uc_org_permanent_id_version", "workflows", ["organization_id", "workflow_permanent_id", "version"]
+ )
+ # ### end Alembic commands ###
+
+
+def downgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_constraint("uc_org_permanent_id_version", "workflows", type_="unique")
+ op.drop_index("permanent_id_version_idx", table_name="workflows")
+ op.drop_index(op.f("ix_workflows_workflow_permanent_id"), table_name="workflows")
+ op.alter_column("workflows", "version", existing_type=sa.INTEGER(), nullable=True)
+ op.alter_column("workflows", "workflow_permanent_id", existing_type=sa.VARCHAR(), nullable=True)
+ # ### end Alembic commands ###
diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.py
index 8604baa41..655c94915 100644
--- a/skyvern/forge/sdk/db/models.py
+++ b/skyvern/forge/sdk/db/models.py
@@ -1,6 +1,19 @@
import datetime
-from sqlalchemy import JSON, Boolean, Column, DateTime, Enum, ForeignKey, Index, Integer, Numeric, String, UnicodeText
+from sqlalchemy import (
+ JSON,
+ Boolean,
+ Column,
+ DateTime,
+ Enum,
+ ForeignKey,
+ Index,
+ Integer,
+ Numeric,
+ String,
+ UnicodeText,
+ UniqueConstraint,
+)
from sqlalchemy.ext.asyncio import AsyncAttrs
from sqlalchemy.orm import DeclarativeBase
@@ -122,6 +135,10 @@ class ArtifactModel(Base):
class WorkflowModel(Base):
__tablename__ = "workflows"
+ __table_args__ = (
+ UniqueConstraint("organization_id", "workflow_permanent_id", "version", name="uc_org_permanent_id_version"),
+ Index("permanent_id_version_idx", "workflow_permanent_id", "version"),
+ )
workflow_id = Column(String, primary_key=True, index=True, default=generate_workflow_id)
organization_id = Column(String, ForeignKey("organizations.organization_id"))
@@ -133,8 +150,8 @@ class WorkflowModel(Base):
modified_at = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow, nullable=False)
deleted_at = Column(DateTime, nullable=True)
- workflow_permanent_id = Column(String, default=generate_workflow_permanent_id)
- version = Column(Integer, default=1)
+ workflow_permanent_id = Column(String, nullable=False, default=generate_workflow_permanent_id, index=True)
+ version = Column(Integer, default=1, nullable=False)
class WorkflowRunModel(Base):