File tree Expand file tree Collapse file tree 1 file changed +23
-9
lines changed
internal/database/migrations Expand file tree Collapse file tree 1 file changed +23
-9
lines changed Original file line number Diff line number Diff line change @@ -93,18 +93,32 @@ WHERE status IS NULL
9393
9494UPDATE 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
110124ALTER TABLE servers ALTER COLUMN server_name SET NOT NULL ;
You can’t perform that action at this time.
0 commit comments