Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Commit 46265cc

Browse files
authored
fix(db): guards against error if constraint already dropped (#5850)
1 parent a0d53a7 commit 46265cc

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/dispatch/database/revisions/tenant/versions/2025-03-11_37406cca756c.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,45 @@
55
Create Date: 2025-03-11 16:26:52.406726
66
77
"""
8-
from alembic import op
98

9+
from alembic import op
10+
from sqlalchemy.engine.reflection import Inspector
1011

1112
# revision identifiers, used by Alembic.
12-
revision = '37406cca756c'
13-
down_revision = 'da444de005a6'
13+
revision = "37406cca756c"
14+
down_revision = "da444de005a6"
1415
branch_labels = None
1516
depends_on = None
1617

1718

1819
def upgrade():
19-
# ### commands auto generated by Alembic - please adjust! ###
20-
op.drop_constraint('case_cost_case_cost_type_id_fkey', 'case_cost', type_='foreignkey')
21-
op.create_foreign_key(None, 'case_cost', 'case_cost_type', ['case_cost_type_id'], ['id'], ondelete='CASCADE')
20+
# Get the connection and inspector
21+
conn = op.get_bind()
22+
inspector = Inspector.from_engine(conn)
23+
24+
# Check if the constraint exists
25+
constraints = inspector.get_foreign_keys("case_cost")
26+
constraint_names = [constraint["name"] for constraint in constraints]
27+
28+
# Drop the constraint if it exists
29+
if "case_cost_case_cost_type_id_fkey" in constraint_names:
30+
op.drop_constraint("case_cost_case_cost_type_id_fkey", "case_cost", type_="foreignkey")
31+
32+
# Create the new foreign key constraint
33+
op.create_foreign_key(
34+
None, "case_cost", "case_cost_type", ["case_cost_type_id"], ["id"], ondelete="CASCADE"
35+
)
2236
# ### end Alembic commands ###
2337

2438

2539
def downgrade():
2640
# ### commands auto generated by Alembic - please adjust! ###
27-
op.drop_constraint(None, 'case_cost', type_='foreignkey')
28-
op.create_foreign_key('case_cost_case_cost_type_id_fkey', 'case_cost', 'case_cost_type', ['case_cost_type_id'], ['id'])
41+
op.drop_constraint(None, "case_cost", type_="foreignkey")
42+
op.create_foreign_key(
43+
"case_cost_case_cost_type_id_fkey",
44+
"case_cost",
45+
"case_cost_type",
46+
["case_cost_type_id"],
47+
["id"],
48+
)
2949
# ### end Alembic commands ###

0 commit comments

Comments
 (0)