about summary refs log tree commit diff
path: root/users/Profpatsch/whatcd-resolver/src/WhatcdResolver.hs
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2023-09-27T21·27+0200
committerclbot <clbot@tvl.fyi>2023-09-29T17·08+0000
commit7bc739ec5fdfce9980afc0b2676a1e8acb0f4897 (patch)
tree5b77f21a58516fa2273124e5b47804a4ee746036 /users/Profpatsch/whatcd-resolver/src/WhatcdResolver.hs
parent7157e2baed44321aa9836eec6e5a8ad4a507a447 (diff)
refactor(users/Profpatsch/whatcd-resolver): move inserts out r/6669
Now we can move the I/O into a where block.

Change-Id: Ib5334948f3d11ca120ce0b7a46c67f8500fdab3a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9484
Reviewed-by: Profpatsch <mail@profpatsch.de>
Tested-by: BuildkiteCI
Autosubmit: Profpatsch <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch/whatcd-resolver/src/WhatcdResolver.hs')
-rw-r--r--users/Profpatsch/whatcd-resolver/src/WhatcdResolver.hs106
1 files changed, 54 insertions, 52 deletions
diff --git a/users/Profpatsch/whatcd-resolver/src/WhatcdResolver.hs b/users/Profpatsch/whatcd-resolver/src/WhatcdResolver.hs
index fc4cc4ccb678..e61e0526c636 100644
--- a/users/Profpatsch/whatcd-resolver/src/WhatcdResolver.hs
+++ b/users/Profpatsch/whatcd-resolver/src/WhatcdResolver.hs
@@ -707,63 +707,11 @@ redactedSearchAndInsert extraArguments = do
                                         <&> Json.Object
                                     )
                             let tourGroup = T3 groupId groupName fullJsonResult
-                            let insertTourGroup dat = do
-                                  _ <-
-                                    execute
-                                      [fmt|
-                                          DELETE FROM redacted.torrent_groups
-                                          WHERE group_id = ?::integer
-                                      |]
-                                      (Only dat.groupId)
-                                  executeManyReturningWith
-                                    [fmt|
-                                      INSERT INTO redacted.torrent_groups (
-                                        group_id, group_name, full_json_result
-                                      ) VALUES
-                                      ( ?, ? , ? )
-                                      ON CONFLICT (group_id) DO UPDATE SET
-                                        group_id = excluded.group_id,
-                                        group_name = excluded.group_name,
-                                        full_json_result = excluded.full_json_result
-                                      RETURNING (id)
-                                    |]
-                                    [ ( dat.groupId,
-                                        dat.groupName,
-                                        dat.fullJsonResult
-                                      )
-                                    ]
-                                    (label @"tourGroupIdPg" <$> Dec.fromField @Int)
-                                    >>= ensureSingleRow
                             torrents <- Json.key "torrents" $
                               Json.eachInArray $ do
                                 torrentId <- Json.keyLabel @"torrentId" "torrentId" (Json.asIntegral @_ @Int)
                                 fullJsonResultT <- label @"fullJsonResult" <$> Json.asValue
                                 pure $ T2 torrentId fullJsonResultT
-                            let insertTorrents dat = do
-                                  _ <-
-                                    execute
-                                      [sql|
-                                        DELETE FROM redacted.torrents_json
-                                        WHERE torrent_id = ANY (?::integer[])
-                                      |]
-                                      (Only $ dat.torrents & unzipT2 & (.torrentId) & PGArray)
-                                  execute
-                                    [sql|
-                                        INSERT INTO redacted.torrents_json
-                                              (torrent_id, torrent_group, full_json_result)
-                                        SELECT inputs.torrent_id, static.torrent_group, inputs.full_json_result FROM
-                                        (SELECT * FROM UNNEST(?::integer[], ?::jsonb[])) AS inputs(torrent_id, full_json_result)
-                                        CROSS JOIN (VALUES(?::integer)) as static(torrent_group)
-                                    |]
-                                    ( dat.torrents
-                                        & unzipT2
-                                        & \t ->
-                                          ( t.torrentId & PGArray,
-                                            t.fullJsonResult & PGArray,
-                                            dat.tourGroupIdPg
-                                          )
-                                    )
-                                  pure ()
                             pure
                               ( insertTourGroup tourGroup
                                   >>= (\tg -> insertTorrents (T2 (getLabel @"tourGroupIdPg" tg) (label @"torrents" torrents)))
@@ -775,6 +723,60 @@ redactedSearchAndInsert extraArguments = do
                       (label @"transaction" transaction)
                   )
         )
+      where
+        insertTourGroup dat = do
+          _ <-
+            execute
+              [fmt|
+                  DELETE FROM redacted.torrent_groups
+                  WHERE group_id = ?::integer
+              |]
+              (Only dat.groupId)
+          executeManyReturningWith
+            [fmt|
+              INSERT INTO redacted.torrent_groups (
+                group_id, group_name, full_json_result
+              ) VALUES
+              ( ?, ? , ? )
+              ON CONFLICT (group_id) DO UPDATE SET
+                group_id = excluded.group_id,
+                group_name = excluded.group_name,
+                full_json_result = excluded.full_json_result
+              RETURNING (id)
+            |]
+            [ ( dat.groupId,
+                dat.groupName,
+                dat.fullJsonResult
+              )
+            ]
+            (label @"tourGroupIdPg" <$> Dec.fromField @Int)
+            >>= ensureSingleRow
+
+        insertTorrents dat = do
+          _ <-
+            execute
+              [sql|
+                DELETE FROM redacted.torrents_json
+                WHERE torrent_id = ANY (?::integer[])
+              |]
+              (Only $ dat.torrents & unzipT2 & (.torrentId) & PGArray)
+          execute
+            [sql|
+                INSERT INTO redacted.torrents_json
+                      (torrent_id, torrent_group, full_json_result)
+                SELECT inputs.torrent_id, static.torrent_group, inputs.full_json_result FROM
+                (SELECT * FROM UNNEST(?::integer[], ?::jsonb[])) AS inputs(torrent_id, full_json_result)
+                CROSS JOIN (VALUES(?::integer)) as static(torrent_group)
+            |]
+            ( dat.torrents
+                & unzipT2
+                & \t ->
+                  ( t.torrentId & PGArray,
+                    t.fullJsonResult & PGArray,
+                    dat.tourGroupIdPg
+                  )
+            )
+          pure ()
 
 redactedGetTorrentFileAndInsert ::
   ( HasField "torrentId" r Int,