mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
refactor: Replace custom queries with dbDeleteOrphans(). (#7862)
* Replace custom queries with dbDeleteOrphans(). Generalize dbDeleteOrphans() a bit more. Allow for multiple tables and custom key pairs. * fix whitespace
This commit is contained in:
committed by
Neil Lathwood
parent
daa0773f48
commit
8ab0b2bffd
@@ -298,22 +298,44 @@ function dbDelete($table, $where = null, $parameters = array())
|
||||
|
||||
/**
|
||||
* Delete orphaned entries from a table that no longer have a parent in parent_table
|
||||
* Format of parents array is as follows table.table_key_column<.target_key_column>
|
||||
*
|
||||
* @param string $parent_table
|
||||
* @param string $child_table
|
||||
* @param string $id_column
|
||||
* @param string $target_table The table to delete entries from
|
||||
* @param array $parents an array of parent tables to check.
|
||||
* @return bool|int
|
||||
*/
|
||||
function dbDeleteOrphans($parent_table, $child_table, $id_column)
|
||||
function dbDeleteOrphans($target_table, $parents)
|
||||
{
|
||||
global $database_link;
|
||||
$time_start = microtime(true);
|
||||
|
||||
$sql = "DELETE C FROM `$child_table` C";
|
||||
$sql .= " LEFT JOIN `$parent_table` P USING (`$id_column`)";
|
||||
$sql .= " WHERE P.`$id_column` IS NULL";
|
||||
if (empty($parents)) {
|
||||
// don't delete all entries if parents is missing
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = dbQuery($sql, array());
|
||||
$target_table = mres($target_table);
|
||||
$sql = "DELETE T FROM `$target_table` T";
|
||||
$where = array();
|
||||
|
||||
foreach ((array)$parents as $parent) {
|
||||
$parent_parts = explode('.', mres($parent));
|
||||
if (count($parent_parts) == 2) {
|
||||
list($parent_table, $parent_column) = $parent_parts;
|
||||
$target_column = $parent_column;
|
||||
} elseif (count($parent_parts) == 3) {
|
||||
list($parent_table, $parent_column, $target_column) = $parent_parts;
|
||||
} else {
|
||||
// invalid input
|
||||
return false;
|
||||
}
|
||||
|
||||
$sql .= " LEFT JOIN `$parent_table` ON `$parent_table`.`$parent_column` = T.`$target_column`";
|
||||
$where[] = " `$parent_table`.`$parent_column` IS NULL";
|
||||
}
|
||||
|
||||
$query = "$sql WHERE" . implode(' AND', $where);
|
||||
$result = dbQuery($query, array());
|
||||
|
||||
recordDbStatistic('delete', $time_start);
|
||||
if ($result) {
|
||||
|
@@ -97,7 +97,7 @@ if ($num > 0) {
|
||||
array_unshift($vars, $device['device_id']);
|
||||
dbDelete(
|
||||
'applications',
|
||||
'`device_id`=? AND `app_type` IN (' . implode(',', array_fill(0, $num, '?')) . ')',
|
||||
'`device_id`=? AND `app_type` IN ' . dbGenPlaceholders($num),
|
||||
$vars
|
||||
);
|
||||
foreach ($apps_to_remove as $app) {
|
||||
@@ -106,7 +106,7 @@ if ($num > 0) {
|
||||
}
|
||||
|
||||
// clean application_metrics
|
||||
dbDeleteOrphans('applications', 'application_metrics', 'app_id');
|
||||
dbDeleteOrphans('application_metrics', array('applications.app_id'));
|
||||
|
||||
echo PHP_EOL;
|
||||
|
||||
|
@@ -117,10 +117,7 @@ foreach ($vrfs_lite_cisco as $vrf) {
|
||||
}
|
||||
|
||||
// remove entries that no longer have an owner
|
||||
dbQuery('DELETE `ipv4_mac` FROM `ipv4_mac`
|
||||
LEFT JOIN `ports` ON `ipv4_mac`.`port_id` = `ports`.`port_id`
|
||||
LEFT JOIN `devices` ON `ipv4_mac`.`device_id` = `devices`.`device_id`
|
||||
WHERE `ports`.`port_id` IS NULL OR `devices`.`device_id` IS NULL');
|
||||
dbDeleteOrphans('ipv4_mac', array('ports.port_id', 'devices.device_id'));
|
||||
|
||||
echo PHP_EOL;
|
||||
unset(
|
||||
|
@@ -243,8 +243,7 @@ foreach (dbFetchRows($sql, array($device['device_id'])) as $test) {
|
||||
}
|
||||
|
||||
// remove orphaned links
|
||||
$del_result = dbQuery('DELETE `l` FROM `links` `l` LEFT JOIN `devices` `d` ON `d`.`device_id` = `l`.`local_device_id` WHERE `d`.`device_id` IS NULL');
|
||||
$deleted = mysqli_affected_rows($del_result);
|
||||
$deleted = (int)dbDeleteOrphans('links', array('devices.device_id.local_device_id'));
|
||||
echo str_repeat('-', $deleted);
|
||||
d_echo(" $deleted orphaned links deleted\n");
|
||||
|
||||
@@ -254,6 +253,5 @@ unset(
|
||||
$fdp_array,
|
||||
$cdp_array,
|
||||
$lldp_array,
|
||||
$del_result,
|
||||
$deleted
|
||||
);
|
||||
|
Reference in New Issue
Block a user