tp_record_optimistic 0.2.0 → 0.2.2
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.
- checksums.yaml +4 -4
- data/README.rdoc +18 -5
- data/config/initializers/inflections.rb +1 -0
- data/lib/tp_record_optimistic/version.rb +1 -1
- data/lib/tp_record_optimistic.rb +16 -6
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e3a91e2f76f9fc5def099fb8a6112397ec969d0
|
4
|
+
data.tar.gz: 65c9294bc994f082bbc58e1e53eed20f39193486
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 023f3eb1706c52918c8e5dd257d216c7f280756cf1002ba4d7d29b3da633f81a5bc6d6aa234e2b3b176f296b624b6d08f345d94c260d3b064d3643a66d048e52
|
7
|
+
data.tar.gz: 8b78f5bc293a870caeb1b9c166e3f528c7358799376e113789d76698c6a74a4536e583e4e7fca14ba82c3d9bf0132f60583c4512e1f118ebb90e7060c324d184
|
data/README.rdoc
CHANGED
@@ -1,20 +1,33 @@
|
|
1
|
-
=
|
1
|
+
= TP optimistic lock
|
2
2
|
|
3
|
-
This gem
|
3
|
+
This gem extends ActiveRecord to handle ActiveRecord::RecordNotUnique with an optimistic behavior.
|
4
|
+
If a uniqueness violation is triggered by the database, it will be returned through the `errors` method.
|
5
|
+
|
6
|
+
The purpose is to be more performative and assertive than `validates_uniqueness_of`, a Rails pessimistic instruction for handling uniqueness.
|
7
|
+
|
8
|
+
`validates_uniqueness_of` performs a select query for each new insert to check unicity.
|
9
|
+
`validates_uniqueness_of` doesn't work very well in an environment with high concurrency. The select and insert are atomic operations, so some attempts of insert the same record twice happens witha high frequency.
|
10
|
+
|
11
|
+
|
12
|
+
example:
|
13
|
+
```
|
14
|
+
class Foo < ActiveRecord::Base
|
15
|
+
acts_as_unique #
|
16
|
+
end
|
4
17
|
|
5
|
-
exemplo:
|
6
18
|
|
7
19
|
def sample
|
8
20
|
Bar.create(uuid: '5ea880de-e4ce-4770-8d10-c89bac181e40', other: 'bla bla')
|
9
21
|
bar = Bar.create(uuid: '5ea880de-e4ce-4770-8d10-c89bac181e40', other: 'bla bla')
|
10
22
|
|
11
|
-
#UNIQUE constraint failed
|
23
|
+
#error returned: UNIQUE constraint failed
|
12
24
|
bar.errors
|
13
25
|
end
|
14
26
|
|
15
27
|
|
16
28
|
def other_sample
|
17
29
|
Bar.create!(uuid: '5ea880de-e4ce-4770-8d10-c89bac181e40', other: 'bla bla')
|
18
|
-
|
30
|
+
|
31
|
+
#error raised: ActiveRecord::RecordInvalid UNIQUE constraint failed
|
19
32
|
Bar.create!(uuid: '5ea880de-e4ce-4770-8d10-c89bac181e40', other: 'bla bla')
|
20
33
|
end
|
data/lib/tp_record_optimistic.rb
CHANGED
@@ -1,9 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'active_record' unless defined? ActiveRecord
|
3
|
-
|
3
|
+
|
4
|
+
module SQLErrorParse
|
5
|
+
def unique_errors(error)
|
6
|
+
table = self.class.table_name
|
7
|
+
split_key = /(INSERT|UPDATE)/
|
8
|
+
especific_key = %w(UNIQUE constraint failed: SQLite3::ConstraintException:)
|
9
|
+
error.split(split_key)[0].split
|
10
|
+
.reject { |e| especific_key.include? e }
|
11
|
+
.map { |e| e.sub("#{table}.", '').delete(':') }
|
12
|
+
.map(&:to_sym)
|
13
|
+
end
|
14
|
+
end
|
4
15
|
|
5
16
|
module TPRecordOptimistic
|
6
17
|
extend ActiveSupport::Concern
|
18
|
+
include SQLErrorParse
|
7
19
|
|
8
20
|
def save(*args)
|
9
21
|
super(*args)
|
@@ -14,7 +26,9 @@ module TPRecordOptimistic
|
|
14
26
|
def save_optimistic(*args)
|
15
27
|
old_save(*args)
|
16
28
|
rescue ActiveRecord::RecordNotUnique => e
|
17
|
-
|
29
|
+
unique_errors(e.to_s).each do |field|
|
30
|
+
errors.add(field, :Unique, message: "UNIQUE constraint failed #{field}")
|
31
|
+
end
|
18
32
|
return false
|
19
33
|
end
|
20
34
|
|
@@ -30,10 +44,6 @@ module TPRecordOptimistic
|
|
30
44
|
errors.add('all', e.to_s)
|
31
45
|
raise ActiveRecord::RecordInvalid, self
|
32
46
|
end
|
33
|
-
|
34
|
-
# def optimistic_unique
|
35
|
-
# alias_method :save, :save_optimistic
|
36
|
-
# end
|
37
47
|
end
|
38
48
|
|
39
49
|
ActiveSupport.on_load(:active_record) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tp_record_optimistic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabricio Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -180,7 +180,7 @@ dependencies:
|
|
180
180
|
version: '2.7'
|
181
181
|
description:
|
182
182
|
email:
|
183
|
-
-
|
183
|
+
- fabricioque@gmail.com
|
184
184
|
executables: []
|
185
185
|
extensions: []
|
186
186
|
extra_rdoc_files: []
|
@@ -194,7 +194,7 @@ files:
|
|
194
194
|
- lib/tp_record_optimistic.rb
|
195
195
|
- lib/tp_record_optimistic/engine.rb
|
196
196
|
- lib/tp_record_optimistic/version.rb
|
197
|
-
homepage: https://github.com/fabricio-oliveira/
|
197
|
+
homepage: https://github.com/fabricio-oliveira/tp_optimistic_lock
|
198
198
|
licenses:
|
199
199
|
- MIT
|
200
200
|
metadata: {}
|
@@ -217,5 +217,5 @@ rubyforge_project:
|
|
217
217
|
rubygems_version: 2.5.2
|
218
218
|
signing_key:
|
219
219
|
specification_version: 4
|
220
|
-
summary: gem
|
220
|
+
summary: gem that implement optimistic lock using unicity restriction from database
|
221
221
|
test_files: []
|