fix: tag migration

This commit is contained in:
Timothy J. Baek 2024-10-14 13:45:09 -07:00
parent 86bc3023b3
commit f0179270e2

View File

@ -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():