# HG changeset patch # User Sascha L. Teichmann # Date 1564043556 -7200 # Node ID 15c9d4064f0f15ac160443b789d42e169b26181f # Parent 07d853f9bf4767739c68519a5233317106ca08b5 AGM import: Mixed deletes with insert and updates to have them sorted by ISRS locations. diff -r 07d853f9bf47 -r 15c9d4064f0f pkg/imports/agm.go --- 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, diff -r 07d853f9bf47 -r 15c9d4064f0f pkg/models/isrs.go --- 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 }