mongoid_retry 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +7 -0
- data/lib/mongoid/mongoid_retry.rb +48 -0
- data/lib/mongoid_retry.rb +1 -0
- data/lib/mongoid_retry/version.rb +3 -0
- data/mongoid_retry.gemspec +21 -0
- data/spec/retry_spec.rb +55 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/support/models/thing.rb +11 -0
- metadata +112 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Travis Dahlke
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
[](http://travis-ci.org/travisdahlke/mongoid_retry)
|
2
|
+
|
3
|
+
# MongoidRetry
|
4
|
+
|
5
|
+
Overcome duplicate key errors in MongoDB by catching the exception, finding the existing document, and updating it instead.
|
6
|
+
This is currently only compatible with Mongoid 2.x.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'mongoid_retry'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install mongoid_retry
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```
|
25
|
+
class Fruit
|
26
|
+
include Mongoid::Document
|
27
|
+
include Mongoid::MongoidRetry
|
28
|
+
|
29
|
+
field :name
|
30
|
+
index :type, unique: true
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
Fruit.new(type: 'apple').save_and_retry
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "mongoid_retry/version"
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module MongoidRetry
|
5
|
+
|
6
|
+
DUPLICATE_KEY_ERROR_CODES = [11000,11001]
|
7
|
+
|
8
|
+
# Catch a duplicate key error
|
9
|
+
def save_and_retry
|
10
|
+
begin
|
11
|
+
save!
|
12
|
+
rescue Mongo::OperationFailure => e
|
13
|
+
if is_a_duplicate_key_error?(e)
|
14
|
+
keys = duplicate_key(e)
|
15
|
+
if (duplicate = find_duplicate(keys))
|
16
|
+
update_document!(duplicate)
|
17
|
+
end
|
18
|
+
else
|
19
|
+
raise e
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def is_a_duplicate_key_error?(exception)
|
27
|
+
DUPLICATE_KEY_ERROR_CODES.include?(exception.error_code)
|
28
|
+
end
|
29
|
+
|
30
|
+
def find_duplicate(keys)
|
31
|
+
self.class.first(conditions: keys)
|
32
|
+
end
|
33
|
+
|
34
|
+
def duplicate_key(exception)
|
35
|
+
index = exception.message[/(?<=\.\$)\w*/,0]
|
36
|
+
fields = index.split(/\d/).map{|s| s.gsub(/^_|_-?$/,'')}
|
37
|
+
fields.inject({}) {|hash, key| hash[key] = send(key) if respond_to?(key); hash}
|
38
|
+
end
|
39
|
+
|
40
|
+
def update_document!(duplicate)
|
41
|
+
attributes.except("_id").each_pair do |key, value|
|
42
|
+
duplicate[key] = value
|
43
|
+
end
|
44
|
+
duplicate.save_and_retry
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "mongoid/mongoid_retry"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mongoid_retry/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Travis Dahlke"]
|
6
|
+
gem.email = ["travis.dahlke@tstmedia.com"]
|
7
|
+
gem.description = %q{Provides a 'save_and_retry' method that will attempt to save a document and, if a duplicate key error is thrown by mongodb, will update the existing document instead of failing.}
|
8
|
+
gem.summary = %q{Catch mongo duplicate key errors and retry}
|
9
|
+
gem.homepage = "http://www.github.com/travisdahlke/mongoid_retry"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "mongoid_retry"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = MongoidRetry::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "mongoid", ['~> 2.0']
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
gem.add_development_dependency('rake', ['>= 0.9.2'])
|
21
|
+
end
|
data/spec/retry_spec.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::MongoidRetry do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
Thing.create_indexes
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#save_and_retry" do
|
10
|
+
describe "when a document with the same key exists" do
|
11
|
+
before(:each) do
|
12
|
+
Thing.create(name: 'apple', color: 'red')
|
13
|
+
end
|
14
|
+
|
15
|
+
subject { Thing.new(name: 'apple', color: 'green') }
|
16
|
+
|
17
|
+
it "should not raise an exception" do
|
18
|
+
subject.save_and_retry
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should find and update the document" do
|
22
|
+
subject.save_and_retry
|
23
|
+
Thing.all.last.color.should == 'green'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not create a duplicate document" do
|
27
|
+
subject.save_and_retry
|
28
|
+
Thing.count.should == 1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "with compound indexes" do
|
33
|
+
before(:each) do
|
34
|
+
Thing.create(name: 'apple', color: 'red', shape: 'round')
|
35
|
+
end
|
36
|
+
|
37
|
+
subject { Thing.new(name: 'grapefruit', color: 'red', shape: 'round') }
|
38
|
+
|
39
|
+
it "should not raise an exception" do
|
40
|
+
subject.save_and_retry
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should find and update the document" do
|
44
|
+
subject.save_and_retry
|
45
|
+
Thing.all.last.name == 'block'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should not create a duplicate document" do
|
49
|
+
subject.save_and_retry
|
50
|
+
Thing.count.should == 1
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'mongoid'
|
5
|
+
require 'mongoid/mongoid_retry'
|
6
|
+
|
7
|
+
require 'rspec'
|
8
|
+
|
9
|
+
Mongoid.configure do |config|
|
10
|
+
config.master = Mongo::Connection.new.db('mongoid_retry_test')
|
11
|
+
config.allow_dynamic_fields = false
|
12
|
+
config.persist_in_safe_mode = true
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
16
|
+
|
17
|
+
RSpec.configure do |config|
|
18
|
+
config.mock_with :rspec
|
19
|
+
config.after :each do
|
20
|
+
Mongoid.master.collections.reject { |c| c.name =~ /^system\./ }.each(&:remove)
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_retry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Travis Dahlke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.9.2
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.9.2
|
62
|
+
description: Provides a 'save_and_retry' method that will attempt to save a document
|
63
|
+
and, if a duplicate key error is thrown by mongodb, will update the existing document
|
64
|
+
instead of failing.
|
65
|
+
email:
|
66
|
+
- travis.dahlke@tstmedia.com
|
67
|
+
executables: []
|
68
|
+
extensions: []
|
69
|
+
extra_rdoc_files: []
|
70
|
+
files:
|
71
|
+
- .gitignore
|
72
|
+
- .travis.yml
|
73
|
+
- Gemfile
|
74
|
+
- LICENSE
|
75
|
+
- README.md
|
76
|
+
- Rakefile
|
77
|
+
- lib/mongoid/mongoid_retry.rb
|
78
|
+
- lib/mongoid_retry.rb
|
79
|
+
- lib/mongoid_retry/version.rb
|
80
|
+
- mongoid_retry.gemspec
|
81
|
+
- spec/retry_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
- spec/support/models/thing.rb
|
84
|
+
homepage: http://www.github.com/travisdahlke/mongoid_retry
|
85
|
+
licenses: []
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.24
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Catch mongo duplicate key errors and retry
|
108
|
+
test_files:
|
109
|
+
- spec/retry_spec.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- spec/support/models/thing.rb
|
112
|
+
has_rdoc:
|