From be8e0e435c7939d554f655226220281e01fc0a5c Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 7 Sep 2020 13:36:27 -0400 Subject: [PATCH] Add cascade to webhook foreign keys --- ..._add_constraints_to_core_sr_ht_webhook_.py | 83 +++++++++++++++++++ gitsrht/webhooks.py | 2 +- 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 gitsrht/alembic/versions/c167cf8a1271_add_constraints_to_core_sr_ht_webhook_.py diff --git a/gitsrht/alembic/versions/c167cf8a1271_add_constraints_to_core_sr_ht_webhook_.py b/gitsrht/alembic/versions/c167cf8a1271_add_constraints_to_core_sr_ht_webhook_.py new file mode 100644 index 0000000..ea933de --- /dev/null +++ b/gitsrht/alembic/versions/c167cf8a1271_add_constraints_to_core_sr_ht_webhook_.py @@ -0,0 +1,83 @@ +"""Add constraints to core.sr.ht webhook tables + +Revision ID: c167cf8a1271 +Revises: 8fbeb080c434 +Create Date: 2020-09-07 13:33:55.440129 + +""" + +# revision identifiers, used by Alembic. +revision = 'c167cf8a1271' +down_revision = '8fbeb080c434' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.drop_constraint( + constraint_name="repo_webhook_subscription_repo_id_fkey", + table_name="repo_webhook_subscription", + type_="foreignkey") + op.create_foreign_key( + constraint_name="repo_webhook_subscription_repo_id_fkey", + source_table="repo_webhook_subscription", + referent_table="repository", + local_cols=["repo_id"], + remote_cols=["id"], + ondelete="CASCADE") + op.drop_constraint( + constraint_name="repo_webhook_subscription_token_id_fkey", + table_name="repo_webhook_subscription", + type_="foreignkey") + op.create_foreign_key( + constraint_name="repo_webhook_subscription_token_id_fkey", + source_table="repo_webhook_subscription", + referent_table="oauthtoken", + local_cols=["token_id"], + remote_cols=["id"], + ondelete="CASCADE") + op.drop_constraint( + constraint_name="repo_webhook_subscription_user_id_fkey", + table_name="repo_webhook_subscription", + type_="foreignkey") + op.create_foreign_key( + constraint_name="repo_webhook_subscription_user_id_fkey", + source_table="repo_webhook_subscription", + referent_table="user", + local_cols=["user_id"], + remote_cols=["id"], + ondelete="CASCADE") + + +def downgrade(): + op.drop_constraint( + constraint_name="repo_webhook_subscription_repo_id_fkey", + table_name="repo_webhook_subscription", + type_="foreignkey") + op.create_foreign_key( + constraint_name="repo_webhook_subscription_repo_id_fkey", + source_table="repo_webhook_subscription", + referent_table="repository", + local_cols=["repo_id"], + remote_cols=["id"]) + op.drop_constraint( + constraint_name="repo_webhook_subscription_token_id_fkey", + table_name="repo_webhook_subscription", + type_="foreignkey") + op.create_foreign_key( + constraint_name="repo_webhook_subscription_token_id_fkey", + source_table="repo_webhook_subscription", + referent_table="oauthtoken", + local_cols=["token_id"], + remote_cols=["id"]) + op.drop_constraint( + constraint_name="repo_webhook_subscription_user_id_fkey", + table_name="repo_webhook_subscription", + type_="foreignkey") + op.create_foreign_key( + constraint_name="repo_webhook_subscription_user_id_fkey", + source_table="repo_webhook_subscription", + referent_table="user", + local_cols=["user_id"], + remote_cols=["id"]) diff --git a/gitsrht/webhooks.py b/gitsrht/webhooks.py index 8384a92..6240d50 100644 --- a/gitsrht/webhooks.py +++ b/gitsrht/webhooks.py @@ -24,5 +24,5 @@ class RepoWebhook(CeleryWebhook): """ repo_id = sa.Column(sa.Integer, - sa.ForeignKey('repository.id'), nullable=False) + sa.ForeignKey('repository.id', ondelete="CASCADE"), nullable=False) repo = sa.orm.relationship('Repository') -- 2.38.4