Skip existing prices on insert and add unique index on (time_dk, price_area)

Changed upsert to INSERT ON CONFLICT DO NOTHING so existing prices are
never overwritten. Added unique index on (time_dk, price_area) to enforce
no duplicate quarter-hour entries per area.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 13:14:28 +01:00
co-authored by Claude Opus 4.6
parent 9554e511ad
commit 97a95684cf
4 changed files with 7 additions and 8 deletions
@@ -0,0 +1 @@
DROP INDEX IF EXISTS idx_day_ahead_prices_time_dk_area;
@@ -0,0 +1 @@
CREATE UNIQUE INDEX idx_day_ahead_prices_time_dk_area ON day_ahead_prices (time_dk, price_area);
+3 -6
View File
@@ -1,11 +1,8 @@
-- :name upsert-price! :! :n
-- :doc Upsert a day-ahead price record
-- :name insert-price! :! :n
-- :doc Insert a day-ahead price record, skip if already exists
INSERT INTO day_ahead_prices (time_utc, time_dk, price_area, price_dkk, price_eur)
VALUES (:time-utc, :time-dk, :price-area, :price-dkk, :price-eur)
ON CONFLICT (time_utc, price_area) DO UPDATE
SET time_dk = EXCLUDED.time_dk,
price_dkk = EXCLUDED.price_dkk,
price_eur = EXCLUDED.price_eur;
ON CONFLICT (time_utc, price_area) DO NOTHING;
-- :name get-prices-for-date :? :*
-- :doc Get all prices for a given date (time_dk local date) and price area
@@ -57,10 +57,10 @@
(.replace ^String s "T" " "))))
(defn save-records!
"Upsert price records into the database."
"Insert price records into the database, skipping existing ones."
[query-fn records]
(doseq [rec records]
(query-fn :upsert-price!
(query-fn :insert-price!
{:time-utc (parse-timestamp (:TimeUTC rec))
:time-dk (parse-timestamp (:TimeDK rec))
:price-area (:PriceArea rec)