changeset 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 07d853f9bf47
children f391497287fb
files pkg/imports/agm.go pkg/models/isrs.go
diffstat 2 files changed, 14 insertions(+), 35 deletions(-) [+]
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,
--- a/pkg/models/isrs.go	Thu Jul 25 09:15:53 2019 +0200
+++ b/pkg/models/isrs.go	Thu Jul 25 10:32:36 2019 +0200
@@ -44,12 +44,6 @@
 }
 
 func (isrs *Isrs) Less(other *Isrs) bool {
-	if isrs.Hectometre < other.Hectometre {
-		return true
-	}
-	if isrs.Hectometre > other.Hectometre {
-		return false
-	}
 	if isrs.CountryCode < other.CountryCode {
 		return true
 	}
@@ -71,6 +65,12 @@
 	if isrs.Orc < other.Orc {
 		return true
 	}
+	if isrs.Orc > other.Orc {
+		return false
+	}
+	if isrs.Hectometre < other.Hectometre {
+		return true
+	}
 	return false
 }