fix: update rows when already exist during an import

When rows in the table devices, sensors, humidities, pressures and
temperatures already exist, they will only be updated.
This commit is contained in:
Markus Pesch 2020-12-14 20:16:35 +01:00
parent 522fe2746a
commit 23695b4513
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982

View File

@ -271,18 +271,21 @@ func Import(sourceDSNURL *url.URL, destDNSURL *url.URL, flogger logger.Logger, o
} }
} }
// Initialize source repository
sourceRepo, err := New(sourceDSNURL, flogger) sourceRepo, err := New(sourceDSNURL, flogger)
if err != nil { if err != nil {
return fmt.Errorf("Failed to open the source repo: %w", err) return fmt.Errorf("Failed to open the source repo: %w", err)
} }
defer sourceRepo.Close() defer sourceRepo.Close()
// Initialize destination repository
destRepo, err := New(destDNSURL, flogger) destRepo, err := New(destDNSURL, flogger)
if err != nil { if err != nil {
return fmt.Errorf("Failed to open the destination repo: %w", err) return fmt.Errorf("Failed to open the destination repo: %w", err)
} }
defer destRepo.Close() defer destRepo.Close()
// AddOrUpdate: Devices
ctx := context.Background() ctx := context.Background()
devices, err := sourceRepo.database.SelectDevices(ctx) devices, err := sourceRepo.database.SelectDevices(ctx)
if err != nil { if err != nil {
@ -292,12 +295,13 @@ func Import(sourceDSNURL *url.URL, destDNSURL *url.URL, flogger logger.Logger, o
flogger.Debug("Found %v devices", len(devices)) flogger.Debug("Found %v devices", len(devices))
for i := range devices { for i := range devices {
err := destRepo.AddDevices(devices[i]) err := destRepo.AddOrUpdateDevices(devices[i])
if err != nil { if err != nil {
return fmt.Errorf("Failed to add device %v into dest repo: %w", devices[i].Name, err) return fmt.Errorf("Failed to add or update device %v into dest repo: %w", devices[i].Name, err)
} }
} }
// AddOrUpdate: Sensors
if optImport.Sensors { if optImport.Sensors {
sensors, err := sourceRepo.database.SelectSensors(ctx) sensors, err := sourceRepo.database.SelectSensors(ctx)
if err != nil { if err != nil {
@ -307,9 +311,9 @@ func Import(sourceDSNURL *url.URL, destDNSURL *url.URL, flogger logger.Logger, o
flogger.Debug("Found %v sensors", len(sensors)) flogger.Debug("Found %v sensors", len(sensors))
for i := range sensors { for i := range sensors {
err := destRepo.AddSensors(sensors[i]) err := destRepo.AddOrUpdateSensors(sensors[i])
if err != nil { if err != nil {
return fmt.Errorf("Failed to add sensor %v into dest repo: %w", sensors[i].Name, err) return fmt.Errorf("Failed to add or update sensor %v into dest repo: %w", sensors[i].Name, err)
} }
} }
} }
@ -327,9 +331,9 @@ func Import(sourceDSNURL *url.URL, destDNSURL *url.URL, flogger logger.Logger, o
flogger.Debug("Found %v %v values", len(measuredValues), key) flogger.Debug("Found %v %v values", len(measuredValues), key)
err = destRepo.AddMeasuredValues(measuredValues...) err = destRepo.AddOrUpdateMeasuredValues(measuredValues...)
if err != nil { if err != nil {
return fmt.Errorf("Failed to add %v into dest repo: %w", key, err) return fmt.Errorf("Failed to add or update %v into dest repo: %w", key, err)
} }
} }
} }