diff pkg/imports/bn.go @ 4133:eb08fbe33074

Fixed bn-import: never write tracking info to the db if import fails.
author Sascha Wilde <wilde@intevation.de>
date Thu, 01 Aug 2019 19:21:43 +0200
parents 980f12d3c766
children 9864d682ab47
line wrap: on
line diff
--- a/pkg/imports/bn.go	Thu Aug 01 19:12:47 2019 +0200
+++ b/pkg/imports/bn.go	Thu Aug 01 19:21:43 2019 +0200
@@ -523,20 +523,10 @@
 			"with intersecting validity already exists: "+
 			"REPLACING all of them!", bn.Bottleneck_id)
 	}
-
-	for _, oldID := range oldBnIds {
-		// It is possible, that two new bottlenecks intersect with the
-		// same old noe, therefor we have to handle duplicates in
-		// oldBnIds.
-		if !seenOldBnIds[oldID] {
-			if _, err := tx.StmtContext(ctx, bs.track).ExecContext(
-				ctx, importID, "waterway.bottlenecks", oldID, true,
-			); err != nil {
-				return err
-			}
-			seenOldBnIds[oldID] = true
-		}
-	}
+	// We write the actual tracking information for deletion of superseded
+	// bottlenecks later to the databs -- AFTER the new bottleneck was
+	// created successfully.  That way, we don't change the database, when
+	// an error arises during inserting the new data.
 
 	var bnIds []int64
 	// Add new BN data:
@@ -598,7 +588,21 @@
 		return nil
 	}
 
-	// Only add new BN data to tracking for staging review.
+	// Now that adding BNs to staging was successful, write import tracking
+	// information to database:
+	for _, oldID := range oldBnIds {
+		// It is possible, that two new bottlenecks intersect with the
+		// same old noe, therefor we have to handle duplicates in
+		// oldBnIds.
+		if !seenOldBnIds[oldID] {
+			if _, err := tx.StmtContext(ctx, bs.track).ExecContext(
+				ctx, importID, "waterway.bottlenecks", oldID, true,
+			); err != nil {
+				return err
+			}
+			seenOldBnIds[oldID] = true
+		}
+	}
 	for _, nid := range bnIds {
 		if _, err := tx.StmtContext(ctx, bs.track).ExecContext(
 			ctx, importID, "waterway.bottlenecks", nid, false,