Skip to content

fix(db): guards against error if constraint already dropped #5850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###
Loading