From 49d6054517835c47aa935781bde28e06b8b39086 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 28 Aug 2023 11:53:24 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix(adds=5Ftables.py):=20fix=20f?= =?UTF-8?q?oreign=20key=20creation=20for=20'flow'=20table=20when=20'user'?= =?UTF-8?q?=20table=20exists=20=E2=9C=A8=20feat(adds=5Ftables.py):=20add?= =?UTF-8?q?=20support=20for=20creating=20'user=5Fid'=20column=20in=20'flow?= =?UTF-8?q?'=20table=20if=20it=20does=20not=20exist=20=F0=9F=94=A7=20fix(a?= =?UTF-8?q?dds=5Ftables.py):=20fix=20index=20creation=20for=20'flow'=20tab?= =?UTF-8?q?le=20when=20'user=5Fid'=20column=20exists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../versions/260dbcc8b680_adds_tables.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/backend/langflow/alembic/versions/260dbcc8b680_adds_tables.py b/src/backend/langflow/alembic/versions/260dbcc8b680_adds_tables.py index 0aa3ed533..db5d413cd 100644 --- a/src/backend/langflow/alembic/versions/260dbcc8b680_adds_tables.py +++ b/src/backend/langflow/alembic/versions/260dbcc8b680_adds_tables.py @@ -27,17 +27,18 @@ def upgrade() -> None: # List existing tables existing_tables = inspector.get_table_names() existing_indices_flow = [] + existing_fks_flow = [] if "flow" in existing_tables: existing_indices_flow = [ index["name"] for index in inspector.get_indexes("flow") ] - # Existing foreign keys for the 'flow' table, if it exists - existing_fks_flow = [] - if "flow" in existing_tables: + # Existing foreign keys for the 'flow' table, if it exists existing_fks_flow = [ fk["referred_table"] + "." + fk["referred_columns"][0] for fk in inspector.get_foreign_keys("flow") ] + # Now check if the columns user_id exists in the 'flow' table + # If it does not exist, we need to create the foreign key if "user" not in existing_tables: op.create_table( @@ -99,17 +100,29 @@ def upgrade() -> None: sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("id"), ) - elif "user.id" not in existing_fks_flow: - with op.batch_alter_table("flow") as batch_op: - batch_op.create_foreign_key("fk_flow_user_id", "user", ["user_id"], ["id"]) # Conditionally create indices for 'flow' table + # if _alembic_tmp_flow exists, then we need to drop it first + if "_alembic_tmp_flow" in existing_tables: + op.drop_table("_alembic_tmp_flow") with op.batch_alter_table("flow", schema=None) as batch_op: + flow_columns = [col["name"] for col in inspector.get_columns("flow")] + if "user_id" not in flow_columns: + batch_op.add_column( + sa.Column( + "user_id", + sqlmodel.sql.sqltypes.GUID(), + nullable=True, # This should be False, but we need to allow NULL values for now + ) + ) + if "user.id" not in existing_fks_flow: + batch_op.create_foreign_key("fk_flow_user_id", "user", ["user_id"], ["id"]) if "ix_flow_description" not in existing_indices_flow: batch_op.create_index( batch_op.f("ix_flow_description"), ["description"], unique=False ) if "ix_flow_name" not in existing_indices_flow: batch_op.create_index(batch_op.f("ix_flow_name"), ["name"], unique=False) + with op.batch_alter_table("flow", schema=None) as batch_op: if "ix_flow_user_id" not in existing_indices_flow: batch_op.create_index( batch_op.f("ix_flow_user_id"), ["user_id"], unique=False