diff --git a/lib/App/Netdisco/DB/ResultSet/Device.pm b/lib/App/Netdisco/DB/ResultSet/Device.pm index 1d755de3..bf3eab04 100644 --- a/lib/App/Netdisco/DB/ResultSet/Device.pm +++ b/lib/App/Netdisco/DB/ResultSet/Device.pm @@ -3,7 +3,10 @@ use base 'App::Netdisco::DB::ResultSet'; use strict; use warnings; + +use Try::Tiny; use NetAddr::IP::Lite ':lower'; +require Dancer::Logger; =head1 ADDITIONAL METHODS @@ -591,12 +594,23 @@ handle the removal or archiving of nodes. =cut +sub _plural { (shift || 0) == 1 ? 'entry' : 'entries' }; + sub delete { my $self = shift; my $schema = $self->result_source->schema; my $devices = $self->search(undef, { columns => 'ip' }); + my $ip = undef; + { + no autovivification; + try { $ip ||= $devices->{attrs}->{where}->{ip} }; + try { $ip ||= $devices->{attrs}->{where}->{'me.ip'} }; + } + die "cannot find IP address in \$devices query for delete\n" + unless $ip; + foreach my $set (qw/ DeviceIp DeviceVlan @@ -604,9 +618,12 @@ sub delete { DeviceModule Community /) { - $schema->resultset($set)->search( + my $gone = $schema->resultset($set)->search( { ip => { '-in' => $devices->as_query } }, )->delete; + + Dancer::Logger::debug sprintf ' [%s] db/device - removed %d %s from %s', + $ip, $gone, _plural($gone), $set if defined Dancer::Logger::logger(); } foreach my $set (qw/ @@ -618,13 +635,16 @@ sub delete { )->delete; } - $schema->resultset('Topology')->search({ + my $gone = $schema->resultset('Topology')->search({ -or => [ { dev1 => { '-in' => $devices->as_query } }, { dev2 => { '-in' => $devices->as_query } }, ], })->delete; + Dancer::Logger::debug sprintf ' [%s] db/device - removed %d manual topology %s', + $ip, $gone, _plural($gone) if defined Dancer::Logger::logger(); + $schema->resultset('DevicePort')->search( { ip => { '-in' => $devices->as_query } }, )->delete(@_); diff --git a/lib/App/Netdisco/DB/ResultSet/DevicePort.pm b/lib/App/Netdisco/DB/ResultSet/DevicePort.pm index cf0c978c..7ae80b5a 100644 --- a/lib/App/Netdisco/DB/ResultSet/DevicePort.pm +++ b/lib/App/Netdisco/DB/ResultSet/DevicePort.pm @@ -4,6 +4,9 @@ use base 'App::Netdisco::DB::ResultSet'; use strict; use warnings; +use Try::Tiny; +require Dancer::Logger; + __PACKAGE__->load_components(qw/ +App::Netdisco::DB::ExplicitLocking /); @@ -222,12 +225,23 @@ handle the removal or archiving of nodes. =cut +sub _plural { (shift || 0) == 1 ? 'entry' : 'entries' }; + sub delete { my $self = shift; my $schema = $self->result_source->schema; my $ports = $self->search(undef, { columns => 'ip' }); + my $ip = undef; + { + no autovivification; + try { $ip ||= ${ $ports->{attrs}->{where}->{ip}->{'-in'} }->[1]->[1] }; + try { $ip ||= $ports->{attrs}->{where}->{'me.ip'} }; + } + die "cannot find IP address in \$ports query for delete\n" + unless $ip; + foreach my $set (qw/ DevicePortPower DevicePortProperties @@ -235,9 +249,12 @@ sub delete { DevicePortWireless DevicePortSsid /) { - $schema->resultset($set)->search( + my $gone = $schema->resultset($set)->search( { ip => { '-in' => $ports->as_query }}, )->delete; + + Dancer::Logger::debug sprintf ' [%s] db/ports - removed %d port %s from %s', + $ip, $gone, _plural($gone), $set if defined Dancer::Logger::logger(); } $schema->resultset('Node')->search(