Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
factor out unneeded explicit typecasts in database queries
Browse files Browse the repository at this point in the history
  • Loading branch information
karenetheridge committed Nov 30, 2020
1 parent 1090fc6 commit 4838137
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions lib/Conch/Controller/Build.pm
Expand Up @@ -211,13 +211,13 @@ sub update ($c) {
my $unhealthy_devices =
$build->search_related('devices', {
health => { '!=' => 'pass' },
phase => { '<' => \[ '?::device_phase_enum', 'production' ] },
phase => { '<' => 'production' },
})->count
+ $build
->related_resultset('racks')
->related_resultset('device_locations')
->search_related('device', {
'device.phase' => { '<' => \[ '?::device_phase_enum', 'production' ] },
'device.phase' => { '<' => 'production' },
health => { '!=' => 'pass' }
})
->count;
Expand Down Expand Up @@ -672,7 +672,7 @@ sub find_devices ($c) {
{
'device.build_id' => undef,
'rack.build_id' => $build_id,
$bad_phase ? ('device.phase' => { '<' => \[ '?::device_phase_enum', $bad_phase ] }) : (),
$bad_phase ? ('device.phase' => { '<' => $bad_phase }) : (),
},
] },
{ join => { device_location => 'rack' } },
Expand Down Expand Up @@ -714,7 +714,7 @@ sub get_devices ($c) {
$rs = $rs->search({ health => $params->{health} }) if $params->{health};
$rs = $rs->search({ 'device.phase' => $params->{phase} }) if $params->{phase};

$rs = $rs->search({ last_seen => { '>' => \[ 'now() - ?::interval', $params->{active_minutes}.' minutes' ] } })
$rs = $rs->search({ last_seen => { '>' => \[ q{now() - interval '1 minute' * ?}, $params->{active_minutes} ] } })
if $params->{active_minutes};

if ($params->{ids_only}) {
Expand Down Expand Up @@ -798,7 +798,7 @@ sub create_and_add_devices ($c) {
}

return $c->status(409, { error => 'cannot add devices to a build when in production (or later) phase' })
if $device_rs->search({ 'device.phase' => { '>=' => \[ '?::device_phase_enum', 'production' ] } })->exists;
if $device_rs->search({ 'device.phase' => { '>=' => 'production' } })->exists;

%devices = map +($_->id => $_), $device_rs->all;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/Controller/Device.pm
Expand Up @@ -214,7 +214,7 @@ sub lookup_by_other_attribute ($c) {
$device_rs = $device_rs->search(
{
# production devices do not consider interface data to be canonical
$device_rs->current_source_alias.'.phase' => { '<' => \[ '?::device_phase_enum', 'production' ] },
$device_rs->current_source_alias.'.phase' => { '<' => 'production' },
'device_nics.'.$key => $value,
},
{ join => 'device_nics' },
Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/Controller/Rack.pm
Expand Up @@ -369,7 +369,7 @@ sub set_assignment ($c) {
if $c->stash('rack_rs')->related_resultset('build')->search({ completed => { '!=' => undef } })->exists;

return $c->status(409, { error => 'cannot add devices to a rack in production (or later) phase' })
if $c->stash('rack_rs')->search({ phase => { '>=' => \[ '?::device_phase_enum', 'production' ] } })->exists;
if $c->stash('rack_rs')->search({ phase => { '>=' => 'production' } })->exists;

# in order to determine if we have duplicate devices, we need to look up all ids for device
# serial numbers...
Expand Down
2 changes: 1 addition & 1 deletion lib/Conch/DB/Helper/ResultSet/WithRole.pm
Expand Up @@ -38,7 +38,7 @@ sub with_role ($self, $role) {
if !$ENV{MOJO_MODE} and not $self->result_source->has_column('role');

return $self->search if $role eq 'ro';
$self->search({ $self->current_source_alias.'.role' => { '>=' => \[ '?::role_enum', $role ] } });
$self->search({ $self->current_source_alias.'.role' => { '>=' => $role } });
}

1;
Expand Down
4 changes: 2 additions & 2 deletions lib/Conch/DB/ResultSet/Build.pm
Expand Up @@ -60,15 +60,15 @@ sub with_user_role ($self, $user_id, $role) {

my $via_user_rs = $self->search(
{
$role ne 'ro' ? ('user_build_roles.role' => { '>=' => \[ '?::role_enum', $role ] } ) : (),
$role ne 'ro' ? ('user_build_roles.role' => { '>=' => $role } ) : (),
'user_build_roles.user_id' => $user_id,
},
{ join => 'user_build_roles' },
);

my $via_org_rs = $self->search(
{
$role ne 'ro' ? ('organization_build_roles.role' => { '>=' => \[ '?::role_enum', $role ] }) : (),
$role ne 'ro' ? ('organization_build_roles.role' => { '>=' => $role }) : (),
'user_organization_roles.user_id' => $user_id,
},
{ join => { organization_build_roles => { organization => 'user_organization_roles' } } } );
Expand Down
4 changes: 2 additions & 2 deletions lib/Conch/DB/ResultSet/Device.pm
Expand Up @@ -44,7 +44,7 @@ sub with_user_role ($self, $user_id, $role) {
my $devices_in_rack_builds = $self->search(
{
# production devices do not consider location data to be canonical
$me.'.phase' => { '<' => \[ '?::device_phase_enum', 'production' ] },
$me.'.phase' => { '<' => 'production' },
'rack.build_id' => { -in => $build_ids_rs->as_query },
},
{ join => { device_location => 'rack' } },
Expand Down Expand Up @@ -93,7 +93,7 @@ sub user_has_role ($self, $user_id, $role) {
# device -> rack -> build -> organization_build_role -> organization -> user
$self
# production devices do not consider location data to be canonical
->search({ $self->current_source_alias.'.phase' => { '<' => \[ '?::device_phase_enum', 'production' ] } })
->search({ $self->current_source_alias.'.phase' => { '<' => 'production' } })
->related_resultset('device_location')
->related_resultset('rack')
->user_has_role($user_id, $role);
Expand Down

0 comments on commit 4838137

Please sign in to comment.