Avoid macsuck generated SQL bug when cleaning NULL VLAN

This commit is contained in:
Oliver Gorwits
2013-10-06 18:08:06 +01:00
parent 4d9f16db3a
commit 794dd03b14
3 changed files with 34 additions and 34 deletions

View File

@@ -130,53 +130,48 @@ sub store_node {
my $nodes = schema('netdisco')->resultset('Node');
# TODO: probably needs changing if we're to support VTP domains
my $old = $nodes->search(
{
mac => $mac,
vlan => $vlan,
-bool => 'active',
-not => {
switch => $ip,
port => $port,
},
});
my $old = $nodes->search({
mac => $mac,
vlan => $vlan,
-bool => 'active',
-not => {
switch => $ip,
port => $port,
},
});
# lock rows,
# and get the count so we know whether to set time_recent
my $old_count = scalar $old->search(undef,
{
columns => [qw/switch vlan port mac/],
order_by => [qw/switch vlan port mac/],
for => 'update',
})->all;
$old->update({ active => \'false' });
my $new = $nodes->search(
{
'me.switch' => $ip,
'me.port' => $port,
'me.mac' => $mac,
},
{
order_by => [qw/switch vlan port mac/],
for => 'update',
});
# lock rows
$new->search({vlan => [$vlan, 0, undef]})->first;
# upgrade old schema
$new->search({vlan => [0, undef]})->delete();
my $new = $nodes->search({
'me.switch' => $ip,
'me.port' => $port,
'me.mac' => $mac,
});
# new data
$new->update_or_create({
vlan => $vlan,
active => \'true',
oui => substr($mac,0,8),
time_last => \$now,
($old_count ? (time_recent => \$now) : ()),
});
$new->update_or_create(
{
vlan => $vlan,
active => \'true',
oui => substr($mac,0,8),
time_last => \$now,
($old_count ? (time_recent => \$now) : ()),
},
{ for => 'update' }
);
# upgrade old schema
# an entry for this MAC existed in old schema with vlan => null
# which now has either vlan number or 0
$new->search({vlan => undef})->delete({only_nodes => 1});
});
}

View File

@@ -100,6 +100,10 @@ sub delete {
# avoid letting DBIC delete nodes
return 0E0;
}
elsif (exists $opts->{only_nodes} and $opts->{only_nodes}) {
# now let DBIC do its thing
return $self->next::method();
}
elsif (exists $opts->{keep_nodes} and $opts->{keep_nodes}) {
# avoid letting DBIC delete nodes
return 0E0;