This commit is contained in:
Jake Kasper
2025-09-02 09:55:14 -05:00
parent b41c53957d
commit 4e8ded0750
2 changed files with 24 additions and 3 deletions

View File

@@ -1,6 +1,21 @@
-- Flyway V2: Ensure weather_data upsert works by providing a unique index
-- This migration assumes V1 baseline corresponds to the current init.sql schema
-- Some rows may have been inserted without a uniqueness constraint; de-duplicate first,
-- keeping the most recently created record per (property_id, date)
-- Remove duplicates (keep latest created_at, then highest id)
WITH ranked AS (
SELECT ctid, ROW_NUMBER() OVER (
PARTITION BY property_id, date
ORDER BY created_at DESC, id DESC
) AS rn
FROM weather_data
), to_delete AS (
SELECT ctid FROM ranked WHERE rn > 1
)
DELETE FROM weather_data w
USING to_delete d
WHERE w.ctid = d.ctid;
-- Now add the uniqueness guarantee
CREATE UNIQUE INDEX IF NOT EXISTS ux_weather_data_property_date
ON weather_data(property_id, date);