diff pkg/imports/agm.go @ 3302:ec6163c6687d

'Historicise' gauges on import Gauge data sets will be updated or a new version will be inserted depending on temporal validity and a timestamp marking the last update in the RIS-Index of a data set. The trigger on date_info is removed because the value is actually an attribut coming from the RIS-Index. Gauge measurements and predictions are associated to the version with matching temporal validity. Bottlenecks are always associated to the actual version of the gauge, although this might change as soon as bottlenecks are 'historicised', too.
author Tom Gottfried <tom@intevation.de>
date Thu, 16 May 2019 18:41:43 +0200
parents 831193935739
children 45a629a3a8b8
line wrap: on
line diff
--- a/pkg/imports/agm.go	Thu May 16 17:22:33 2019 +0200
+++ b/pkg/imports/agm.go	Thu May 16 18:41:43 2019 +0200
@@ -75,7 +75,7 @@
   SELECT o.id AS id
   FROM waterway.gauge_measurements o
   JOIN waterway.gauge_measurements n
-    ON n.fk_gauge_id = o.fk_gauge_id AND n.measure_date = o.measure_date
+    USING (location, validity, measure_date)
     WHERE n.id IN (SELECT key FROM staged)
 	  AND o.id NOT IN (SELECT key FROM staged)
 )
@@ -159,12 +159,16 @@
   source_organization
 FROM waterway.gauge_measurements
 WHERE
-  fk_gauge_id = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int) AND
-  measure_date = $6 AND staging_done`
+  location
+    = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int)
+  AND measure_date = $6
+  AND staging_done
+`
 
 	agmInsertSQL = `
 INSERT INTO waterway.gauge_measurements (
-  fk_gauge_id,
+  location,
+  validity,
   measure_date,
   country_code,
   sender,
@@ -175,8 +179,12 @@
   date_info,
   source_organization,
   staging_done
-) VALUES(
+) VALUES (
   ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int),
+  (SELECT validity FROM waterway.gauges
+     WHERE location
+          = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int)
+       AND validity @> CAST($6 AS timestamp with time zone)),
   $6,
   $7,
   $8,