Skip to content

Commit b2e7476

Browse files
fix: add initial setup validations (#5385)
* convert flow ids to uuids when loading from dir * Edit a migration file to check for foreign key existence * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent a269023 commit b2e7476

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/backend/base/langflow/alembic/versions/b2fa308044b5_add_unique_constraints.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ def upgrade() -> None:
4040
batch_op.add_column(sa.Column("folder", sqlmodel.sql.sqltypes.AutoString(), nullable=True))
4141
if "user_id" not in flow_columns:
4242
batch_op.add_column(sa.Column("user_id", sqlmodel.sql.sqltypes.types.Uuid(), nullable=True))
43+
4344
indices = inspector.get_indexes("flow")
4445
indices_names = [index["name"] for index in indices]
4546
if "ix_flow_user_id" not in indices_names:
4647
batch_op.create_index(batch_op.f("ix_flow_user_id"), ["user_id"], unique=False)
47-
if "fk_flow_user_id_user" not in indices_names:
48+
49+
# Check for existing foreign key constraints
50+
constraints = inspector.get_foreign_keys("flow")
51+
constraint_names = [constraint["name"] for constraint in constraints]
52+
53+
if "fk_flow_user_id_user" not in constraint_names:
4854
batch_op.create_foreign_key("fk_flow_user_id_user", "user", ["user_id"], ["id"])
4955

5056
except Exception as e:

src/backend/base/langflow/initial_setup/setup.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,13 @@ async def load_flows_from_directory() -> None:
565565
flow["id"] = no_json_name
566566
flow_id = flow.get("id")
567567

568+
if isinstance(flow_id, str):
569+
try:
570+
flow_id = UUID(flow_id)
571+
except ValueError:
572+
logger.error(f"Invalid UUID string: {flow_id}")
573+
return
574+
568575
existing = await find_existing_flow(session, flow_id, flow_endpoint_name)
569576
if existing:
570577
logger.debug(f"Found existing flow: {existing.name}")
@@ -583,17 +590,24 @@ async def load_flows_from_directory() -> None:
583590
folder_id = await get_default_folder_id(session, user_id)
584591
existing.folder_id = folder_id
585592

593+
if isinstance(existing.id, str):
594+
try:
595+
existing.id = UUID(existing.id)
596+
except ValueError:
597+
logger.error(f"Invalid UUID string: {existing.id}")
598+
return
599+
586600
session.add(existing)
587601
else:
588602
logger.info(f"Creating new flow: {flow_id} with endpoint name {flow_endpoint_name}")
589603

590604
# Current behavior loads all new flows into default folder
591605
folder_id = await get_default_folder_id(session, user_id)
592-
593606
flow["user_id"] = user_id
594607
flow["folder_id"] = folder_id
595608
flow = Flow.model_validate(flow, from_attributes=True)
596609
flow.updated_at = datetime.now(tz=timezone.utc).astimezone()
610+
597611
session.add(flow)
598612

599613

@@ -604,6 +618,7 @@ async def find_existing_flow(session, flow_id, flow_endpoint_name):
604618
if existing := (await session.exec(stmt)).first():
605619
logger.debug(f"Found existing flow by endpoint name: {existing.name}")
606620
return existing
621+
607622
stmt = select(Flow).where(Flow.id == flow_id)
608623
if existing := (await session.exec(stmt)).first():
609624
logger.debug(f"Found existing flow by id: {flow_id}")

0 commit comments

Comments
 (0)