diff pkg/imports/bn.go @ 3648:0ec5c8ec1e44

Avoid empty validity time ranges An entry which is not valid at any point in time makes no sense. Further, multiple of such entries would violate a UNIQUE constraint. Since an UPDATE now can change validity time ranges, do all the adjustments for that regardles of whether an INSERT or UPDATE happens.
author Tom Gottfried <tom@intevation.de>
date Wed, 12 Jun 2019 18:26:26 +0200
parents 02951a62e8c6
children 2a079d0a71c1
line wrap: on
line diff
--- a/pkg/imports/bn.go	Wed Jun 12 18:21:14 2019 +0200
+++ b/pkg/imports/bn.go	Wed Jun 12 18:26:26 2019 +0200
@@ -49,8 +49,8 @@
     erased = true
   WHERE bottleneck_id = $1
     AND NOT erased
-    -- Don't touch old entry if validity did not change: will be updated
-    AND validity <> $2
+    -- Don't touch old entry if new validity contains old: will be updated
+    AND NOT validity <@ $2
   RETURNING 1
 )
 -- Decide whether a new version will be INSERTed
@@ -142,7 +142,8 @@
   revisiting_time = $10,
   limiting = $11,
   date_info = $12,
-  source_organization = $13
+  source_organization = $13,
+  validity = $15
 WHERE bottleneck_id = $1
   AND NOT erased
   AND $12 > date_info
@@ -442,19 +443,6 @@
 			feedback.Warn(handleError(err).Error())
 			return nil
 		}
-
-		// Set end of validity of old version to start of new version
-		// in case of overlap
-		if _, err = tx.StmtContext(ctx, fixValidityStmt).ExecContext(ctx,
-			bn.Bottleneck_id,
-			validity,
-		); err != nil {
-			feedback.Warn(handleError(err).Error())
-			if err2 := tx.Rollback(); err2 != nil {
-				return err2
-			}
-			return nil
-		}
 		feedback.Info("insert new version")
 	case !isNew:
 		// try to update
@@ -472,6 +460,7 @@
 			bn.Date_Info,
 			bn.Source,
 			tolerance,
+			&validity,
 		).Scan(&nid)
 		switch {
 		case err == sql.ErrNoRows:
@@ -514,6 +503,20 @@
 		}
 	}
 
+	// Set end of validity of old version to start of new version
+	// in case of overlap
+	if _, err = tx.StmtContext(ctx, fixValidityStmt).ExecContext(ctx,
+		bn.Bottleneck_id,
+		validity,
+	); err != nil {
+		feedback.Warn(handleError(err).Error())
+		if err2 := tx.Rollback(); err2 != nil {
+			return err2
+		}
+		return nil
+	}
+
+	// Insert riverbed materials
 	if materials != nil {
 		for _, mat := range materials {
 			if _, err := tx.StmtContext(ctx,