#476 log on delete for rows from related tables

This commit is contained in:
Oliver Gorwits
2019-01-02 21:44:36 +00:00
parent 3769b7b66b
commit 5bc0afa2f5
2 changed files with 40 additions and 3 deletions

View File

@@ -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(@_);

View File

@@ -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(