mongoid_versioned_atomic 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mongoid_versioned_atomic/v_atomic.rb +49 -31
- data/lib/mongoid_versioned_atomic/version.rb +1 -1
- data/test/dummy/app/models/user.rb +2 -1
- data/test/dummy/log/test.log +4206 -0
- data/test/v_atomic_test.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d83eb64a3ce02734d1d23bb0a68774ac009088a1
|
4
|
+
data.tar.gz: 12d3ddf6e8b9c379191b86d7505c354aca79e0fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2e65e4bc80c43b1b3683a9390cbc52a30f9f60fa793c875c63ee2b425da4f0d33cc670619b8f9fc37b17fa99d48ad0434bbb46e68aba73e55931c79bd7fa079
|
7
|
+
data.tar.gz: 7b8dbd095df5136ccb0ee7ab7abae9905fd961edd2ffe35ae2957a321e7c5f3668d01bc5b62533c448c0ce524a5d874b90ad55f261ae712e4e64ffcbb0093ffd
|
@@ -1,4 +1,8 @@
|
|
1
1
|
module MongoidVersionedAtomic
|
2
|
+
|
3
|
+
class DbUnchanged < StandardError
|
4
|
+
end
|
5
|
+
|
2
6
|
module VAtomic
|
3
7
|
|
4
8
|
extend ActiveSupport::Concern
|
@@ -10,6 +14,10 @@ module MongoidVersionedAtomic
|
|
10
14
|
attr_accessor :modified_count
|
11
15
|
attr_accessor :upserted_id
|
12
16
|
#before_save :filter_fields
|
17
|
+
after_create :check_upserted_id
|
18
|
+
after_update :check_modified_count
|
19
|
+
|
20
|
+
|
13
21
|
end
|
14
22
|
|
15
23
|
def self.included(base)
|
@@ -156,6 +164,15 @@ module MongoidVersionedAtomic
|
|
156
164
|
|
157
165
|
end
|
158
166
|
|
167
|
+
## after create callback , ensures that callback chain is halted if nothing was created.
|
168
|
+
def check_upserted_id
|
169
|
+
raise DbUnchanged if !self.upserted_id
|
170
|
+
end
|
171
|
+
|
172
|
+
## after update callback ensures that callback chain is halted if nothing was modified.
|
173
|
+
def check_modified_count
|
174
|
+
raise DbUnchanged if !self.modified_count == 1
|
175
|
+
end
|
159
176
|
|
160
177
|
## @param query[Hash] : optional query hash.
|
161
178
|
## @param log[Boolean] : defaults to false, set true if you want to print out the final command sent to mongo.
|
@@ -198,44 +215,42 @@ module MongoidVersionedAtomic
|
|
198
215
|
|
199
216
|
expected_version = 1
|
200
217
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
218
|
+
begin
|
219
|
+
prepare_insert(options) do
|
220
|
+
|
221
|
+
as_document.keys.each do |k|
|
222
|
+
if (k != "version" && k != "op_success")
|
223
|
+
update["$setOnInsert"][k] = self.send(k.to_sym)
|
224
|
+
end
|
225
|
+
end
|
208
226
|
|
209
|
-
|
227
|
+
update["$setOnInsert"]["version"] = 1
|
210
228
|
|
211
|
-
|
229
|
+
options,update = self.class.before_persist(options,update,true)
|
212
230
|
|
213
|
-
|
214
|
-
|
215
|
-
|
231
|
+
self.class.log_opts(query,update,options,"create",log)
|
232
|
+
|
233
|
+
write_result = collection.update_one(query,update,options)
|
216
234
|
|
217
235
|
|
218
|
-
|
219
|
-
self.matched_count = write_result.matched_count
|
220
|
-
self.modified_count = write_result.modified_count
|
221
|
-
self.upserted_id = write_result.upserted_id
|
222
|
-
##as long as it matched a document, or it inserted a document
|
223
|
-
if write_result.matched_count > 0 || write_result.upserted_id
|
224
|
-
self.send("op_success=",true)
|
225
|
-
self.version = 1
|
226
|
-
else
|
227
|
-
self.send("op_success",false)
|
228
|
-
end
|
229
|
-
#if !write_result.upserted_id.nil?
|
230
236
|
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
237
|
+
self.matched_count = write_result.matched_count
|
238
|
+
self.modified_count = write_result.modified_count
|
239
|
+
self.upserted_id = write_result.upserted_id
|
240
|
+
##as long as it matched a document, or it inserted a document
|
241
|
+
if write_result.matched_count > 0 || write_result.upserted_id
|
242
|
+
self.send("op_success=",true)
|
243
|
+
self.version = 1
|
244
|
+
else
|
245
|
+
self.send("op_success",false)
|
246
|
+
end
|
236
247
|
|
237
248
|
|
238
|
-
|
249
|
+
|
250
|
+
end
|
251
|
+
rescue DbUnchanged => error
|
252
|
+
puts "caught db unchanged error, so callbacks will be halted.-------------------------------------------------------------"
|
253
|
+
end
|
239
254
|
|
240
255
|
end
|
241
256
|
|
@@ -297,7 +312,7 @@ module MongoidVersionedAtomic
|
|
297
312
|
expected_version = curr_doc["version"] + 1
|
298
313
|
|
299
314
|
##what happens is that we send the update["$set"][k] to whatever was stored in the dirty_fields.
|
300
|
-
|
315
|
+
begin
|
301
316
|
prepare_update(options) do
|
302
317
|
|
303
318
|
dirty_fields.keys.each do |k|
|
@@ -325,6 +340,9 @@ module MongoidVersionedAtomic
|
|
325
340
|
end
|
326
341
|
|
327
342
|
end
|
343
|
+
rescue DbUnchanged => error
|
344
|
+
#puts "Rescued db unchanged error, so remaining after update callbacks will be halted."
|
345
|
+
end
|
328
346
|
|
329
347
|
end
|
330
348
|
|