roqua-support 0.1.7 → 0.1.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b4f2e07189d3ee1b5b4c0e6472bd3c1b4ba2c6a
4
- data.tar.gz: 3ae9698e6c4b77a16e7ff1226cb9269e3d3f428c
3
+ metadata.gz: d04050aedf6fdfafa5b9a9a6cabc22b11f57f215
4
+ data.tar.gz: 06c935c3e34b4d9b402d63717a4800d398d0d127
5
5
  SHA512:
6
- metadata.gz: 6ab8c7979cb7a686033f05b6a0a9e64e028a94a59a6a6186a5621826e91f044dcedb0075dfe337b38c32eb623a6f45d63ff64c0b38e52bfb65bbfded5b518281
7
- data.tar.gz: 86c8129d450c4e37237488eeef12ffc1766813b8d3b0d78762b2ec4531aec0c83cbaeb9cd4db439edaddd47cc8bd2dab90f75758797d275c050b2fddc02284e0
6
+ metadata.gz: e4d6e986f2ab7560d71eba134b8f2b2b5dc8407c7ce01fa59af20f6ca828b9646fe5551e370289e03efd0ce3f702ce9feff6df7e5f887905d695446031c8870b
7
+ data.tar.gz: 1a33aae58fd341f06e1695de7e1af81f8f170dde10fdd804fa48eff30235462279fdd3a6f3d97e629a620912fa5909363d9dbb57ef2d0b1ee675eb5199bfc8df
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.1.8 / 2014-06-17
2
+
3
+ * catch Mysql2 errors
1
4
  * remove dubious return from ensure in activerecord extensions
2
5
 
3
6
  ## 0.1.5 / 2014-03-10
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- roqua-support (0.1.7)
4
+ roqua-support (0.1.8)
5
5
  activesupport (>= 3.2, < 5.0)
6
6
 
7
7
  GEM
@@ -64,7 +64,7 @@ GEM
64
64
  ruby-progressbar (1.4.1)
65
65
  slop (3.4.7)
66
66
  thor (0.18.1)
67
- thread_safe (0.3.3)
67
+ thread_safe (0.3.4)
68
68
  timers (1.1.0)
69
69
  tzinfo (1.1.0)
70
70
  thread_safe (~> 0.1)
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.1.0
@@ -1,5 +1,5 @@
1
1
  module Roqua
2
2
  module Support
3
- VERSION = "0.1.7"
3
+ VERSION = "0.1.8"
4
4
  end
5
5
  end
@@ -9,6 +9,12 @@ module ActiveRecord
9
9
  # containing the validation errors is returned instead.
10
10
  def self.uniq_find_or_create_by(attributes, &block)
11
11
  find_or_create_by(attributes, &block)
12
+ # When a real race condition occurs, activerecord has no clue about a uniqueness constraint
13
+ # being violated (this is exactly why validates :attribute, uniqueness: true does not work
14
+ # for these cases) and a plain Mysql2::Error exception is raised instead of
15
+ # ActiveRecord::RecordNotUnique
16
+ rescue Mysql2::Error => exception
17
+ find_by(attributes) || raise(exception)
12
18
  rescue ActiveRecord::RecordNotUnique => exception
13
19
  find_by(attributes) || raise(exception)
14
20
  end
@@ -17,6 +23,12 @@ module ActiveRecord
17
23
  # error other than uniqueness.
18
24
  def self.uniq_find_or_create_by!(attributes, &block)
19
25
  find_or_create_by!(attributes, &block)
26
+ # When a real race condition occurs, activerecord has no clue about a uniqueness constraint
27
+ # being violated (this is exactly why validates :attribute, uniqueness: true does not work
28
+ # for these cases) and a plain Mysql2::Error exception is raised instead of
29
+ # ActiveRecord::RecordNotUnique
30
+ rescue Mysql2::Error => exception
31
+ find_by(attributes) || raise(exception)
20
32
  rescue ActiveRecord::RecordNotUnique => exception
21
33
  find_by(attributes) || raise(exception)
22
34
  rescue ActiveRecord::RecordInvalid => exception
@@ -13,9 +13,14 @@ module ActiveRecord
13
13
  end
14
14
  end
15
15
 
16
+ module Mysql2
17
+ class Error < StandardError
18
+ end
19
+ end
20
+
16
21
  describe ActiveRecord::Base do
17
22
  let(:attributes) { double('attributes') }
18
- let(:block) { -> {} }
23
+ let(:block) { -> (*args){} }
19
24
  let(:record) { double('record') }
20
25
 
21
26
  describe '#uniq_find_or_create_by' do
@@ -36,6 +41,13 @@ describe ActiveRecord::Base do
36
41
  expect(ActiveRecord::Base.uniq_find_or_create_by attributes, &block).to eq(record)
37
42
  end
38
43
 
44
+ it 'returns a concurrenlty created record when activerecord is unaware of the uniqueness violation' do
45
+ allow(ActiveRecord::Base).to receive(:find_by).with(attributes).and_return record
46
+ allow(ActiveRecord::Base).to receive(:find_or_create_by).with(attributes, &block)
47
+ .and_raise Mysql2::Error
48
+ expect(ActiveRecord::Base.uniq_find_or_create_by attributes, &block).to eq(record)
49
+ end
50
+
39
51
  it 'raises when a concurrent record is detected by the database but could not be queried for unknown reasons' do
40
52
  allow(ActiveRecord::Base).to receive(:find_by).with(attributes).and_return nil
41
53
  allow(ActiveRecord::Base).to receive(:find_or_create_by).with(attributes, &block)
@@ -70,6 +82,13 @@ describe ActiveRecord::Base do
70
82
  expect(ActiveRecord::Base.uniq_find_or_create_by! attributes, &block).to eq(record)
71
83
  end
72
84
 
85
+ it 'returns a concurrenlty created record when activerecord is unaware of the uniqueness violation' do
86
+ allow(ActiveRecord::Base).to receive(:find_by).with(attributes).and_return record
87
+ allow(ActiveRecord::Base).to receive(:find_or_create_by!).with(attributes, &block)
88
+ .and_raise Mysql2::Error
89
+ expect(ActiveRecord::Base.uniq_find_or_create_by! attributes, &block).to eq(record)
90
+ end
91
+
73
92
  it 'raises when a concurrent record is detected by the database but could not be queried for unknown reasons' do
74
93
  allow(ActiveRecord::Base).to receive(:find_by).with(attributes).and_return nil
75
94
  allow(ActiveRecord::Base).to receive(:find_or_create_by!).with(attributes, &block)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roqua-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Veldthuis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-21 00:00:00.000000000 Z
11
+ date: 2014-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -94,6 +94,7 @@ files:
94
94
  - LICENSE.txt
95
95
  - README.md
96
96
  - Rakefile
97
+ - circle.yml
97
98
  - lib/roqua-support.rb
98
99
  - lib/roqua-support/version.rb
99
100
  - lib/roqua/core_ext/activerecord/uniq_find_or_create.rb
@@ -137,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
138
  version: '0'
138
139
  requirements: []
139
140
  rubyforge_project:
140
- rubygems_version: 2.2.2
141
+ rubygems_version: 2.2.1
141
142
  signing_key:
142
143
  specification_version: 4
143
144
  summary: Helper objects and proxies used by a lot of RoQua applications