-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate vertex_builds to sql database
- Loading branch information
1 parent
77786d9
commit d3985f3
Showing
17 changed files
with
204 additions
and
178 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/backend/base/langflow/alembic/versions/0d60fcbd4e8e_create_vertex_builds_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""create vertex_builds table | ||
Revision ID: 0d60fcbd4e8e | ||
Revises: 90be8e2ed91e | ||
Create Date: 2024-07-26 11:41:31.274271 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
import sqlmodel | ||
from sqlalchemy.engine.reflection import Inspector | ||
Check failure on line 13 in src/backend/base/langflow/alembic/versions/0d60fcbd4e8e_create_vertex_builds_table.py GitHub Actions / Ruff Style Check (3.12)Ruff (F401)
|
||
from langflow.utils import migration | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '0d60fcbd4e8e' | ||
down_revision: Union[str, None] = '90be8e2ed91e' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
conn = op.get_bind() | ||
if not migration.table_exists("vertex_build", conn): | ||
op.create_table( | ||
"vertex_build", | ||
sa.Column("timestamp", sa.DateTime(), nullable=False), | ||
sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=True), | ||
sa.Column("data", sa.JSON(), nullable=True), | ||
sa.Column("artifacts", sa.JSON(), nullable=True), | ||
sa.Column("params", sqlmodel.sql.sqltypes.AutoString(), nullable=True), | ||
sa.Column("build_id", sqlmodel.sql.sqltypes.GUID(), nullable=False), | ||
sa.Column("flow_id", sqlmodel.sql.sqltypes.GUID(), nullable=False), | ||
sa.Column("valid", sqlmodel.sql.sqltypes.BOOLEAN(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["flow_id"], | ||
["flow.id"], | ||
), | ||
sa.PrimaryKeyConstraint("build_id"), | ||
) | ||
pass | ||
|
||
|
||
def downgrade() -> None: | ||
conn = op.get_bind() | ||
if migration.table_exists("vertex_build", conn): | ||
op.drop_table("vertex_build") | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/backend/base/langflow/services/database/models/vertex_builds/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .model import VertexBuildTable | ||
|
||
__all__ = ["VertexBuildTable"] |
37 changes: 37 additions & 0 deletions
37
src/backend/base/langflow/services/database/models/vertex_builds/crud.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import Optional | ||
from uuid import UUID | ||
|
||
from sqlalchemy.exc import IntegrityError | ||
from sqlmodel import Session, select | ||
from sqlalchemy import delete | ||
|
||
from langflow.services.database.models.transactions.model import TransactionBase, TransactionTable | ||
Check failure on line 8 in src/backend/base/langflow/services/database/models/vertex_builds/crud.py GitHub Actions / Ruff Style Check (3.12)Ruff (F401)
Check failure on line 8 in src/backend/base/langflow/services/database/models/vertex_builds/crud.py GitHub Actions / Ruff Style Check (3.12)Ruff (F401)
|
||
from langflow.services.database.models.vertex_builds.model import VertexBuildBase, VertexBuildTable | ||
|
||
|
||
def get_vertex_builds_by_flow_id(db: Session, flow_id: UUID, limit: Optional[int] = 1000) -> list[VertexBuildTable]: | ||
stmt = ( | ||
select(VertexBuildTable) | ||
.where(VertexBuildTable.flow_id == flow_id) | ||
.order_by(VertexBuildTable.timestamp) | ||
.limit(limit) | ||
) | ||
|
||
builds = db.exec(stmt) | ||
return [t for t in builds] | ||
|
||
|
||
def log_vertex_build(db: Session, vertex_build: VertexBuildBase) -> VertexBuildTable: | ||
table = VertexBuildTable(**vertex_build.model_dump()) | ||
db.add(table) | ||
try: | ||
db.commit() | ||
return table | ||
except IntegrityError as e: | ||
db.rollback() | ||
raise e | ||
|
||
|
||
def delete_vertex_builds_by_flow_id(db: Session, flow_id: UUID) -> None: | ||
delete(VertexBuildTable).where(VertexBuildTable.flow_id == flow_id) | ||
db.commit() |
52 changes: 52 additions & 0 deletions
52
src/backend/base/langflow/services/database/models/vertex_builds/model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from datetime import datetime, timezone | ||
from typing import TYPE_CHECKING, Optional | ||
from uuid import UUID, uuid4 | ||
|
||
from pydantic import field_validator, BaseModel | ||
from sqlmodel import JSON, Column, Field, Relationship, SQLModel | ||
|
||
|
||
if TYPE_CHECKING: | ||
from langflow.services.database.models.flow.model import Flow | ||
|
||
|
||
|
||
class VertexBuildBase(SQLModel): | ||
timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||
id: str = Field(nullable=False) | ||
data: Optional[dict] = Field(default=None, sa_column=Column(JSON)) | ||
artifacts: Optional[dict] = Field(default=None, sa_column=Column(JSON)) | ||
params: Optional[str] = Field(nullable=True) | ||
valid: bool = Field(nullable=False) | ||
flow_id: UUID = Field(foreign_key="flow.id") | ||
|
||
# Needed for Column(JSON) | ||
class Config: | ||
arbitrary_types_allowed = True | ||
|
||
@field_validator("flow_id", mode="before") | ||
@classmethod | ||
def validate_flow_id(cls, value): | ||
if value is None: | ||
return value | ||
if isinstance(value, str): | ||
value = UUID(value) | ||
return value | ||
|
||
|
||
class VertexBuildTable(VertexBuildBase, table=True): | ||
__tablename__ = "vertex_build" | ||
build_id: Optional[UUID] = Field(default_factory=uuid4, primary_key=True) | ||
flow: "Flow" = Relationship(back_populates="vertex_builds") | ||
|
||
|
||
class VertexBuildMapModel(BaseModel): | ||
vertex_builds: dict[str, list[VertexBuildTable]] | ||
@classmethod | ||
def from_list_of_dicts(cls, vertex_build_dicts: list[VertexBuildTable]): | ||
vertex_build_map = {} | ||
for vertex_build in vertex_build_dicts: | ||
if vertex_build.id not in vertex_build_map: | ||
vertex_build_map[vertex_build.id] = [] | ||
vertex_build_map[vertex_build.id].append(vertex_build) | ||
return cls(vertex_builds=vertex_build_map) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.