after_commit_everywhere 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/Gemfile.lock +99 -97
- data/README.md +69 -2
- data/after_commit_everywhere.gemspec +2 -0
- data/lib/after_commit_everywhere/version.rb +1 -1
- data/lib/after_commit_everywhere.rb +22 -2
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f406689608dbc8a4a3c904dffd3f090ad6e284f037a4316728e7cadd5ccdb9ed
|
4
|
+
data.tar.gz: 9d70381c05b482fee5f7f1f705bd263c9e43bdfac2d7558dff2ac82e98c9201e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28bc55a3725c4baaca95574bf0b3631a411466dbcdee4e7c148c4d00c30f0ecd8a476863c65497a2c81df85978aa0705838376cc02f0a40fd4ff9f79d062db1c
|
7
|
+
data.tar.gz: 3013886554bb909368399f258a0047f55b87a4e9420eb2e923569b4c8cddfca438fb6ca2c470ae109f93fe668318efdf4cd59e785d858754f7df9df032f1b211
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,30 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## 1.3.0 (2022-10-28)
|
8
|
+
|
9
|
+
### Added
|
10
|
+
|
11
|
+
- `in_transaction` helper method to execute code within existing transaction or start a new one if there is no tx open.
|
12
|
+
|
13
|
+
It is similar to `ActiveRecord::Base.transaction`, but it doesn't swallow `ActiveRecord::Rollback` exception in case when there is no transaction open.
|
14
|
+
|
15
|
+
See discussion at [#23](https://github.com/Envek/after_commit_everywhere/pull/23) for details.
|
16
|
+
|
17
|
+
[Pull request #23](https://github.com/Envek/after_commit_everywhere/pull/23) by [@jpcamara][].
|
18
|
+
|
19
|
+
- Ability to call `in_transaction` helper with the same arguments as [`ActiveRecord::Base.transaction`](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-transaction). [@Envek][].
|
20
|
+
|
21
|
+
## 1.2.2 (2022-06-20)
|
22
|
+
|
23
|
+
### Fixed
|
24
|
+
|
25
|
+
- Connection leak from the connection pool when `after_commit` called outside Rails executor without connection checked out *and* some connections were already checked out from another threads.
|
26
|
+
|
27
|
+
See discussion at [issue #20](https://github.com/Envek/after_commit_everywhere/issues/20) for details.
|
28
|
+
|
29
|
+
[Pull request #22](https://github.com/Envek/after_commit_everywhere/pull/22) by [@Envek][].
|
30
|
+
|
7
31
|
## 1.2.1 (2022-06-10)
|
8
32
|
|
9
33
|
### Fixed
|
@@ -89,3 +113,4 @@ See [#11](https://github.com/Envek/after_commit_everywhere/issues/11) for discus
|
|
89
113
|
[@joevandyk]: https://github.com/joevandyk "Joe Van Dyk"
|
90
114
|
[@stokarenko]: https://github.com/stokarenko "Sergey Tokarenko"
|
91
115
|
[@lolripgg]: https://github.com/lolripgg "James Brewer"
|
116
|
+
[@jpcamara]: https://github.com/jpcamara "JP Camara"
|
data/Gemfile.lock
CHANGED
@@ -1,74 +1,74 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
after_commit_everywhere (1.
|
4
|
+
after_commit_everywhere (1.3.0)
|
5
5
|
activerecord (>= 4.2)
|
6
6
|
activesupport
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
actioncable (7.0.
|
12
|
-
actionpack (= 7.0.
|
13
|
-
activesupport (= 7.0.
|
11
|
+
actioncable (7.0.4)
|
12
|
+
actionpack (= 7.0.4)
|
13
|
+
activesupport (= 7.0.4)
|
14
14
|
nio4r (~> 2.0)
|
15
15
|
websocket-driver (>= 0.6.1)
|
16
|
-
actionmailbox (7.0.
|
17
|
-
actionpack (= 7.0.
|
18
|
-
activejob (= 7.0.
|
19
|
-
activerecord (= 7.0.
|
20
|
-
activestorage (= 7.0.
|
21
|
-
activesupport (= 7.0.
|
16
|
+
actionmailbox (7.0.4)
|
17
|
+
actionpack (= 7.0.4)
|
18
|
+
activejob (= 7.0.4)
|
19
|
+
activerecord (= 7.0.4)
|
20
|
+
activestorage (= 7.0.4)
|
21
|
+
activesupport (= 7.0.4)
|
22
22
|
mail (>= 2.7.1)
|
23
23
|
net-imap
|
24
24
|
net-pop
|
25
25
|
net-smtp
|
26
|
-
actionmailer (7.0.
|
27
|
-
actionpack (= 7.0.
|
28
|
-
actionview (= 7.0.
|
29
|
-
activejob (= 7.0.
|
30
|
-
activesupport (= 7.0.
|
26
|
+
actionmailer (7.0.4)
|
27
|
+
actionpack (= 7.0.4)
|
28
|
+
actionview (= 7.0.4)
|
29
|
+
activejob (= 7.0.4)
|
30
|
+
activesupport (= 7.0.4)
|
31
31
|
mail (~> 2.5, >= 2.5.4)
|
32
32
|
net-imap
|
33
33
|
net-pop
|
34
34
|
net-smtp
|
35
35
|
rails-dom-testing (~> 2.0)
|
36
|
-
actionpack (7.0.
|
37
|
-
actionview (= 7.0.
|
38
|
-
activesupport (= 7.0.
|
36
|
+
actionpack (7.0.4)
|
37
|
+
actionview (= 7.0.4)
|
38
|
+
activesupport (= 7.0.4)
|
39
39
|
rack (~> 2.0, >= 2.2.0)
|
40
40
|
rack-test (>= 0.6.3)
|
41
41
|
rails-dom-testing (~> 2.0)
|
42
42
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
43
|
-
actiontext (7.0.
|
44
|
-
actionpack (= 7.0.
|
45
|
-
activerecord (= 7.0.
|
46
|
-
activestorage (= 7.0.
|
47
|
-
activesupport (= 7.0.
|
43
|
+
actiontext (7.0.4)
|
44
|
+
actionpack (= 7.0.4)
|
45
|
+
activerecord (= 7.0.4)
|
46
|
+
activestorage (= 7.0.4)
|
47
|
+
activesupport (= 7.0.4)
|
48
48
|
globalid (>= 0.6.0)
|
49
49
|
nokogiri (>= 1.8.5)
|
50
|
-
actionview (7.0.
|
51
|
-
activesupport (= 7.0.
|
50
|
+
actionview (7.0.4)
|
51
|
+
activesupport (= 7.0.4)
|
52
52
|
builder (~> 3.1)
|
53
53
|
erubi (~> 1.4)
|
54
54
|
rails-dom-testing (~> 2.0)
|
55
55
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
56
|
-
activejob (7.0.
|
57
|
-
activesupport (= 7.0.
|
56
|
+
activejob (7.0.4)
|
57
|
+
activesupport (= 7.0.4)
|
58
58
|
globalid (>= 0.3.6)
|
59
|
-
activemodel (7.0.
|
60
|
-
activesupport (= 7.0.
|
61
|
-
activerecord (7.0.
|
62
|
-
activemodel (= 7.0.
|
63
|
-
activesupport (= 7.0.
|
64
|
-
activestorage (7.0.
|
65
|
-
actionpack (= 7.0.
|
66
|
-
activejob (= 7.0.
|
67
|
-
activerecord (= 7.0.
|
68
|
-
activesupport (= 7.0.
|
59
|
+
activemodel (7.0.4)
|
60
|
+
activesupport (= 7.0.4)
|
61
|
+
activerecord (7.0.4)
|
62
|
+
activemodel (= 7.0.4)
|
63
|
+
activesupport (= 7.0.4)
|
64
|
+
activestorage (7.0.4)
|
65
|
+
actionpack (= 7.0.4)
|
66
|
+
activejob (= 7.0.4)
|
67
|
+
activerecord (= 7.0.4)
|
68
|
+
activesupport (= 7.0.4)
|
69
69
|
marcel (~> 1.0)
|
70
70
|
mini_mime (>= 1.1.0)
|
71
|
-
activesupport (7.0.
|
71
|
+
activesupport (7.0.4)
|
72
72
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
73
73
|
i18n (>= 1.6, < 2)
|
74
74
|
minitest (>= 5.1)
|
@@ -81,21 +81,21 @@ GEM
|
|
81
81
|
thor (>= 0.14.0)
|
82
82
|
ast (2.4.2)
|
83
83
|
builder (3.2.4)
|
84
|
+
byebug (11.1.3)
|
84
85
|
coderay (1.1.3)
|
85
86
|
concurrent-ruby (1.1.10)
|
86
87
|
crass (1.0.6)
|
87
88
|
diff-lcs (1.5.0)
|
88
|
-
digest (3.1.0)
|
89
89
|
dry-initializer (3.1.1)
|
90
|
-
erubi (1.
|
90
|
+
erubi (1.11.0)
|
91
91
|
globalid (1.0.0)
|
92
92
|
activesupport (>= 5.0)
|
93
|
-
i18n (1.
|
93
|
+
i18n (1.12.0)
|
94
94
|
concurrent-ruby (~> 1.0)
|
95
95
|
isolator (0.8.0)
|
96
96
|
sniffer (>= 0.3.1)
|
97
97
|
jaro_winkler (1.5.4)
|
98
|
-
loofah (2.
|
98
|
+
loofah (2.19.0)
|
99
99
|
crass (~> 1.0.2)
|
100
100
|
nokogiri (>= 1.5.9)
|
101
101
|
mail (2.7.1)
|
@@ -104,57 +104,54 @@ GEM
|
|
104
104
|
method_source (1.0.0)
|
105
105
|
mini_mime (1.1.2)
|
106
106
|
mini_portile2 (2.8.0)
|
107
|
-
minitest (5.
|
108
|
-
net-imap (0.
|
109
|
-
digest
|
107
|
+
minitest (5.16.3)
|
108
|
+
net-imap (0.3.1)
|
110
109
|
net-protocol
|
111
|
-
|
112
|
-
net-pop (0.1.1)
|
113
|
-
digest
|
110
|
+
net-pop (0.1.2)
|
114
111
|
net-protocol
|
115
|
-
timeout
|
116
112
|
net-protocol (0.1.3)
|
117
113
|
timeout
|
118
|
-
net-smtp (0.3.
|
119
|
-
digest
|
114
|
+
net-smtp (0.3.2)
|
120
115
|
net-protocol
|
121
|
-
timeout
|
122
116
|
nio4r (2.5.8)
|
123
|
-
nokogiri (1.13.
|
117
|
+
nokogiri (1.13.9)
|
124
118
|
mini_portile2 (~> 2.8.0)
|
125
119
|
racc (~> 1.4)
|
126
120
|
parallel (1.22.1)
|
127
|
-
parser (3.1.2.
|
121
|
+
parser (3.1.2.1)
|
128
122
|
ast (~> 2.4.1)
|
129
123
|
pry (0.14.1)
|
130
124
|
coderay (~> 1.1)
|
131
125
|
method_source (~> 1.0)
|
126
|
+
pry-byebug (3.10.1)
|
127
|
+
byebug (~> 11.0)
|
128
|
+
pry (>= 0.13, < 0.15)
|
132
129
|
racc (1.6.0)
|
133
130
|
rack (2.2.3.1)
|
134
|
-
rack-test (
|
135
|
-
rack (>= 1.
|
136
|
-
rails (7.0.
|
137
|
-
actioncable (= 7.0.
|
138
|
-
actionmailbox (= 7.0.
|
139
|
-
actionmailer (= 7.0.
|
140
|
-
actionpack (= 7.0.
|
141
|
-
actiontext (= 7.0.
|
142
|
-
actionview (= 7.0.
|
143
|
-
activejob (= 7.0.
|
144
|
-
activemodel (= 7.0.
|
145
|
-
activerecord (= 7.0.
|
146
|
-
activestorage (= 7.0.
|
147
|
-
activesupport (= 7.0.
|
131
|
+
rack-test (2.0.2)
|
132
|
+
rack (>= 1.3)
|
133
|
+
rails (7.0.4)
|
134
|
+
actioncable (= 7.0.4)
|
135
|
+
actionmailbox (= 7.0.4)
|
136
|
+
actionmailer (= 7.0.4)
|
137
|
+
actionpack (= 7.0.4)
|
138
|
+
actiontext (= 7.0.4)
|
139
|
+
actionview (= 7.0.4)
|
140
|
+
activejob (= 7.0.4)
|
141
|
+
activemodel (= 7.0.4)
|
142
|
+
activerecord (= 7.0.4)
|
143
|
+
activestorage (= 7.0.4)
|
144
|
+
activesupport (= 7.0.4)
|
148
145
|
bundler (>= 1.15.0)
|
149
|
-
railties (= 7.0.
|
146
|
+
railties (= 7.0.4)
|
150
147
|
rails-dom-testing (2.0.3)
|
151
148
|
activesupport (>= 4.2.0)
|
152
149
|
nokogiri (>= 1.6)
|
153
150
|
rails-html-sanitizer (1.4.3)
|
154
151
|
loofah (~> 2.3)
|
155
|
-
railties (7.0.
|
156
|
-
actionpack (= 7.0.
|
157
|
-
activesupport (= 7.0.
|
152
|
+
railties (7.0.4)
|
153
|
+
actionpack (= 7.0.4)
|
154
|
+
activesupport (= 7.0.4)
|
158
155
|
method_source
|
159
156
|
rake (>= 12.2)
|
160
157
|
thor (~> 1.0)
|
@@ -162,27 +159,27 @@ GEM
|
|
162
159
|
rainbow (3.1.1)
|
163
160
|
rake (13.0.6)
|
164
161
|
rexml (3.2.5)
|
165
|
-
rspec (3.
|
166
|
-
rspec-core (~> 3.
|
167
|
-
rspec-expectations (~> 3.
|
168
|
-
rspec-mocks (~> 3.
|
169
|
-
rspec-core (3.
|
170
|
-
rspec-support (~> 3.
|
171
|
-
rspec-expectations (3.
|
162
|
+
rspec (3.12.0)
|
163
|
+
rspec-core (~> 3.12.0)
|
164
|
+
rspec-expectations (~> 3.12.0)
|
165
|
+
rspec-mocks (~> 3.12.0)
|
166
|
+
rspec-core (3.12.0)
|
167
|
+
rspec-support (~> 3.12.0)
|
168
|
+
rspec-expectations (3.12.0)
|
172
169
|
diff-lcs (>= 1.2.0, < 2.0)
|
173
|
-
rspec-support (~> 3.
|
174
|
-
rspec-mocks (3.
|
170
|
+
rspec-support (~> 3.12.0)
|
171
|
+
rspec-mocks (3.12.0)
|
175
172
|
diff-lcs (>= 1.2.0, < 2.0)
|
176
|
-
rspec-support (~> 3.
|
177
|
-
rspec-rails (
|
178
|
-
actionpack (>=
|
179
|
-
activesupport (>=
|
180
|
-
railties (>=
|
181
|
-
rspec-core (~> 3.
|
182
|
-
rspec-expectations (~> 3.
|
183
|
-
rspec-mocks (~> 3.
|
184
|
-
rspec-support (~> 3.
|
185
|
-
rspec-support (3.
|
173
|
+
rspec-support (~> 3.12.0)
|
174
|
+
rspec-rails (6.0.1)
|
175
|
+
actionpack (>= 6.1)
|
176
|
+
activesupport (>= 6.1)
|
177
|
+
railties (>= 6.1)
|
178
|
+
rspec-core (~> 3.11)
|
179
|
+
rspec-expectations (~> 3.11)
|
180
|
+
rspec-mocks (~> 3.11)
|
181
|
+
rspec-support (~> 3.11)
|
182
|
+
rspec-support (3.12.0)
|
186
183
|
rubocop (0.81.0)
|
187
184
|
jaro_winkler (~> 1.5.1)
|
188
185
|
parallel (~> 1.10)
|
@@ -191,22 +188,25 @@ GEM
|
|
191
188
|
rexml
|
192
189
|
ruby-progressbar (~> 1.7)
|
193
190
|
unicode-display_width (>= 1.4.0, < 2.0)
|
194
|
-
ruby-next-core (0.15.
|
191
|
+
ruby-next-core (0.15.3)
|
195
192
|
ruby-progressbar (1.11.0)
|
196
193
|
sniffer (0.5.0)
|
197
194
|
anyway_config (>= 1.0)
|
198
195
|
dry-initializer (~> 3)
|
199
|
-
sqlite3 (1.
|
200
|
-
|
196
|
+
sqlite3 (1.5.3)
|
197
|
+
mini_portile2 (~> 2.8.0)
|
201
198
|
thor (1.2.1)
|
202
199
|
timeout (0.3.0)
|
203
|
-
tzinfo (2.0.
|
200
|
+
tzinfo (2.0.5)
|
204
201
|
concurrent-ruby (~> 1.0)
|
205
202
|
unicode-display_width (1.8.0)
|
203
|
+
webrick (1.7.0)
|
206
204
|
websocket-driver (0.7.5)
|
207
205
|
websocket-extensions (>= 0.1.0)
|
208
206
|
websocket-extensions (0.1.5)
|
209
|
-
|
207
|
+
yard (0.9.28)
|
208
|
+
webrick (~> 1.7.0)
|
209
|
+
zeitwerk (2.6.1)
|
210
210
|
|
211
211
|
PLATFORMS
|
212
212
|
ruby
|
@@ -217,12 +217,14 @@ DEPENDENCIES
|
|
217
217
|
bundler (~> 2.0)
|
218
218
|
isolator (~> 0.7)
|
219
219
|
pry
|
220
|
+
pry-byebug
|
220
221
|
rails
|
221
222
|
rake (~> 13.0)
|
222
223
|
rspec (~> 3.0)
|
223
224
|
rspec-rails
|
224
225
|
rubocop (~> 0.81.0)
|
225
226
|
sqlite3 (~> 1.3, >= 1.3.6)
|
227
|
+
yard
|
226
228
|
|
227
229
|
BUNDLED WITH
|
228
|
-
2.3.
|
230
|
+
2.3.16
|
data/README.md
CHANGED
@@ -111,13 +111,80 @@ Will be executed right after transaction in which it have been declared was roll
|
|
111
111
|
|
112
112
|
If called outside transaction will raise an exception!
|
113
113
|
|
114
|
-
Please keep in mind ActiveRecord's [limitations for rolling back nested transactions](http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#module-ActiveRecord::Transactions::ClassMethods-label-Nested+transactions).
|
114
|
+
Please keep in mind ActiveRecord's [limitations for rolling back nested transactions](http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#module-ActiveRecord::Transactions::ClassMethods-label-Nested+transactions). See [`in_transaction`](#in_transaction) for a workaround to this limitation.
|
115
115
|
|
116
116
|
### Available helper methods
|
117
117
|
|
118
|
+
#### `in_transaction`
|
119
|
+
|
120
|
+
Makes sure the provided block is running in a transaction.
|
121
|
+
|
122
|
+
This method aims to provide clearer intention than a typical `ActiveRecord::Base.transaction` block - `in_transaction` only cares that _some_ transaction is present, not that a transaction is nested in any way.
|
123
|
+
|
124
|
+
If a transaction is present, it will yield without taking any action. Note that this means `ActiveRecord::Rollback` errors will not be trapped by `in_transaction` but will propagate up to the nearest parent transaction block.
|
125
|
+
|
126
|
+
If no transaction is present, the provided block will open a new transaction.
|
127
|
+
|
128
|
+
```rb
|
129
|
+
class ServiceObjectBtw
|
130
|
+
include AfterCommitEverywhere
|
131
|
+
|
132
|
+
def call
|
133
|
+
in_transaction do
|
134
|
+
an_update
|
135
|
+
another_update
|
136
|
+
after_commit { puts "We're all done!" }
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
```
|
141
|
+
|
142
|
+
Our service object can run its database operations safely when run in isolation.
|
143
|
+
|
144
|
+
```rb
|
145
|
+
ServiceObjectBtw.new.call # This opens a new #transaction block
|
146
|
+
```
|
147
|
+
|
148
|
+
If it is later called from code already wrapped in a transaction, the existing transaction will be utilized without any nesting:
|
149
|
+
|
150
|
+
```rb
|
151
|
+
ActiveRecord::Base.transaction do
|
152
|
+
new_update
|
153
|
+
next_update
|
154
|
+
# This no longer opens a new #transaction block, because one is already present
|
155
|
+
ServiceObjectBtw.new.call
|
156
|
+
end
|
157
|
+
```
|
158
|
+
|
159
|
+
This can be called directly on the module as well:
|
160
|
+
|
161
|
+
```rb
|
162
|
+
AfterCommitEverywhere.in_transaction do
|
163
|
+
AfterCommitEverywhere.after_commit { puts "We're all done!" }
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
118
167
|
#### `in_transaction?`
|
119
168
|
|
120
|
-
Returns `true` when called inside open transaction, `false` otherwise.
|
169
|
+
Returns `true` when called inside an open transaction, `false` otherwise.
|
170
|
+
|
171
|
+
```rb
|
172
|
+
def check_for_transaction
|
173
|
+
if in_transaction?
|
174
|
+
puts "We're in a transaction!"
|
175
|
+
else
|
176
|
+
puts "We're not in a transaction..."
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
check_for_transaction
|
181
|
+
# => prints "We're not in a transaction..."
|
182
|
+
|
183
|
+
in_transaction do
|
184
|
+
check_for_transaction
|
185
|
+
end
|
186
|
+
# => prints "We're in a transaction!"
|
187
|
+
```
|
121
188
|
|
122
189
|
### Available callback options
|
123
190
|
|
@@ -34,10 +34,12 @@ Gem::Specification.new do |spec|
|
|
34
34
|
spec.add_development_dependency "bundler", "~> 2.0"
|
35
35
|
spec.add_development_dependency "isolator", "~> 0.7"
|
36
36
|
spec.add_development_dependency "pry"
|
37
|
+
spec.add_development_dependency "pry-byebug"
|
37
38
|
spec.add_development_dependency "rails"
|
38
39
|
spec.add_development_dependency "rake", "~> 13.0"
|
39
40
|
spec.add_development_dependency "rspec", "~> 3.0"
|
40
41
|
spec.add_development_dependency "rspec-rails"
|
41
42
|
spec.add_development_dependency "rubocop", "~> 0.81.0"
|
42
43
|
spec.add_development_dependency "sqlite3", "~> 1.3", ">= 1.3.6"
|
44
|
+
spec.add_development_dependency "yard"
|
43
45
|
end
|
@@ -14,7 +14,7 @@ module AfterCommitEverywhere
|
|
14
14
|
class NotInTransaction < RuntimeError; end
|
15
15
|
|
16
16
|
delegate :after_commit, :before_commit, :after_rollback, to: AfterCommitEverywhere
|
17
|
-
delegate :in_transaction?, to: AfterCommitEverywhere
|
17
|
+
delegate :in_transaction?, :in_transaction, to: AfterCommitEverywhere
|
18
18
|
|
19
19
|
# Causes {before_commit} and {after_commit} to raise an exception when
|
20
20
|
# called outside a transaction.
|
@@ -125,13 +125,33 @@ module AfterCommitEverywhere
|
|
125
125
|
# Helper method to determine whether we're currently in transaction or not
|
126
126
|
def in_transaction?(connection = nil)
|
127
127
|
# Don't establish new connection if not connected: we apparently not in transaction
|
128
|
-
return false unless connection || ActiveRecord::Base.connection_pool.
|
128
|
+
return false unless connection || ActiveRecord::Base.connection_pool.active_connection?
|
129
129
|
|
130
130
|
connection ||= default_connection
|
131
131
|
# service transactions (tests and database_cleaner) are not joinable
|
132
132
|
connection.transaction_open? && connection.current_transaction.joinable?
|
133
133
|
end
|
134
134
|
|
135
|
+
# Makes sure the provided block runs in a transaction. If we are not currently in a transaction, a new transaction is started.
|
136
|
+
#
|
137
|
+
# It mimics the ActiveRecord's +transaction+ method's API and actually uses it under the hood.
|
138
|
+
#
|
139
|
+
# However, the main difference is that it doesn't swallow +ActiveRecord::Rollback+ exception in case when there is no transaction open.
|
140
|
+
#
|
141
|
+
# @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-transaction
|
142
|
+
#
|
143
|
+
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection to operate in. Defaults to +ActiveRecord::Base.connection+
|
144
|
+
# @param requires_new [Boolean] Forces creation of new subtransaction (savepoint) even if transaction is already opened.
|
145
|
+
# @param new_tx_options [Hash<Symbol, void>] Options to be passed to +connection.transaction+ on new transaction creation
|
146
|
+
# @return void
|
147
|
+
def in_transaction(connection = default_connection, requires_new: false, **new_tx_options)
|
148
|
+
if in_transaction?(connection) && !requires_new
|
149
|
+
yield
|
150
|
+
else
|
151
|
+
connection.transaction(requires_new: requires_new, **new_tx_options) { yield }
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
135
155
|
private
|
136
156
|
|
137
157
|
def default_connection
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: after_commit_everywhere
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Novikov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry-byebug
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: rails
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -184,6 +198,20 @@ dependencies:
|
|
184
198
|
- - ">="
|
185
199
|
- !ruby/object:Gem::Version
|
186
200
|
version: 1.3.6
|
201
|
+
- !ruby/object:Gem::Dependency
|
202
|
+
name: yard
|
203
|
+
requirement: !ruby/object:Gem::Requirement
|
204
|
+
requirements:
|
205
|
+
- - ">="
|
206
|
+
- !ruby/object:Gem::Version
|
207
|
+
version: '0'
|
208
|
+
type: :development
|
209
|
+
prerelease: false
|
210
|
+
version_requirements: !ruby/object:Gem::Requirement
|
211
|
+
requirements:
|
212
|
+
- - ">="
|
213
|
+
- !ruby/object:Gem::Version
|
214
|
+
version: '0'
|
187
215
|
description: Brings before_commit, after_commit, and after_rollback transactional
|
188
216
|
callbacks outside of your ActiveRecord models.
|
189
217
|
email:
|