From 94f80f3c7062dc4ac95555dffc90d93e2b62c1de Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Thu, 4 Jan 2018 20:17:47 +0000 Subject: [PATCH] Make expire_nodes and expire_nodes_archive behave correctly --- lib/App/Netdisco/DB/ResultSet/Node.pm | 51 ++++++++++++------------ lib/App/Netdisco/Worker/Plugin/Expire.pm | 8 ++++ 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/lib/App/Netdisco/DB/ResultSet/Node.pm b/lib/App/Netdisco/DB/ResultSet/Node.pm index a0e48826..adc6027a 100644 --- a/lib/App/Netdisco/DB/ResultSet/Node.pm +++ b/lib/App/Netdisco/DB/ResultSet/Node.pm @@ -112,39 +112,38 @@ sub delete { foreach my $set (qw/ NodeMonitor NodeWireless + NodeNbt /) { $schema->resultset($set)->search( { mac => { '-in' => $nodes->as_query }}, )->delete; } + # for node_ip only delete if there are no longer + # any nodes referencing the IP. + my $except = $nodes->search(undef, { '+columns' => 'port' })->as_query; + my @except_all = @{$$except}; + my $except_query = shift @except_all; + + my $deleted = $schema->storage->dbh_do(sub { + my ($storage, $dbh, @extra) = @_; + local $dbh->{TraceLevel} = '1|SQL'; + $dbh->do(<[1]} @except_all)); + }); + # now let DBIC do its thing - my @retval = ($self->next::method()); - - # 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({ - 'me.mac' => { '-in' => $schema->resultset($set)->search({ - '-and' => [ - -bool => 'nodes.active', - 'me.mac' => { '-in' => $nodes->as_query } - ] - }, - { - columns => 'mac', - join => 'nodes', - group_by => 'me.mac', - having => \[ 'count(nodes.mac) = 0' ], - })->as_query, - }, - })->delete; - } - - return (wantarray ? @retval : $retval[0]); + return $self->next::method(); } } diff --git a/lib/App/Netdisco/Worker/Plugin/Expire.pm b/lib/App/Netdisco/Worker/Plugin/Expire.pm index 0625338d..1a8835d7 100644 --- a/lib/App/Netdisco/Worker/Plugin/Expire.pm +++ b/lib/App/Netdisco/Worker/Plugin/Expire.pm @@ -40,6 +40,14 @@ register_worker({ phase => 'main' }, sub { }); } + # also have to clean up node_ip that have no correspoding node + schema('netdisco')->resultset('NodeIp')->search({ + mac => { -in => schema('netdisco')->resultset('NodeIp')->search( + { port => undef }, + { join => 'nodes', select => [{ distinct => 'me.mac' }], } + )->as_query }, + })->delete; + if (setting('expire_jobs') and setting('expire_jobs') > 0) { schema('netdisco')->txn_do_locked('admin', 'EXCLUSIVE', sub { schema('netdisco')->resultset('Admin')->search({