From 4d2452b03f504b6ed5a71b85c08dd1042be126ef Mon Sep 17 00:00:00 2001 From: Alexander Bondarenko <486682+dpwiz@users.noreply.github.com> Date: Wed, 29 May 2024 19:18:36 +0300 Subject: [PATCH] test that light query results match the original --- src/Simplex/Chat.hs | 13 ++++++++++++- src/Simplex/Chat/Store/Direct.hs | 30 ++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 0521bbd969..145670244e 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -3381,7 +3381,12 @@ subscribeUserConnections vr onlyNeeded user = do addSub :: Connection -> [ConnId] -> [ConnId] addSub c = (aConnId c :) getContactConns :: CM [ConnId] - getContactConns = withStore_ getUserContactConnIds + getContactConns = do + ctConns <- withStore_ getUserContactConnIds + ctConns' <- mapMaybe contactConnId . filter contactActive <$> withStore_ (`getUserContacts` vr) + unless (S.fromList ctConns == S.fromList ctConns') $ + logError $ "getContactConns differ: " <> tshow (ctConns, ctConns') + pure ctConns -- (map fst cts', M.fromList cts') getUserContactLinkConns :: CM ([ConnId], Map ConnId UserContact) getUserContactLinkConns = do (cs, ucs) <- unzip <$> withStore_ (`getUserContactLinks` vr) @@ -3390,6 +3395,12 @@ subscribeUserConnections vr onlyNeeded user = do getGroupMemberConns :: CM ([(GroupInfo, [ConnId])], [ConnId]) getGroupMemberConns = do gs <- withStore_ (`getUserGroupMemberConnIds` vr) + let mConns = S.fromList (concatMap snd gs) + gs' <- withStore_ (`getUserGroups` vr) + let mPairs = concatMap (\(Group _ ms) -> mapMaybe memberConnId (filter (not . memberRemoved) ms)) gs' + let mConns' = S.fromList mPairs + unless (mConns == mConns') $ + logError $ "getGroupMemberConns differ: " <> tshow (mConns, mConns') pure (gs, concatMap snd gs) getSndFileTransferConns :: CM ([ConnId], Map ConnId SndFileTransfer) getSndFileTransferConns = do diff --git a/src/Simplex/Chat/Store/Direct.hs b/src/Simplex/Chat/Store/Direct.hs index d5b6357d1c..e93becd1ed 100644 --- a/src/Simplex/Chat/Store/Direct.hs +++ b/src/Simplex/Chat/Store/Direct.hs @@ -853,14 +853,28 @@ getUserContactConnIds :: DB.Connection -> User -> IO [ConnId] getUserContactConnIds db User {userId} = map fromOnly <$> DB.query - db - [sql| - SELECT c.agent_conn_id - FROM connections c - JOIN contacts ct ON ct.contact_id = c.contact_id - WHERE c.user_id = ? AND ct.user_id = ? AND ct.deleted = 0 - |] - (userId, userId) + db + [sql| + SELECT c.agent_conn_id + FROM contacts ct + LEFT JOIN connections c ON c.contact_id = ct.contact_id + WHERE ct.user_id = ? + AND ct.contact_status = ? + AND ct.deleted = 0 + AND + c.connection_id = ( + SELECT cc_connection_id FROM ( + SELECT + cc.connection_id AS cc_connection_id, + cc.created_at AS cc_created_at + FROM connections cc + WHERE cc.user_id = ct.user_id AND cc.contact_id = ct.contact_id + ORDER BY cc_created_at DESC + LIMIT 1 + ) + ) + |] + (userId, CSActive) getConnectionById :: DB.Connection -> VersionRangeChat -> User -> Int64 -> ExceptT StoreError IO Connection getConnectionById db vr User {userId} connId = ExceptT $ do