From ec2b0ea2075bc424e6723e8b0a46876d0886e1f9 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Mon, 13 Nov 2023 10:02:20 -0500 Subject: [PATCH] fix: idempotency per-client --- ...9_notifications-primary-key-includes-client-id.sql | 8 ++++++++ src/stores/notification.rs | 11 ++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 migrations/1699887339_notifications-primary-key-includes-client-id.sql diff --git a/migrations/1699887339_notifications-primary-key-includes-client-id.sql b/migrations/1699887339_notifications-primary-key-includes-client-id.sql new file mode 100644 index 00000000..54ca7ba4 --- /dev/null +++ b/migrations/1699887339_notifications-primary-key-includes-client-id.sql @@ -0,0 +1,8 @@ +ALTER TABLE public.notifications +DROP CONSTRAINT notifications_pkey; + +ALTER TABLE public.notifications +ALTER COLUMN id varchar(255) not null; + +ALTER TABLE public.notifications +ADD PRIMARY KEY (id, client_id); diff --git a/src/stores/notification.rs b/src/stores/notification.rs index e876f4ec..0147d33c 100644 --- a/src/stores/notification.rs +++ b/src/stores/notification.rs @@ -48,11 +48,12 @@ impl NotificationStore for sqlx::PgPool { payload: &MessagePayload, ) -> stores::Result { let res = sqlx::query_as::( - "INSERT INTO public.notifications (id, tenant_id, client_id, last_payload) -VALUES ($1, $2, $3, $4) -ON CONFLICT (id) - DO UPDATE SET last_received_at = now() -RETURNING *;", + " + INSERT INTO public.notifications (id, tenant_id, client_id, last_payload) + VALUES ($1, $2, $3, $4) + ON CONFLICT (id, client_id) + DO UPDATE SET last_received_at = now() + RETURNING *;", ) .bind(id) .bind(tenant_id)