database
This commit is contained in:
@@ -1,6 +1,21 @@
|
|||||||
-- Flyway V2: Ensure weather_data upsert works by providing a unique index
|
-- 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
|
CREATE UNIQUE INDEX IF NOT EXISTS ux_weather_data_property_date
|
||||||
ON weather_data(property_id, date);
|
ON weather_data(property_id, date);
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,8 @@ services:
|
|||||||
- ./backend:/app
|
- ./backend:/app
|
||||||
- /app/node_modules
|
- /app/node_modules
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
db:
|
||||||
|
condition: service_healthy
|
||||||
networks:
|
networks:
|
||||||
- proxy
|
- proxy
|
||||||
- turftracker
|
- turftracker
|
||||||
@@ -75,6 +76,11 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data
|
- postgres_data:/var/lib/postgresql/data
|
||||||
- ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
|
- ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-turftracker} -d ${DB_NAME:-turftracker} -h 127.0.0.1 -p 5432"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 20
|
||||||
networks:
|
networks:
|
||||||
- turftracker
|
- turftracker
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|||||||
Reference in New Issue
Block a user