retry_on_duplicate 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -1
- data/lib/active_record_retry_on_duplicate/version.rb +3 -0
- data/lib/active_record_retry_on_duplicate.rb +44 -0
- data/lib/retry_on_duplicate.rb +2 -45
- data/retry_on_duplicate.gemspec +2 -2
- metadata +5 -4
- data/lib/retry_on_duplicate/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9e172427d70457df5a68084a9488ab7113d614f0bb5a93bf08547e3a7c0bc5f
|
4
|
+
data.tar.gz: c09485affd7e385a3633938576ca4874aef196114a147d36bc3f0f98de3ffea3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f7b6d17f789a00d2c80e13b8708d33c5953be1ee72e5e3c6020dd995fdc6f0dfa0f9b96af6655c33c656fb3495aaa8a5a899712e91648bc2501bb2f3effb231
|
7
|
+
data.tar.gz: 294cee68f75cc5a1bf8ed544ff01588b38fd9ab3e313d7da0ede76ebb4d2a9e6433aae659ddf19e91f451184af32ccf0c60ea60f5dc8afef74b391cc40c9c6e4
|
data/README.md
CHANGED
@@ -22,7 +22,27 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
add db:migrate for a unique constraint at the database level
|
26
|
+
|
27
|
+
```
|
28
|
+
add_index :users, :email, unique: true
|
29
|
+
```
|
30
|
+
|
31
|
+
How to prevent duplicate database record creation
|
32
|
+
|
33
|
+
```
|
34
|
+
User.retry_on_duplicate do
|
35
|
+
User.find_or_create_by(email: email)
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
|
40
|
+
## Problems Solved
|
41
|
+
|
42
|
+
- `ActiveRecord::RecordNotUnique: Mysql2::Error: Duplicate entry`
|
43
|
+
|
44
|
+
- `ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint `
|
45
|
+
|
26
46
|
|
27
47
|
## Development
|
28
48
|
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "active_record_retry_on_duplicate/version"
|
2
|
+
|
3
|
+
module ActiveRecordRetryOnDuplicate
|
4
|
+
def self.included(base)
|
5
|
+
base.send :extend, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def retry_on_duplicate
|
10
|
+
retried = false
|
11
|
+
begin
|
12
|
+
yield
|
13
|
+
rescue *retry_on_duplicate_rescue_messages => err
|
14
|
+
if retry_on_duplicate_on_retry?(err, retried)
|
15
|
+
retried = true
|
16
|
+
retry
|
17
|
+
else
|
18
|
+
raise err
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def retry_on_duplicate_rescue_messages
|
26
|
+
@retry_on_duplicate_rescue_messages ||=
|
27
|
+
[ ActiveRecord::RecordNotUnique ] | extra_retry_on_duplicate_rescue_messages
|
28
|
+
end
|
29
|
+
|
30
|
+
def extra_retry_on_duplicate_rescue_messages
|
31
|
+
arr = []
|
32
|
+
arr << PG::UniqueViolation if defined?(PG::UniqueViolation)
|
33
|
+
arr << Mysql2::Error if defined?(Mysql2::Error)
|
34
|
+
arr
|
35
|
+
end
|
36
|
+
|
37
|
+
def retry_on_duplicate_on_retry?(err, retried)
|
38
|
+
!retried &&
|
39
|
+
(err.message.include?("Duplicate entry") ||
|
40
|
+
err.message.include?("PG::UniqueViolation") ||
|
41
|
+
err.message.include?("has already been taken"))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/retry_on_duplicate.rb
CHANGED
@@ -1,49 +1,6 @@
|
|
1
1
|
require "active_support/lazy_load_hooks"
|
2
|
-
require "
|
3
|
-
|
4
|
-
module RetryOnDuplicate
|
5
|
-
def self.included(base)
|
6
|
-
base.send :extend, ClassMethods
|
7
|
-
end
|
8
|
-
|
9
|
-
module ClassMethods
|
10
|
-
def retry_on_duplicate
|
11
|
-
retried = false
|
12
|
-
begin
|
13
|
-
yield
|
14
|
-
rescue *retry_on_duplicate_rescue_messages => err
|
15
|
-
if retry_on_duplicate_on_retry?(err, retried)
|
16
|
-
retried = true
|
17
|
-
retry
|
18
|
-
else
|
19
|
-
raise err
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def retry_on_duplicate_rescue_messages
|
27
|
-
@retry_on_duplicate_rescue_messages ||=
|
28
|
-
[ ActiveRecord::RecordNotUnique ] | extra_retry_on_duplicate_rescue_messages
|
29
|
-
end
|
30
|
-
|
31
|
-
def extra_retry_on_duplicate_rescue_messages
|
32
|
-
arr = []
|
33
|
-
arr << PG::UniqueViolation if defined?(PG::UniqueViolation)
|
34
|
-
arr << Mysql2::Error if defined?(Mysql2::Error)
|
35
|
-
arr
|
36
|
-
end
|
37
|
-
|
38
|
-
def retry_on_duplicate_on_retry?(err, retried)
|
39
|
-
!retried &&
|
40
|
-
(err.message.include?("Duplicate entry") ||
|
41
|
-
err.message.include?("PG::UniqueViolation") ||
|
42
|
-
err.message.include?("has already been taken"))
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
2
|
+
require "active_record_retry_on_duplicate"
|
46
3
|
|
47
4
|
ActiveSupport.on_load(:active_record) do
|
48
|
-
ActiveRecord::Base.send :include,
|
5
|
+
ActiveRecord::Base.send :include, ActiveRecordRetryOnDuplicate
|
49
6
|
end
|
data/retry_on_duplicate.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
|
2
2
|
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "
|
4
|
+
require "active_record_retry_on_duplicate/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "retry_on_duplicate"
|
8
|
-
spec.version =
|
8
|
+
spec.version = ActiveRecordRetryOnDuplicate::VERSION
|
9
9
|
spec.authors = ["Grayson Chen"]
|
10
10
|
spec.email = ["cgg5207@sina.com"]
|
11
11
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: retry_on_duplicate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grayson Chen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,8 +68,9 @@ files:
|
|
68
68
|
- Rakefile
|
69
69
|
- bin/console
|
70
70
|
- bin/setup
|
71
|
+
- lib/active_record_retry_on_duplicate.rb
|
72
|
+
- lib/active_record_retry_on_duplicate/version.rb
|
71
73
|
- lib/retry_on_duplicate.rb
|
72
|
-
- lib/retry_on_duplicate/version.rb
|
73
74
|
- retry_on_duplicate.gemspec
|
74
75
|
homepage: https://github.com/graysonchen/retry_on_duplicate
|
75
76
|
licenses:
|
@@ -94,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
95
|
- !ruby/object:Gem::Version
|
95
96
|
version: '0'
|
96
97
|
requirements: []
|
97
|
-
rubygems_version: 3.0.
|
98
|
+
rubygems_version: 3.0.9
|
98
99
|
signing_key:
|
99
100
|
specification_version: 4
|
100
101
|
summary: Write a short summary, because RubyGems requires one.
|