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
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
|]