From 2d22737c31be6229d6225f06e85469fe9adcc8a0 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 28 Aug 2023 11:17:12 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(adds=5Ftables.py):=20create?= =?UTF-8?q?=20"user"=20and=20"apikey"=20tables=20only=20if=20they=20don't?= =?UTF-8?q?=20exist=20to=20prevent=20duplicate=20table=20creation=20?= =?UTF-8?q?=E2=9C=A8=20feat(adds=5Ftables.py):=20add=20unique=20index=20on?= =?UTF-8?q?=20"username"=20column=20in=20"user"=20table=20for=20faster=20u?= =?UTF-8?q?sername=20lookups=20=E2=9C=A8=20feat(adds=5Ftables.py):=20add?= =?UTF-8?q?=20unique=20index=20on=20"api=5Fkey"=20column=20in=20"apikey"?= =?UTF-8?q?=20table=20for=20faster=20API=20key=20lookups=20=E2=9C=A8=20fea?= =?UTF-8?q?t(adds=5Ftables.py):=20add=20index=20on=20"name"=20column=20in?= =?UTF-8?q?=20"apikey"=20table=20for=20faster=20name=20lookups=20=E2=9C=A8?= =?UTF-8?q?=20feat(adds=5Ftables.py):=20add=20index=20on=20"user=5Fid"=20c?= =?UTF-8?q?olumn=20in=20"apikey"=20table=20for=20faster=20user=20ID=20look?= =?UTF-8?q?ups?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../versions/260dbcc8b680_adds_tables.py | 82 ++++++++++--------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/src/backend/langflow/alembic/versions/260dbcc8b680_adds_tables.py b/src/backend/langflow/alembic/versions/260dbcc8b680_adds_tables.py index c0b9c60c9..0aa3ed533 100644 --- a/src/backend/langflow/alembic/versions/260dbcc8b680_adds_tables.py +++ b/src/backend/langflow/alembic/versions/260dbcc8b680_adds_tables.py @@ -39,45 +39,51 @@ def upgrade() -> None: for fk in inspector.get_foreign_keys("flow") ] - op.create_table( - "user", - sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False), - sa.Column("username", sqlmodel.sql.sqltypes.AutoString(), nullable=False), - sa.Column("password", sqlmodel.sql.sqltypes.AutoString(), nullable=False), - sa.Column("is_active", sa.Boolean(), nullable=False), - sa.Column("is_superuser", sa.Boolean(), nullable=False), - sa.Column("create_at", sa.DateTime(), nullable=False), - sa.Column("updated_at", sa.DateTime(), nullable=False), - sa.Column("last_login_at", sa.DateTime(), nullable=True), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("id"), - ) - with op.batch_alter_table("user", schema=None) as batch_op: - batch_op.create_index(batch_op.f("ix_user_username"), ["username"], unique=True) - - op.create_table( - "apikey", - sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=True), - sa.Column("created_at", sa.DateTime(), nullable=False), - sa.Column("last_used_at", sa.DateTime(), nullable=True), - sa.Column("total_uses", sa.Integer(), nullable=False), - sa.Column("is_active", sa.Boolean(), nullable=False), - sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False), - sa.Column("api_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False), - sa.Column("user_id", sqlmodel.sql.sqltypes.GUID(), nullable=False), - sa.ForeignKeyConstraint( - ["user_id"], - ["user.id"], - ), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("id"), - ) - with op.batch_alter_table("apikey", schema=None) as batch_op: - batch_op.create_index(batch_op.f("ix_apikey_api_key"), ["api_key"], unique=True) - batch_op.create_index(batch_op.f("ix_apikey_name"), ["name"], unique=False) - batch_op.create_index( - batch_op.f("ix_apikey_user_id"), ["user_id"], unique=False + if "user" not in existing_tables: + op.create_table( + "user", + sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False), + sa.Column("username", sqlmodel.sql.sqltypes.AutoString(), nullable=False), + sa.Column("password", sqlmodel.sql.sqltypes.AutoString(), nullable=False), + sa.Column("is_active", sa.Boolean(), nullable=False), + sa.Column("is_superuser", sa.Boolean(), nullable=False), + sa.Column("create_at", sa.DateTime(), nullable=False), + sa.Column("updated_at", sa.DateTime(), nullable=False), + sa.Column("last_login_at", sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("id"), ) + with op.batch_alter_table("user", schema=None) as batch_op: + batch_op.create_index( + batch_op.f("ix_user_username"), ["username"], unique=True + ) + + if "apikey" not in existing_tables: + op.create_table( + "apikey", + sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=True), + sa.Column("created_at", sa.DateTime(), nullable=False), + sa.Column("last_used_at", sa.DateTime(), nullable=True), + sa.Column("total_uses", sa.Integer(), nullable=False), + sa.Column("is_active", sa.Boolean(), nullable=False), + sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False), + sa.Column("api_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False), + sa.Column("user_id", sqlmodel.sql.sqltypes.GUID(), nullable=False), + sa.ForeignKeyConstraint( + ["user_id"], + ["user.id"], + ), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("id"), + ) + with op.batch_alter_table("apikey", schema=None) as batch_op: + batch_op.create_index( + batch_op.f("ix_apikey_api_key"), ["api_key"], unique=True + ) + batch_op.create_index(batch_op.f("ix_apikey_name"), ["name"], unique=False) + batch_op.create_index( + batch_op.f("ix_apikey_user_id"), ["user_id"], unique=False + ) if "flow" not in existing_tables: op.create_table( "flow",