diff --git a/src/dispatch/database/revisions/tenant/versions/2025-03-11_37406cca756c.py b/src/dispatch/database/revisions/tenant/versions/2025-03-11_37406cca756c.py index 9245956f364e..5774f9eaa6c4 100644 --- a/src/dispatch/database/revisions/tenant/versions/2025-03-11_37406cca756c.py +++ b/src/dispatch/database/revisions/tenant/versions/2025-03-11_37406cca756c.py @@ -5,25 +5,45 @@ Create Date: 2025-03-11 16:26:52.406726 """ -from alembic import op +from alembic import op +from sqlalchemy.engine.reflection import Inspector # revision identifiers, used by Alembic. -revision = '37406cca756c' -down_revision = 'da444de005a6' +revision = "37406cca756c" +down_revision = "da444de005a6" branch_labels = None depends_on = None def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_constraint('case_cost_case_cost_type_id_fkey', 'case_cost', type_='foreignkey') - op.create_foreign_key(None, 'case_cost', 'case_cost_type', ['case_cost_type_id'], ['id'], ondelete='CASCADE') + # Get the connection and inspector + conn = op.get_bind() + inspector = Inspector.from_engine(conn) + + # Check if the constraint exists + constraints = inspector.get_foreign_keys("case_cost") + constraint_names = [constraint["name"] for constraint in constraints] + + # Drop the constraint if it exists + if "case_cost_case_cost_type_id_fkey" in constraint_names: + op.drop_constraint("case_cost_case_cost_type_id_fkey", "case_cost", type_="foreignkey") + + # Create the new foreign key constraint + op.create_foreign_key( + None, "case_cost", "case_cost_type", ["case_cost_type_id"], ["id"], ondelete="CASCADE" + ) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.drop_constraint(None, 'case_cost', type_='foreignkey') - op.create_foreign_key('case_cost_case_cost_type_id_fkey', 'case_cost', 'case_cost_type', ['case_cost_type_id'], ['id']) + op.drop_constraint(None, "case_cost", type_="foreignkey") + op.create_foreign_key( + "case_cost_case_cost_type_id_fkey", + "case_cost", + "case_cost_type", + ["case_cost_type_id"], + ["id"], + ) # ### end Alembic commands ###