fix: BUG-20 clamp trips sequence after recompute + renumber migration

This commit is contained in:
Mathieu Moran
2026-08-14 21:45:26 -04:00
parent 25f57603c6
commit 01c7fdf0c4
5 changed files with 52 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
-- Phase 29 (BUG-20): compact trip IDs and stop BIGSERIAL inflation.
-- trips.id has no foreign-key references (deleted_trip_markers keys on
-- (device_id, start_time)), so renumbering is purely cosmetic.
BEGIN;
-- Shift all ids up first so the compact ids (1..N) can't collide with any
-- id that still exists during the renumber.
UPDATE trips SET id = id + 1000000;
-- Renumber compactly in start_time order.
WITH renumbered AS (
SELECT id, row_number() OVER (ORDER BY start_time, id) AS new_id
FROM trips
)
UPDATE trips t
SET id = r.new_id
FROM renumbered r
WHERE t.id = r.id;
-- Reset the sequence to the real max id so the next insert gets MAX(id) + 1.
SELECT setval(
pg_get_serial_sequence('trips', 'id'),
(SELECT COALESCE(MAX(id), 0) FROM trips)
);
COMMIT;