From 0fd9c2b5781e10e513bb33b85f7ea2c15d7adc1c Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Thu, 16 Jun 2022 12:21:19 +0200 Subject: [PATCH] Fixed update operations in DataSync operation not removing database records from a subscription when the WHERE condition doesn't match anymore --- IHP/DataSync/ControllerImpl.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/IHP/DataSync/ControllerImpl.hs b/IHP/DataSync/ControllerImpl.hs index 43ff270b0..039fc4947 100644 --- a/IHP/DataSync/ControllerImpl.hs +++ b/IHP/DataSync/ControllerImpl.hs @@ -114,7 +114,13 @@ runDataSyncController ensureRLSEnabled installTableChangeTriggers receiveData se -- results set isWatchingRecord <- Set.member id <$> readIORef watchedRecordIdsRef when isWatchingRecord do - sendJSON DidUpdate { subscriptionId, id, changeSet = changesToValue changeSet } + -- The updated record could not be part of the query result set anymore + -- E.g. if it's not matched anymore by the WHERE condition after the update + [(PG.Only isRecordInResultSet)] <- sqlQueryWithRLS ("SELECT EXISTS(SELECT * FROM (" <> theQuery <> ") AS records WHERE records.id = ? LIMIT 1)") (theParams <> [PG.toField id]) + + if isRecordInResultSet + then sendJSON DidUpdate { subscriptionId, id, changeSet = changesToValue changeSet } + else sendJSON DidDelete { subscriptionId, id } ChangeNotifications.DidDelete { id } -> do -- Only send the notifcation if the deleted record was part of the initial -- results set