changeset 4083:2df48e364721 timezone

Merged default into timezone branch.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 25 Jul 2019 16:46:06 +0200
parents 80bdcd137a1d (current diff) 419f28898db0 (diff)
children 350a24c92848
files
diffstat 4 files changed, 27 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/fa.go	Thu Jul 25 15:43:13 2019 +0200
+++ b/pkg/imports/fa.go	Thu Jul 25 16:46:06 2019 +0200
@@ -37,11 +37,6 @@
 	Insecure bool `json:"insecure"`
 }
 
-type uniqueFairwayAvailability struct {
-	BottleneckId string
-	Surdat       time.Time
-}
-
 // FAJobKind is import queue type identifier.
 const FAJobKind JobKind = "fa"
 
@@ -61,14 +56,7 @@
 FROM waterway.effective_fairway_availability
 ORDER BY measure_date DESC LIMIT 1
 `
-	listFairwayAvailabilitySQL = `
-SELECT
-  fa.id,
-  bn.bottleneck_id,
-  fa.surdat
-FROM waterway.fairway_availability fa
-JOIN waterway.bottlenecks bn ON bn.id = fa.bottleneck_id
-`
+
 	insertFASQL = `
 INSERT INTO waterway.fairway_availability (
   position_code,
@@ -87,7 +75,11 @@
   $4,
   $5,
   $6
-)
+) ON CONFLICT (bottleneck_id, surdat) DO UPDATE SET
+  position_code = EXCLUDED.position_code,
+  critical = EXCLUDED.critical,
+  date_info = EXCLUDED.date_info,
+  source_organization = EXCLUDED.source_organization
 RETURNING id`
 
 	insertBnPdfsSQL = `
@@ -104,6 +96,7 @@
   $4,
   $5
 ) ON CONFLICT ON CONSTRAINT bottleneck_pdfs_pkey DO NOTHING`
+
 	insertEFASQL = `
 INSERT INTO waterway.effective_fairway_availability (
   fairway_availability_id,
@@ -131,6 +124,7 @@
   $9,
   $10
 ) ON CONFLICT ON CONSTRAINT effective_fairway_availability_pkey DO NOTHING`
+
 	insertFAVSQL = `
 INSERT INTO waterway.fa_reference_values (
   fairway_availability_id,
@@ -215,36 +209,6 @@
 	return bns, nil
 }
 
-func loadFairwayAvailabilities(ctx context.Context, tx *sql.Tx) (map[uniqueFairwayAvailability]int64, error) {
-	rows, err := tx.QueryContext(ctx, listFairwayAvailabilitySQL)
-	if err != nil {
-		return nil, err
-	}
-	defer rows.Close()
-	fairwayAvailabilities := map[uniqueFairwayAvailability]int64{}
-	for rows.Next() {
-		var id int64
-		var bnId string
-		var sd time.Time
-		if err = rows.Scan(
-			&id,
-			&bnId,
-			&sd,
-		); err != nil {
-			return nil, err
-		}
-		key := uniqueFairwayAvailability{
-			BottleneckId: bnId,
-			Surdat:       sd,
-		}
-		fairwayAvailabilities[key] = id
-	}
-	if err = rows.Err(); err != nil {
-		return nil, err
-	}
-	return fairwayAvailabilities, nil
-}
-
 func latestDate(ctx context.Context, tx *sql.Tx) (pgtype.Timestamp, error) {
 	var date pgtype.Timestamp
 	err := tx.QueryRowContext(ctx, latestMeasureDateSQL).Scan(&date)
@@ -285,12 +249,7 @@
 		return nil, err
 	}
 
-	fairwayAvailabilities, err := loadFairwayAvailabilities(ctx, tx)
-	if err != nil {
-		return nil, err
-	}
-
-	faids, err := doForFAs(ctx, bns, fairwayAvailabilities, fas, tx, feedback)
+	faids, err := doForFAs(ctx, bns, fas, tx, feedback)
 	if err != nil {
 		return nil, fmt.Errorf("Error processing data: %v", err)
 	}
@@ -320,7 +279,6 @@
 func doForFAs(
 	ctx context.Context,
 	bnIds bottlenecks,
-	fairwayAvailabilities map[uniqueFairwayAvailability]int64,
 	fas []*ifaf.FairwayAvailability,
 	tx *sql.Tx,
 	feedback Feedback,
@@ -355,25 +313,17 @@
 			feedback.Warn("Bottleneck %s not found in database.", faRes.Bottleneck_id)
 			continue
 		}
-		uniqueFa := uniqueFairwayAvailability{
-			BottleneckId: faRes.Bottleneck_id,
-			Surdat:       faRes.SURDAT,
-		}
-		var found bool
-		if faID, found = fairwayAvailabilities[uniqueFa]; !found {
-			err = insertFAStmt.QueryRowContext(
-				ctx,
-				faRes.POSITION,
-				faRes.Bottleneck_id,
-				faRes.SURDAT,
-				faRes.Critical,
-				faRes.Date_Info,
-				faRes.Source,
-			).Scan(&faID)
-			if err != nil {
-				return nil, err
-			}
-			fairwayAvailabilities[uniqueFa] = faID
+		err = insertFAStmt.QueryRowContext(
+			ctx,
+			faRes.POSITION,
+			faRes.Bottleneck_id,
+			faRes.SURDAT,
+			faRes.Critical,
+			faRes.Date_Info,
+			faRes.Source,
+		).Scan(&faID)
+		if err != nil {
+			return nil, err
 		}
 		feedback.Info("Processing for Bottleneck %s", faRes.Bottleneck_id)
 		faIDs = append(faIDs, faRes.Bottleneck_id)
--- a/schema/gemma.sql	Thu Jul 25 15:43:13 2019 +0200
+++ b/schema/gemma.sql	Thu Jul 25 16:46:06 2019 +0200
@@ -826,8 +826,8 @@
         id         int PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
         state      import_state NOT NULL DEFAULT 'queued',
         kind       varchar   NOT NULL,
-        enqueued   timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
-        due        timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+        enqueued   timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
+        due        timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
         retry_wait interval
             CHECK(retry_wait IS NULL
                 OR retry_wait >= interval '0 microseconds'),
@@ -850,7 +850,7 @@
     CREATE TABLE import_logs (
         import_id int NOT NULL REFERENCES imports(id)
             ON DELETE CASCADE,
-        time timestamp NOT NULL DEFAULT now(),
+        time timestamp with time zone NOT NULL DEFAULT now(),
         kind log_type NOT NULL DEFAULT 'info',
         msg TEXT NOT NULL
     )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/schema/updates/1010/01.timezones-imports.sql	Thu Jul 25 16:46:06 2019 +0200
@@ -0,0 +1,3 @@
+ALTER TABLE import.imports     ALTER COLUMN enqueued TYPE timestamp with time zone;
+ALTER TABLE import.imports     ALTER COLUMN due      TYPE timestamp with time zone;
+ALTER TABLE import.import_logs ALTER COLUMN time     TYPE timestamp with time zone;
--- a/schema/version.sql	Thu Jul 25 15:43:13 2019 +0200
+++ b/schema/version.sql	Thu Jul 25 16:46:06 2019 +0200
@@ -1,1 +1,1 @@
-INSERT INTO gemma_schema_version(version) VALUES (1009);
+INSERT INTO gemma_schema_version(version) VALUES (1010);