Skip to content

Commit

Permalink
add workflow_permanent_id unique constraint and index to workflows ta…
Browse files Browse the repository at this point in the history
…ble (#309)
  • Loading branch information
wintonzheng authored May 14, 2024
1 parent b959822 commit a4ed6de
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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 ###
23 changes: 20 additions & 3 deletions skyvern/forge/sdk/db/models.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"))
Expand All @@ -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):
Expand Down

0 comments on commit a4ed6de

Please sign in to comment.