refactor(users/Profpatsch/whatcd-resolver): prepare SQL changes

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 <mail@profpatsch.de>
This commit is contained in:
Profpatsch 2025-03-10 12:51:01 +01:00
parent 2adf7b5634
commit 1e188ee116

View file

@ -653,31 +653,46 @@ getBestTorrents opts = do
-- prefer torrents which we already downloaded -- prefer torrents which we already downloaded
torrent_file, torrent_file,
seeding_weight DESC 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 SELECT
tg.group_id, group_id,
t.torrent_id, torrent_id,
t.seeding_weight, seeding_weight,
tg.full_json_result->>'releaseType' AS release_type, release_type,
-- TODO: different endpoints handle this differently (e.g. action=search and action=artist), we should unify this while parsing artists,
COALESCE( group_name,
t.full_json_result->'artists', group_year,
tg.full_json_result->'artists', has_torrent_file,
'[]'::jsonb transmission_torrent_hash,
) as artists, torrent_format
tg.full_json_result->>'groupName' AS group_name, FROM prepare1
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[])
|] |]
<> case opts.ordering of <> case opts.ordering of
BySeedingWeight -> [fmt|ORDER BY seeding_weight DESC|] <> "\n" 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| <> [sql|
LIMIT ?::int LIMIT ?::int
|] |]