Skip to content

Commit f1a118c

Browse files
authored
Remove dbDeleteOrphans (librenms#18609)
Replace with simple Eloquent queries.
1 parent 47a6540 commit f1a118c

File tree

4 files changed

+5
-52
lines changed

4 files changed

+5
-52
lines changed

LibreNMS/Device/Processor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public static function runDiscovery(OS $os)
161161
);
162162
}
163163

164-
dbDeleteOrphans(static::$table, ['devices.device_id']);
164+
\App\Models\Processor::doesntHave('device')->delete();
165165

166166
echo PHP_EOL;
167167
}

includes/dbFacile.php

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -194,55 +194,6 @@ function dbDelete($table, $where = null, $parameters = [])
194194
return $result;
195195
}//end dbDelete()
196196

197-
/**
198-
* Delete orphaned entries from a table that no longer have a parent in parent_table
199-
* Format of parents array is as follows table.table_key_column<.target_key_column>
200-
*
201-
* @param string $target_table The table to delete entries from
202-
* @param array $parents an array of parent tables to check.
203-
* @return bool|int
204-
*
205-
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent#deleting-models
206-
* @see https://laravel.com/docs/eloquent#deleting-models
207-
*/
208-
function dbDeleteOrphans($target_table, $parents)
209-
{
210-
if (empty($parents)) {
211-
// don't delete all entries if parents is missing
212-
return false;
213-
}
214-
215-
$target_table = $target_table;
216-
$sql = "DELETE T FROM `$target_table` T";
217-
$where = [];
218-
219-
foreach ((array) $parents as $parent) {
220-
$parent_parts = explode('.', (string) $parent);
221-
if (count($parent_parts) == 2) {
222-
[$parent_table, $parent_column] = $parent_parts;
223-
$target_column = $parent_column;
224-
} elseif (count($parent_parts) == 3) {
225-
[$parent_table, $parent_column, $target_column] = $parent_parts;
226-
} else {
227-
// invalid input
228-
return false;
229-
}
230-
231-
$sql .= " LEFT JOIN `$parent_table` ON `$parent_table`.`$parent_column` = T.`$target_column`";
232-
$where[] = " `$parent_table`.`$parent_column` IS NULL";
233-
}
234-
235-
$query = "$sql WHERE" . implode(' AND', $where);
236-
237-
try {
238-
$result = Eloquent::DB()->delete($query);
239-
} catch (PDOException $pdoe) {
240-
dbHandleException(new QueryException('dbFacile', $query, [], $pdoe));
241-
}
242-
243-
return $result;
244-
}
245-
246197
/**
247198
* Fetches all of the rows (associatively) from the last performed query.
248199
* Most other retrieval functions build off this

includes/discovery/applications.inc.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use App\Facades\LibrenmsConfig;
2828
use App\Models\Application;
29+
use App\Models\ApplicationMetric;
2930
use App\Models\Eventlog;
3031
use App\Observers\ModuleModelObserver;
3132
use LibreNMS\Enum\Severity;
@@ -102,7 +103,7 @@
102103
});
103104

104105
// clean application_metrics
105-
dbDeleteOrphans('application_metrics', ['applications.app_id']);
106+
ApplicationMetric::doesntHave('app')->delete();
106107

107108
echo PHP_EOL;
108109

includes/discovery/discovery-protocols.inc.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use App\Facades\LibrenmsConfig;
4+
use App\Models\Link;
45
use App\Models\Ospfv3Nbr;
56
use LibreNMS\Util\IP;
67
use LibreNMS\Util\StringHelpers;
@@ -555,7 +556,7 @@
555556
}
556557

557558
// remove orphaned links
558-
$deleted = (int) dbDeleteOrphans('links', ['devices.device_id.local_device_id']);
559+
$deleted = Link::doesntHave('device')->delete();
559560
echo str_repeat('-', $deleted);
560561
d_echo(" $deleted orphaned links deleted\n");
561562

0 commit comments

Comments
 (0)