Skip to content

Commit

Permalink
fix: idempotency is per-client (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Nov 13, 2023
1 parent f3c5dd6 commit 1905c8b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER TABLE public.notifications
DROP CONSTRAINT notifications_pkey;

ALTER TABLE public.notifications
ALTER COLUMN id SET NOT NULL;

ALTER TABLE public.notifications
ADD PRIMARY KEY (id, client_id);
11 changes: 6 additions & 5 deletions src/stores/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ impl NotificationStore for sqlx::PgPool {
payload: &MessagePayload,
) -> stores::Result<Notification> {
let res = sqlx::query_as::<sqlx::postgres::Postgres, Notification>(
"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)
Expand Down
22 changes: 18 additions & 4 deletions tests/functional/stores/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,31 @@ async fn notification_multiple_clients_same_payload(ctx: &mut StoreContext) {
blob: "example-payload".to_string(),
};

let client_id = create_client(&ctx.clients).await;
let client_id1 = create_client(&ctx.clients).await;
let res = ctx
.notifications
.create_or_update_notification(&message_id, TENANT_ID, &client_id, &payload)
.create_or_update_notification(&message_id, TENANT_ID, &client_id1, &payload)
.await;
assert!(res.is_ok());

let client_id = create_client(&ctx.clients).await;
let client_id2 = create_client(&ctx.clients).await;
let res = ctx
.notifications
.create_or_update_notification(&message_id, TENANT_ID, &client_id, &payload)
.create_or_update_notification(&message_id, TENANT_ID, &client_id2, &payload)
.await;
assert!(res.is_ok());

let notification1 = ctx
.notifications
.get_notification(&message_id, &client_id1, TENANT_ID)
.await
.unwrap();
assert_eq!(notification1.client_id, client_id1);

let notification2 = ctx
.notifications
.get_notification(&message_id, &client_id2, TENANT_ID)
.await
.unwrap();
assert_eq!(notification2.client_id, client_id2);
}

0 comments on commit 1905c8b

Please sign in to comment.