fix: Add Cascade Delete Function for Transactions and Builds Associated with Flows (#3848)
* refactor: Add cascade delete functionality for flows This commit adds a new function `cascade_delete_flow` to the `utils.py` file in the `langflow.api` module. This function is responsible for deleting related records when a flow is deleted. It uses the `delete` method from SQLAlchemy to delete records from the `TransactionTable` and `VertexBuildTable` tables based on the flow ID. Finally, it deletes the flow record itself from the `Flow` table. The function is wrapped in a try-except block to handle any exceptions that may occur during the deletion process. If an exception is raised, a `RuntimeError` is raised with an appropriate error message. This refactor improves the code by encapsulating the cascade delete logic in a separate function, making it more modular and easier to maintain. * refactor: Add cascade delete functionality for flows * refactor: Add cascade delete functionality for flows and folders * refactor: Remove unused delete_flow_by_id function * refactor: Add cascade delete functionality for flows and folders * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
14b6157b1d
commit
323d5142b8
5 changed files with 94 additions and 21 deletions
|
|
@ -2,6 +2,7 @@ import json
|
|||
from collections import namedtuple
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from langflow.services.database.models.folder.model import FolderCreate
|
||||
import orjson
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
|
@ -196,6 +197,75 @@ async def test_delete_flows_with_transaction_and_build(
|
|||
assert response.json() == {"vertex_builds": {}}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_folder_with_flows_with_transaction_and_build(
|
||||
client: TestClient, json_flow: str, active_user, logged_in_headers
|
||||
):
|
||||
# Create a new folder
|
||||
folder_name = f"Test Folder {uuid4()}"
|
||||
folder = FolderCreate(name=folder_name, description="Test folder description", components_list=[], flows_list=[])
|
||||
|
||||
response = client.post("/api/v1/folders/", json=folder.model_dump(), headers=logged_in_headers)
|
||||
assert response.status_code == 201, f"Expected status code 201, but got {response.status_code}"
|
||||
|
||||
created_folder = response.json()
|
||||
folder_id = created_folder["id"]
|
||||
|
||||
# Create ten flows
|
||||
number_of_flows = 10
|
||||
flows = [FlowCreate(name=f"Flow {i}", description="description", data={}) for i in range(number_of_flows)]
|
||||
flow_ids = []
|
||||
for flow in flows:
|
||||
flow.folder_id = folder_id
|
||||
response = client.post("api/v1/flows/", json=flow.model_dump(), headers=logged_in_headers)
|
||||
assert response.status_code == 201
|
||||
flow_ids.append(response.json()["id"])
|
||||
|
||||
# Create a transaction for each flow
|
||||
for flow_id in flow_ids:
|
||||
VertexTuple = namedtuple("VertexTuple", ["id"])
|
||||
|
||||
await log_transaction(
|
||||
str(flow_id), source=VertexTuple(id="vid"), target=VertexTuple(id="tid"), status="success"
|
||||
)
|
||||
|
||||
# Create a build for each flow
|
||||
for flow_id in flow_ids:
|
||||
build = {
|
||||
"valid": True,
|
||||
"params": {},
|
||||
"data": ResultDataResponse(),
|
||||
"artifacts": {},
|
||||
"vertex_id": "vid",
|
||||
"flow_id": flow_id,
|
||||
}
|
||||
log_vertex_build(
|
||||
flow_id=build["flow_id"],
|
||||
vertex_id=build["vertex_id"],
|
||||
valid=build["valid"],
|
||||
params=build["params"],
|
||||
data=build["data"],
|
||||
artifacts=build.get("artifacts"),
|
||||
)
|
||||
|
||||
response = client.request("DELETE", f"api/v1/folders/{folder_id}", headers=logged_in_headers)
|
||||
assert response.status_code == 204
|
||||
|
||||
for flow_id in flow_ids:
|
||||
response = client.request(
|
||||
"GET", "api/v1/monitor/transactions", params={"flow_id": flow_id}, headers=logged_in_headers
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json() == []
|
||||
|
||||
for flow_id in flow_ids:
|
||||
response = client.request(
|
||||
"GET", "api/v1/monitor/builds", params={"flow_id": flow_id}, headers=logged_in_headers
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"vertex_builds": {}}
|
||||
|
||||
|
||||
def test_create_flows(client: TestClient, session: Session, json_flow: str, logged_in_headers):
|
||||
flow = orjson.loads(json_flow)
|
||||
data = flow["data"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue