Skip to content

Commit

Permalink
Update to MongoDB v1.0.0 API
Browse files Browse the repository at this point in the history
Fixes #17.
  • Loading branch information
xdg committed Dec 12, 2016
1 parent c8e8e71 commit 3015cc9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
9 changes: 9 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ Revision history for Meerkat

{{$NEXT}}

[FIXED]

- No longer issues deprecation warnings (or fails tests) with
MongoDB v1.6.0, which warns when using deprecated legacy methods.

[PREREQS]

- Requires MongoDB Perl Driver v1.0.0 or later

0.014 2016-02-27 08:20:44-05:00 America/New_York

[ADDED]
Expand Down
8 changes: 4 additions & 4 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ my %WriteMakefileArgs = (
"Carp" => 0,
"DateTime" => 0,
"Module::Runtime" => 0,
"MongoDB" => 0,
"MongoDB" => 1,
"MongoDB::OID" => 0,
"Moose" => 2,
"Moose::Role" => 2,
"MooseX::AttributeShortcuts" => 0,
"MooseX::Role::Logger" => 0,
"MooseX::Role::MongoDB" => "0.009",
"MooseX::Role::MongoDB" => "0.01",
"MooseX::Storage" => 0,
"MooseX::Storage::Engine" => 0,
"MooseX::Types" => 0,
Expand Down Expand Up @@ -70,14 +70,14 @@ my %FallbackPrereqs = (
"ExtUtils::MakeMaker" => 0,
"File::Spec" => 0,
"Module::Runtime" => 0,
"MongoDB" => 0,
"MongoDB" => 1,
"MongoDB::OID" => 0,
"MooX::Types::MooseLike::Base" => 0,
"Moose" => 2,
"Moose::Role" => 2,
"MooseX::AttributeShortcuts" => 0,
"MooseX::Role::Logger" => 0,
"MooseX::Role::MongoDB" => "0.009",
"MooseX::Role::MongoDB" => "0.01",
"MooseX::Storage" => 0,
"MooseX::Storage::Engine" => 0,
"MooseX::Types" => 0,
Expand Down
4 changes: 2 additions & 2 deletions lib/Meerkat.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use MooseX::AttributeShortcuts;

use Meerkat::Collection;
use Module::Runtime qw/require_module compose_module_name/;
use MongoDB;
use MongoDB 1;
use Try::Tiny;
use Type::Params qw/compile/;
use Types::Standard qw/:types/;

use namespace::autoclean;

with 'MooseX::Role::Logger', 'MooseX::Role::MongoDB' => { -version => 0.009 };
with 'MooseX::Role::Logger', 'MooseX::Role::MongoDB' => { -version => 0.010 };

=attr model_namespace (required)
Expand Down
33 changes: 16 additions & 17 deletions lib/Meerkat/Collection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,9 @@ sub find {
$person->ensure_indexes;
Executes MongoDB's L<ensure_index|MongoDB::Collection/ensure_index> for every
index returned by the C<_index> method of the associated class. Returns true
on success or throws an error if one occurs. See L<Meerkat::Role::Document> for
more.
Ensures an index is constructed for index returned by the C<_index> method
of the associated class. Returns true on success or throws an error if one
occurs. See L<Meerkat::Role::Document> for more.
=cut

Expand All @@ -212,19 +211,20 @@ sub ensure_indexes {
my ($self) = $check->(@_);
state $aoa_check = compile( slurpy ArrayRef [ArrayRef] );
my ($aoa) = $aoa_check->( $self->class->_indexes );
my $index_view = $self->_mongo_collection->indexes;
my @indexes;
for my $index (@$aoa) {
my @copy = @$index;
my $options = ref $copy[0] eq 'HASH' ? shift @copy : {};
my $options = ref $copy[0] eq 'HASH' ? shift @copy : undef;
if ( @copy % 2 != 0 ) {
$self->_croak(
"_indexes must provide a list of key/value pairs, with an optional leading hashref"
);
}
my $spec = Tie::IxHash->new(@copy);
$self->_try_mongo_op(
ensure_indexes => sub { $self->_mongo_collection->ensure_index( $spec, $options ) }
);
push @indexes, { keys => $spec, ( $options ? ( options => $options ) : () ) };
}
$self->_try_mongo_op( ensure_indexes => sub { $index_view->create_many(@indexes) } );
return 1;
}

Expand All @@ -237,7 +237,7 @@ sub remove {
state $check = compile( Object, Object );
my ( $self, $obj ) = $check->(@_);
$self->_try_mongo_op(
remove => sub { $self->_mongo_collection->remove( { _id => $obj->_id } ) } );
remove => sub { $self->_mongo_collection->delete_one( { _id => $obj->_id } ) } );
$obj->_set_removed(1);
return 1;
}
Expand Down Expand Up @@ -271,13 +271,8 @@ sub update {
my ( $self, $obj, $update ) = $check->(@_);
my $data = $self->_try_mongo_op(
update => sub {
$self->_mongo_collection->find_and_modify(
{
query => { _id => $obj->_id },
update => $update,
new => 1,
}
);
$self->_mongo_collection->find_one_and_update( { _id => $obj->_id },
$update, { returnDocument => "after" } );
},
);

Expand Down Expand Up @@ -327,7 +322,11 @@ sub _save {
my ( $self, $obj ) = $check->(@_);
my $pack = $obj->pack;
delete $pack->{$_} for qw/__CLASS__ _collection _removed/;
return $self->_try_mongo_op( sync => sub { !!$self->_mongo_collection->save($pack) }
return $self->_try_mongo_op(
sync => sub {
!!$self->_mongo_collection->replace_one( { _id => $pack->{_id} },
$pack, { upsert => 1 } );
}
);
}

Expand Down
2 changes: 1 addition & 1 deletion t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ test 'create indexes' => sub {
my $self = shift;
$self->create_person;
ok( $self->person->ensure_indexes, "created indexes" );
my @got = $self->person->_mongo_collection->get_indexes;
my @got = $self->person->_mongo_collection->indexes->list->all;
my @expected = $self->person->class->_indexes;
is( scalar @got, 1 + @expected, "correct number of indexes" )
or diag explain \@expected;
Expand Down
3 changes: 2 additions & 1 deletion t/exceptions.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ test 'bad sync' => sub {
my $copy = $self->person->find_id( $obj->_id );

# intentionally create a bad document
$self->person->_mongo_collection->update( { _id => $obj->_id }, { name => [] } );
$self->person->_mongo_collection->replace_one( { _id => $obj->_id },
{ name => [] } );

like(
exception { $obj->sync },
Expand Down

0 comments on commit 3015cc9

Please sign in to comment.