atomic_first_or_create 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +7 -0
- data/atomic_first_or_create.gemspec +26 -0
- data/lib/atomic_first_or_create.rb +21 -0
- data/lib/atomic_first_or_create/version.rb +3 -0
- data/test/test_atomic.rb +35 -0
- data/test/test_helper.rb +19 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d2264c3cddeb6d09f64944764365d93d5e024dbb61feeccdce0a9e39585b9b6d
|
4
|
+
data.tar.gz: 1d35576d14960be88b037f292e9fab1f64bdf2f44a0537bd7c08bba3e57c0fac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 686ee67eb5226075f8fb28d051500d4ae7a99f2dcd85e76cd048eafcc877ec96af9eb31d57b0c21c8d53a9348a24b2d79659fb8d3579b86d89b2832473977293
|
7
|
+
data.tar.gz: dcbbd1f3422fb90baf6c59266231dc2970890172175d64bdd4380d59a492c500f15d723f029999306d5d610da34fa1d8c1bb3ed4051e5a8b9fd184f52579dd1c
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Guilherme Pereira
|
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,59 @@
|
|
1
|
+
# AtomicFirstOrCreate
|
2
|
+
|
3
|
+
ActiveRecord `first_or_create` alternative that retries on RecordNotUnique exception.
|
4
|
+
|
5
|
+
`first_or_create` does not guarantee uniqueness by itself, and if there is a
|
6
|
+
uniqueness constraint on the database, it may fail with a RecordNotUnique
|
7
|
+
exception. This gem adds `atomic_first_or_create` to ActiveRecord::Relation,
|
8
|
+
which, in conjunction with a uniqueness constraint, provides the correct
|
9
|
+
behaviour.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'atomic_first_or_create'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install atomic_first_or_create
|
24
|
+
|
25
|
+
## Solution
|
26
|
+
|
27
|
+
As documented by the rails team, methods like `first_or_create` or
|
28
|
+
`find_or_create` are not atomic:
|
29
|
+
|
30
|
+
"Please note *this method is not atomic*, it runs first a SELECT, and if there
|
31
|
+
are no results an INSERT is attempted. If there are other threads or processes
|
32
|
+
there is a race condition between both calls and it could be the case that you
|
33
|
+
end up with two similar records."
|
34
|
+
|
35
|
+
The documentation suggests using a UNIQUE constraint on the rows which you wish
|
36
|
+
to enforce uniqueness, and retrying when the query raises the exception
|
37
|
+
`ActiveRecord::RecordNotUnique`. This gem provides a method
|
38
|
+
`atomic_first_or_create` which does exactly that.
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
Finds the first record with the given attributes, or creates a record with the attributes if one is not found:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# Find the first user with uid "abc123" or create a new one.
|
46
|
+
User.where(uid: 'abc123').atomic_first_or_create(name: 'John', uid: 'abc123')
|
47
|
+
# => #<User id: 1, uid: "abc123", name: 'John'>
|
48
|
+
```
|
49
|
+
```ruby
|
50
|
+
# Find the first user with uid "abc123" or create a new one.
|
51
|
+
# We already have one so the existing record will be returned.
|
52
|
+
User.where(uid: 'abc123').atomic_first_or_create(name: 'John', uid: 'abc123')
|
53
|
+
# => #<User id: 1, uid: "abc123", name: 'John'>
|
54
|
+
```
|
55
|
+
```ruby
|
56
|
+
# Find the first user with uid "abc1234" or create a new one
|
57
|
+
User.where(uid: 'abc123').atomic_first_or_create(name: 'Johansson', uid: 'abc1234')
|
58
|
+
# => #<User id: 2, uid: "abc1234", name: 'Johansson'>
|
59
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'atomic_first_or_create/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "atomic_first_or_create"
|
8
|
+
spec.version = AtomicFirstOrCreate::VERSION
|
9
|
+
spec.authors = ["EdgePetrol"]
|
10
|
+
spec.email = ["devops@edgepetrol.com"]
|
11
|
+
spec.summary = %q{ActiveRecord first_or_create that retries on RecordNotUnique exception}
|
12
|
+
spec.description = %q{first_or_create does not guarantee uniqueness by itself, and if there is a uniqueness constraint on the database, it may fail with a RecordNotUnique exception. This gem adds atomic_first_or_create, which, in conjunction with a uniqueness constraint, provides the correct behaviour.}
|
13
|
+
spec.homepage = "https://edgepetrol.com"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", '>= 4'
|
22
|
+
|
23
|
+
spec.add_development_dependency "rake", '~> 13'
|
24
|
+
spec.add_development_dependency "minitest", '~> 5'
|
25
|
+
spec.add_development_dependency "sqlite3", '~> 1'
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "atomic_first_or_create/version"
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class Relation
|
5
|
+
def atomic_first_or_create(**opts)
|
6
|
+
# We do not want to retry forever to avoid getting stuck
|
7
|
+
|
8
|
+
tries = 2
|
9
|
+
begin
|
10
|
+
first_or_create **opts
|
11
|
+
rescue ActiveRecord::RecordNotUnique
|
12
|
+
tries -= 1
|
13
|
+
if tries.zero?
|
14
|
+
raise
|
15
|
+
end
|
16
|
+
|
17
|
+
retry
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/test/test_atomic.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'atomic_first_or_create'
|
3
|
+
|
4
|
+
class AtomicTest < MiniTest::Test
|
5
|
+
def teardown
|
6
|
+
Person.delete_all
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_creation
|
10
|
+
p = Person.where(first_name: 'Andy', last_name: 'Matt').atomic_first_or_create
|
11
|
+
|
12
|
+
assert_equal ['Andy', 'Matt'], [p.first_name, p.last_name]
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_return_existing
|
16
|
+
p1 = Person.where(first_name: 'Andy', last_name: 'Matt').atomic_first_or_create
|
17
|
+
p2 = Person.where(first_name: 'Andy', last_name: 'Matt').atomic_first_or_create
|
18
|
+
|
19
|
+
assert_equal p1, p2
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_creation_with_extra_args
|
23
|
+
p = Person.where(first_name: 'Andy', last_name: 'Matt').atomic_first_or_create age: 28
|
24
|
+
|
25
|
+
assert_equal 28, p.age
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_return_with_extra_args
|
29
|
+
Person.where(first_name: 'Andy', last_name: 'Matt').atomic_first_or_create age: 28
|
30
|
+
|
31
|
+
p2 = Person.where(first_name: 'Andy', last_name: 'Matt').atomic_first_or_create age: 52
|
32
|
+
|
33
|
+
assert_equal 28, p2.age
|
34
|
+
end
|
35
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
5
|
+
|
6
|
+
ActiveRecord::Schema.define do
|
7
|
+
self.verbose = false
|
8
|
+
|
9
|
+
create_table :people do |t|
|
10
|
+
t.string :first_name
|
11
|
+
t.string :last_name
|
12
|
+
t.integer :age
|
13
|
+
end
|
14
|
+
|
15
|
+
add_index :people, [:first_name, :last_name], unique: true
|
16
|
+
end
|
17
|
+
|
18
|
+
class Person < ActiveRecord::Base
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: atomic_first_or_create
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- EdgePetrol
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1'
|
69
|
+
description: first_or_create does not guarantee uniqueness by itself, and if there
|
70
|
+
is a uniqueness constraint on the database, it may fail with a RecordNotUnique exception.
|
71
|
+
This gem adds atomic_first_or_create, which, in conjunction with a uniqueness constraint,
|
72
|
+
provides the correct behaviour.
|
73
|
+
email:
|
74
|
+
- devops@edgepetrol.com
|
75
|
+
executables: []
|
76
|
+
extensions: []
|
77
|
+
extra_rdoc_files: []
|
78
|
+
files:
|
79
|
+
- ".gitignore"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- atomic_first_or_create.gemspec
|
85
|
+
- lib/atomic_first_or_create.rb
|
86
|
+
- lib/atomic_first_or_create/version.rb
|
87
|
+
- test/test_atomic.rb
|
88
|
+
- test/test_helper.rb
|
89
|
+
homepage: https://edgepetrol.com
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubygems_version: 3.1.4
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: ActiveRecord first_or_create that retries on RecordNotUnique exception
|
112
|
+
test_files:
|
113
|
+
- test/test_atomic.rb
|
114
|
+
- test/test_helper.rb
|