From 1e188ee116632fdf34c231d45b94cb8e9f8c8875 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Mon, 10 Mar 2025 12:51:01 +0100 Subject: [PATCH] refactor(users/Profpatsch/whatcd-resolver): prepare SQL changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For the next step, we need to put more things in WITH-Clauses, so let’s prepare it now to get a nicer diff. Change-Id: Ibd5b67c77c87ce3d6aa0ab04ca3d66f9b90dc856 Reviewed-on: https://cl.tvl.fyi/c/depot/+/13237 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- .../whatcd-resolver/src/Redacted.hs | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/users/Profpatsch/whatcd-resolver/src/Redacted.hs b/users/Profpatsch/whatcd-resolver/src/Redacted.hs index a9790bb2f..2b91862f7 100644 --- a/users/Profpatsch/whatcd-resolver/src/Redacted.hs +++ b/users/Profpatsch/whatcd-resolver/src/Redacted.hs @@ -653,31 +653,46 @@ getBestTorrents opts = do -- prefer torrents which we already downloaded torrent_file, seeding_weight DESC + ), + prepare1 AS ( + SELECT + tg.group_id, + t.torrent_id, + t.seeding_weight, + tg.full_json_result->>'releaseType' AS release_type, + -- TODO: different endpoints handle this differently (e.g. action=search and action=artist), we should unify this while parsing + COALESCE( + t.full_json_result->'artists', + tg.full_json_result->'artists', + '[]'::jsonb + ) as artists, + tg.full_json_result->>'groupName' AS group_name, + tg.full_json_result->>'groupYear' AS group_year, + t.torrent_file IS NOT NULL AS has_torrent_file, + t.transmission_torrent_hash, + t.full_json_result->>'encoding' AS torrent_format + FROM filtered_torrents f + JOIN redacted.torrents t ON t.id = f.id + JOIN redacted.torrent_groups tg ON tg.id = t.torrent_group + WHERE + tg.full_json_result->>'releaseType' <> ALL (?::text[]) ) SELECT - tg.group_id, - t.torrent_id, - t.seeding_weight, - tg.full_json_result->>'releaseType' AS release_type, - -- TODO: different endpoints handle this differently (e.g. action=search and action=artist), we should unify this while parsing - COALESCE( - t.full_json_result->'artists', - tg.full_json_result->'artists', - '[]'::jsonb - ) as artists, - tg.full_json_result->>'groupName' AS group_name, - tg.full_json_result->>'groupYear' AS group_year, - t.torrent_file IS NOT NULL AS has_torrent_file, - t.transmission_torrent_hash, - t.full_json_result->>'encoding' AS torrent_format - FROM filtered_torrents f - JOIN redacted.torrents t ON t.id = f.id - JOIN redacted.torrent_groups tg ON tg.id = t.torrent_group - WHERE tg.full_json_result->>'releaseType' <> ALL (?::text[]) + group_id, + torrent_id, + seeding_weight, + release_type, + artists, + group_name, + group_year, + has_torrent_file, + transmission_torrent_hash, + torrent_format + FROM prepare1 |] <> case opts.ordering of BySeedingWeight -> [fmt|ORDER BY seeding_weight DESC|] <> "\n" - ByLastReleases -> [fmt|ORDER BY tg.group_id DESC|] <> "\n" + ByLastReleases -> [fmt|ORDER BY group_id DESC|] <> "\n" <> [sql| LIMIT ?::int |]