From f0179270e2e8bc4b47505c854946ceee7e03b8cc Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 14 Oct 2024 13:45:09 -0700 Subject: [PATCH] fix: tag migration --- .../versions/3ab32c4b8f59_update_tags.py | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/backend/open_webui/migrations/versions/3ab32c4b8f59_update_tags.py b/backend/open_webui/migrations/versions/3ab32c4b8f59_update_tags.py index 7c7126e2f..6e010424b 100644 --- a/backend/open_webui/migrations/versions/3ab32c4b8f59_update_tags.py +++ b/backend/open_webui/migrations/versions/3ab32c4b8f59_update_tags.py @@ -28,25 +28,39 @@ def upgrade(): unique_constraints = inspector.get_unique_constraints("tag") existing_indexes = inspector.get_indexes("tag") - print(existing_pk, unique_constraints) + print(f"Primary Key: {existing_pk}") + print(f"Unique Constraints: {unique_constraints}") + print(f"Indexes: {existing_indexes}") with op.batch_alter_table("tag", schema=None) as batch_op: - # Drop unique constraints that could conflict with new primary key + # Drop existing primary key constraint if it exists + if existing_pk and existing_pk.get("constrained_columns"): + pk_name = existing_pk.get("name") + if pk_name: + print(f"Dropping primary key constraint: {pk_name}") + batch_op.drop_constraint(pk_name, type_="primary") + + # Now create the new primary key with the combination of 'id' and 'user_id' + print("Creating new primary key with 'id' and 'user_id'.") + batch_op.create_primary_key("pk_id_user_id", ["id", "user_id"]) + + # Drop unique constraints that could conflict with the new primary key for constraint in unique_constraints: - if constraint["name"] == "uq_id_user_id": + if ( + constraint["name"] == "uq_id_user_id" + ): # Adjust this name according to what is actually returned by the inspector + print(f"Dropping unique constraint: {constraint['name']}") batch_op.drop_constraint(constraint["name"], type_="unique") for index in existing_indexes: if index["unique"]: - # Drop the unique index - batch_op.drop_index(index["name"]) - - # Drop existing primary key constraint if it exists - if existing_pk and existing_pk.get("constrained_columns"): - batch_op.drop_constraint(existing_pk["name"], type_="primary") - - # Immediately after dropping the old primary key, create the new one - batch_op.create_primary_key("pk_id_user_id", ["id", "user_id"]) + if not any( + constraint["name"] == index["name"] + for constraint in unique_constraints + ): + # You are attempting to drop unique indexes + print(f"Dropping unique index: {index['name']}") + batch_op.drop_index(index["name"]) def downgrade():