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:
@@ -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);
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
-- :name upsert-price! :! :n
|
-- :name insert-price! :! :n
|
||||||
-- :doc Upsert a day-ahead price record
|
-- :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)
|
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)
|
VALUES (:time-utc, :time-dk, :price-area, :price-dkk, :price-eur)
|
||||||
ON CONFLICT (time_utc, price_area) DO UPDATE
|
ON CONFLICT (time_utc, price_area) DO NOTHING;
|
||||||
SET time_dk = EXCLUDED.time_dk,
|
|
||||||
price_dkk = EXCLUDED.price_dkk,
|
|
||||||
price_eur = EXCLUDED.price_eur;
|
|
||||||
|
|
||||||
-- :name get-prices-for-date :? :*
|
-- :name get-prices-for-date :? :*
|
||||||
-- :doc Get all prices for a given date (time_dk local date) and price area
|
-- :doc Get all prices for a given date (time_dk local date) and price area
|
||||||
|
|||||||
@@ -57,10 +57,10 @@
|
|||||||
(.replace ^String s "T" " "))))
|
(.replace ^String s "T" " "))))
|
||||||
|
|
||||||
(defn save-records!
|
(defn save-records!
|
||||||
"Upsert price records into the database."
|
"Insert price records into the database, skipping existing ones."
|
||||||
[query-fn records]
|
[query-fn records]
|
||||||
(doseq [rec records]
|
(doseq [rec records]
|
||||||
(query-fn :upsert-price!
|
(query-fn :insert-price!
|
||||||
{:time-utc (parse-timestamp (:TimeUTC rec))
|
{:time-utc (parse-timestamp (:TimeUTC rec))
|
||||||
:time-dk (parse-timestamp (:TimeDK rec))
|
:time-dk (parse-timestamp (:TimeDK rec))
|
||||||
:price-area (:PriceArea rec)
|
:price-area (:PriceArea rec)
|
||||||
|
|||||||
Reference in New Issue
Block a user