File Coverage

blib/lib/MongoDB/Op/_DropIndexes.pm
Criterion Covered Total %
statement 27 39 69.2
branch 0 10 0.0
condition 0 3 0.0
subroutine 9 10 90.0
pod 0 1 0.0
total 36 63 57.1


line stmt bran cond sub pod time code
1             # Copyright 2016 - present MongoDB, Inc.
2             #
3             # Licensed under the Apache License, Version 2.0 (the "License");
4             # you may not use this file except in compliance with the License.
5             # You may obtain a copy of the License at
6             #
7             # http://www.apache.org/licenses/LICENSE-2.0
8             #
9             # Unless required by applicable law or agreed to in writing, software
10             # distributed under the License is distributed on an "AS IS" BASIS,
11             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12             # See the License for the specific language governing permissions and
13             # limitations under the License.
14              
15 58     58   422 use strict;
  58         143  
  58         1912  
16 58     58   362 use warnings;
  58         132  
  58         2332  
17             package MongoDB::Op::_DropIndexes;
18              
19             # Implements index drops; returns a MongoDB::CommandResult
20              
21 58     58   380 use version;
  58         176  
  58         364  
22             our $VERSION = 'v2.2.0';
23              
24 58     58   4827 use Moo;
  58         133  
  58         345  
25              
26 58     58   18572 use MongoDB::Error;
  58         146  
  58         7234  
27 58     58   473 use MongoDB::Op::_Command;
  58         160  
  58         1624  
28 58     58   383 use Safe::Isa;
  58         187  
  58         9022  
29 58         509 use MongoDB::_Types qw(
30             Numish
31             Stringish
32 58     58   427 );
  58         162  
33              
34 58     58   40023 use namespace::clean;
  58         159  
  58         407  
35              
36             has index_name => (
37             is => 'ro',
38             required => 1,
39             isa => Stringish,
40             );
41              
42             has max_time_ms => (
43             is => 'ro',
44             isa => Numish,
45             );
46              
47             with $_ for qw(
48             MongoDB::Role::_PrivateConstructor
49             MongoDB::Role::_CollectionOp
50             MongoDB::Role::_WriteOp
51             );
52              
53             sub execute {
54 0     0 0   my ( $self, $link ) = @_;
55              
56             my $op = MongoDB::Op::_Command->_new(
57             db_name => $self->db_name,
58             query => [
59             dropIndexes => $self->coll_name,
60             index => $self->index_name,
61 0 0         ( $link->supports_helper_write_concern ? ( @{ $self->write_concern->as_args } ) : () ),
  0 0          
62             (defined($self->max_time_ms)
63             ? (maxTimeMS => $self->max_time_ms)
64             : ()
65             ),
66             ],
67             query_flags => {},
68             bson_codec => $self->bson_codec,
69             monitoring_callback => $self->monitoring_callback,
70             );
71              
72 0           my $res;
73 0           eval {
74 0           $res = $op->execute($link);
75 0           $res->assert_no_write_concern_error;
76             };
77             # XXX This logic will be a problem for command monitoring - may need to
78             # move into Op::_Command as an 'error_filter' callback or something.
79 0 0         if ( my $err = $@ ) {
80 0 0         if ( $err->$_isa("MongoDB::DatabaseError") ) {
81             # 2.6 and 3.0 don't have the code, so we fallback to string
82             # matching on the error message
83 0 0 0       return $err->result
84             if $err->code == INDEX_NOT_FOUND() || $err->message =~ /index not found with name/;
85             }
86 0           die $err;
87             }
88              
89 0           return $res;
90             }
91              
92             1;