diff --git a/Netdisco/Changes b/Netdisco/Changes index 7ac33d69..a6a64b32 100644 --- a/Netdisco/Changes +++ b/Netdisco/Changes @@ -6,6 +6,7 @@ * Fix for latest Dancer (YAML::XS) * [#160] Job Queue fatal error on num_slots * [#157] Device Port Log being emptied by device discover + * [#156] Only delete node_ip and node_nbt when no active nodes reference 2.029012 - 2014-10-09 diff --git a/Netdisco/lib/App/Netdisco/DB/ResultSet/Node.pm b/Netdisco/lib/App/Netdisco/DB/ResultSet/Node.pm index 3a143243..4b9c4b5b 100644 --- a/Netdisco/lib/App/Netdisco/DB/ResultSet/Node.pm +++ b/Netdisco/lib/App/Netdisco/DB/ResultSet/Node.pm @@ -109,9 +109,30 @@ sub delete { return 0E0; } else { + # for node_ip and node_nbt *only* delete if there are no longer + # any active nodes referencing the IP or NBT (hence 2nd IN clause). foreach my $set (qw/ NodeIp NodeNbt + /) { + $schema->resultset($set)->search({ + '-and' => [ + 'me.mac' => { '-in' => $nodes->as_query }, + 'me.mac' => { '-in' => $schema->resultset($set)->search({ + -bool => 'nodes.active', + }, + { + columns => 'mac', + join => 'nodes', + group_by => 'me.mac', + having => \[ 'count(nodes.mac) = 0' ], + })->as_query, + }, + ], + })->delete; + } + + foreach my $set (qw/ NodeMonitor NodeWireless /) {