Skip to content

Commit 8f4e942

Browse files
committed
Address claude's review feedback about is_latest
Signed-off-by: Radoslav Dimitrov <[email protected]>
1 parent 634b1e6 commit 8f4e942

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

internal/database/migrations/008_separate_official_metadata.sql

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,32 @@ WHERE status IS NULL
9393

9494
UPDATE servers SET published_at = NOW() WHERE published_at IS NULL;
9595

96-
-- For is_latest: only set to true if no other version of this server already has is_latest=true
97-
-- Otherwise set to false
98-
UPDATE servers s1
99-
SET is_latest = CASE
100-
WHEN EXISTS (
96+
-- For is_latest: preserve existing true values, handle NULLs deterministically
97+
-- First, for servers where NO version has is_latest=true, mark the most recent as latest
98+
WITH servers_without_latest AS (
99+
SELECT DISTINCT s1.server_name
100+
FROM servers s1
101+
WHERE NOT EXISTS (
101102
SELECT 1 FROM servers s2
102103
WHERE s2.server_name = s1.server_name
103104
AND s2.is_latest = true
104-
) THEN false
105-
ELSE true
106-
END
107-
WHERE s1.is_latest IS NULL;
105+
)
106+
),
107+
latest_versions AS (
108+
SELECT s.server_name, MAX(s.published_at) as max_published
109+
FROM servers s
110+
INNER JOIN servers_without_latest swl ON s.server_name = swl.server_name
111+
GROUP BY s.server_name
112+
)
113+
UPDATE servers s
114+
SET is_latest = true
115+
FROM latest_versions lv
116+
WHERE s.server_name = lv.server_name
117+
AND s.published_at = lv.max_published
118+
AND s.is_latest IS NULL;
119+
120+
-- Finally, set remaining NULLs to false
121+
UPDATE servers SET is_latest = false WHERE is_latest IS NULL;
108122

109123
-- Make the new columns NOT NULL now that all records have values
110124
ALTER TABLE servers ALTER COLUMN server_name SET NOT NULL;

0 commit comments

Comments
 (0)