mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Remove extra rows with duplicate keys in SyncsModels trait (#13632)
Fixes issues with bad data being left in the DB if it has a duplicate key somehow.
This commit is contained in:
@@ -41,15 +41,23 @@ trait SyncsModels
|
|||||||
protected function syncModels($device, $relationship, $models): Collection
|
protected function syncModels($device, $relationship, $models): Collection
|
||||||
{
|
{
|
||||||
$models = $models->keyBy->getCompositeKey();
|
$models = $models->keyBy->getCompositeKey();
|
||||||
$existing = $device->$relationship->keyBy->getCompositeKey();
|
$existing = $device->$relationship->groupBy->getCompositeKey();
|
||||||
|
|
||||||
foreach ($existing as $exist_key => $exist_value) {
|
foreach ($existing as $exist_key => $existing_rows) {
|
||||||
if ($models->offsetExists($exist_key)) {
|
if ($models->offsetExists($exist_key)) {
|
||||||
// update
|
// update
|
||||||
$exist_value->fill($models->get($exist_key)->getAttributes())->save();
|
foreach ($existing_rows as $index => $existing_row) {
|
||||||
|
if ($index == 0) {
|
||||||
|
$existing_row->fill($models->get($exist_key)->getAttributes())->save();
|
||||||
|
} else {
|
||||||
|
// delete extra rows at this key
|
||||||
|
$existing_row->delete();
|
||||||
|
$existing_rows->forget($index);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// delete
|
// delete
|
||||||
$exist_value->delete();
|
$existing_rows->each->delete();
|
||||||
$existing->forget($exist_key);
|
$existing->forget($exist_key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,7 +65,7 @@ trait SyncsModels
|
|||||||
$new = $models->diffKeys($existing);
|
$new = $models->diffKeys($existing);
|
||||||
$device->$relationship()->saveMany($new);
|
$device->$relationship()->saveMany($new);
|
||||||
|
|
||||||
return $existing->merge($new);
|
return $existing->map->first()->merge($new);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user