diff pkg/imports/agm.go @ 4060:15c9d4064f0f

AGM import: Mixed deletes with insert and updates to have them sorted by ISRS locations.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 25 Jul 2019 10:32:36 +0200
parents 4a7c2140e44b
children 4332b9e26e2f eb2f949ddfa2
line wrap: on
line diff
--- a/pkg/imports/agm.go	Thu Jul 25 09:15:53 2019 +0200
+++ b/pkg/imports/agm.go	Thu Jul 25 10:32:36 2019 +0200
@@ -498,10 +498,9 @@
 	var removed int
 
 	// Issue deletes
-	for _, key := range sortByLocationCode(oldGMLines) {
-		old := oldGMLines[key]
+	for _, old := range oldGMLines {
 		removed += len(old)
-		for _, line := range sortByMeasureDate(old) {
+		for _, line := range old {
 			if _, err := txTrackStmt.ExecContext(
 				ctx, importID, "waterway.gauge_measurements",
 				line.id,
@@ -528,6 +527,12 @@
 		return nil, fmt.Errorf("Commit failed: %v", err)
 	}
 
+	// Sort here to mix the deletes right beside the matching inserts/updates.
+	// This also makes the output deterministic.
+	sort.Slice(entries, func(i, j int) bool {
+		return entries[i].FKGaugeID.Less(&entries[j].FKGaugeID)
+	})
+
 	feedback.Info("Imported %d entries with changes", len(entries))
 	feedback.Info("Importing approved gauge measurements took %s",
 		time.Since(start))
@@ -535,32 +540,6 @@
 	return entries, nil
 }
 
-func sortByLocationCode(data map[models.Isrs]map[int64]*agmLine) []models.Isrs {
-	keys := make([]models.Isrs, len(data))
-	var i int
-	for key := range data {
-		keys[i] = key
-		i++
-	}
-	sort.Slice(keys, func(i, j int) bool {
-		return keys[i].Less(&keys[j])
-	})
-	return keys
-}
-
-func sortByMeasureDate(data map[int64]*agmLine) []*agmLine {
-	lines := make([]*agmLine, len(data))
-	var i int
-	for _, line := range data {
-		lines[i] = line
-		i++
-	}
-	sort.Slice(lines, func(i, j int) bool {
-		return lines[i].MeasureDate.Before(lines[j].MeasureDate.Time)
-	})
-	return lines
-}
-
 func getOldGMLines(
 	ctx context.Context,
 	stmt *sql.Stmt,