From 2d9464e841f40e921be1d8e04107122351fb33ab Mon Sep 17 00:00:00 2001 From: Bavisetti Narayan <72156168+NarayanBavisetti@users.noreply.github.com> Date: Tue, 24 Dec 2024 21:00:50 +0530 Subject: [PATCH] chore: create unique constraints for webhook (#6257) * chore: create unique constraints for webhook * chore: updated the migration file --- apiserver/plane/app/serializers/webhook.py | 2 +- .../0088_sticky_sort_order_workspaceuserlink.py | 12 ++++++++++++ apiserver/plane/db/models/webhook.py | 9 ++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/apiserver/plane/app/serializers/webhook.py b/apiserver/plane/app/serializers/webhook.py index fa4019f7a..1036b700c 100644 --- a/apiserver/plane/app/serializers/webhook.py +++ b/apiserver/plane/app/serializers/webhook.py @@ -116,7 +116,7 @@ class WebhookSerializer(DynamicBaseSerializer): class Meta: model = Webhook fields = "__all__" - read_only_fields = ["workspace", "secret_key"] + read_only_fields = ["workspace", "secret_key", "deleted_at"] class WebhookLogSerializer(DynamicBaseSerializer): diff --git a/apiserver/plane/db/migrations/0088_sticky_sort_order_workspaceuserlink.py b/apiserver/plane/db/migrations/0088_sticky_sort_order_workspaceuserlink.py index 0efdf41f2..1b3122157 100644 --- a/apiserver/plane/db/migrations/0088_sticky_sort_order_workspaceuserlink.py +++ b/apiserver/plane/db/migrations/0088_sticky_sort_order_workspaceuserlink.py @@ -109,4 +109,16 @@ class Migration(migrations.Migration): name="entity_name", field=models.CharField(max_length=30, verbose_name="Transaction Type"), ), + migrations.AlterUniqueTogether( + name="webhook", + unique_together={("workspace", "url", "deleted_at")}, + ), + migrations.AddConstraint( + model_name="webhook", + constraint=models.UniqueConstraint( + condition=models.Q(("deleted_at__isnull", True)), + fields=("workspace", "url"), + name="webhook_url_unique_url_when_deleted_at_null", + ), + ), ] diff --git a/apiserver/plane/db/models/webhook.py b/apiserver/plane/db/models/webhook.py index ec8fcda3a..dc04e0419 100644 --- a/apiserver/plane/db/models/webhook.py +++ b/apiserver/plane/db/models/webhook.py @@ -47,11 +47,18 @@ class Webhook(BaseModel): return f"{self.workspace.slug} {self.url}" class Meta: - unique_together = ["workspace", "url"] + unique_together = ["workspace", "url", "deleted_at"] verbose_name = "Webhook" verbose_name_plural = "Webhooks" db_table = "webhooks" ordering = ("-created_at",) + constraints = [ + models.UniqueConstraint( + fields=["workspace", "url"], + condition=models.Q(deleted_at__isnull=True), + name="webhook_url_unique_url_when_deleted_at_null", + ) + ] class WebhookLog(BaseModel):