em-pg-client-helper 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/em-pg-client-helper.rb +21 -16
- data/spec/db_transaction_spec.rb +3 -3
- metadata +4 -4
data/lib/em-pg-client-helper.rb
CHANGED
@@ -48,16 +48,17 @@ module PG::EM::Client::Helper
|
|
48
48
|
#
|
49
49
|
# Calling this method opens up a transaction (by executing `BEGIN`), and
|
50
50
|
# then runs the supplied block, passing in a transaction object which you
|
51
|
-
# can use to execute SQL commands.
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
51
|
+
# can use to execute SQL commands. You *must* call `txn.commit` or
|
52
|
+
# `txn.callback` yourself as your inner-most callback, otherwise you will
|
53
|
+
# leave the connection in a weird limbo state.
|
54
|
+
#
|
55
|
+
# Since `db_transaction` returns a deferrable, you should use `#callback`
|
56
|
+
# to specify what to run after the transaction completes.
|
56
57
|
#
|
57
58
|
# If an SQL error occurs during the transaction, the block's execution
|
58
59
|
# will be aborted, a `ROLLBACK` will be executed, and the `#errback`
|
59
|
-
# block (if defined) on the deferrable
|
60
|
-
# `#callback` block).
|
60
|
+
# block (if defined) on the deferrable that `db_transaction` returns will
|
61
|
+
# be executed (rather than the `#callback` block).
|
61
62
|
#
|
62
63
|
# Arguments:
|
63
64
|
#
|
@@ -71,7 +72,10 @@ module PG::EM::Client::Helper
|
|
71
72
|
# of the transaction. This block will be passed a
|
72
73
|
# `PG::EM::Client::Helper::Transaction` instance, which has methods to
|
73
74
|
# allow you to commit or rollback the transaction, and execute SQL
|
74
|
-
# statements within the context of the transaction.
|
75
|
+
# statements within the context of the transaction. This block *must*
|
76
|
+
# return a deferrable. When that returned deferrable completes, a
|
77
|
+
# COMMIT will be triggered automatically. If that deferrable fails,
|
78
|
+
# a ROLLBACK will be sent, instead.
|
75
79
|
#
|
76
80
|
# Returns a deferrable object, on which you can call `#callback` and
|
77
81
|
# `#errback` to define what to do when the transaction succeeds or fails,
|
@@ -79,7 +83,7 @@ module PG::EM::Client::Helper
|
|
79
83
|
#
|
80
84
|
def db_transaction(db, opts = {}, &blk)
|
81
85
|
if db.is_a? PG::EM::ConnectionPool
|
82
|
-
db.__send__(:
|
86
|
+
db.__send__(:hold_deferred) do |conn|
|
83
87
|
::PG::EM::Client::Helper::Transaction.new(conn, opts, &blk)
|
84
88
|
end
|
85
89
|
else
|
@@ -108,7 +112,6 @@ module PG::EM::Client::Helper
|
|
108
112
|
|
109
113
|
conn.exec_defer("BEGIN").callback do
|
110
114
|
blk.call(self)
|
111
|
-
commit if @active
|
112
115
|
end.errback { |ex| rollback(ex) }
|
113
116
|
end
|
114
117
|
|
@@ -131,9 +134,11 @@ module PG::EM::Client::Helper
|
|
131
134
|
# event of a database error or other exception.
|
132
135
|
#
|
133
136
|
def rollback(ex)
|
134
|
-
@
|
135
|
-
@
|
136
|
-
|
137
|
+
if @active
|
138
|
+
@conn.exec_defer("ROLLBACK") do
|
139
|
+
@active = false
|
140
|
+
self.fail(ex)
|
141
|
+
end
|
137
142
|
end
|
138
143
|
end
|
139
144
|
|
@@ -158,9 +163,9 @@ module PG::EM::Client::Helper
|
|
158
163
|
"Cannot execute a query in a transaction that has been closed"
|
159
164
|
end
|
160
165
|
|
161
|
-
|
162
|
-
|
163
|
-
|
166
|
+
@conn.exec_defer(sql, values).
|
167
|
+
errback { |ex| rollback(ex) }.
|
168
|
+
tap { |df| df.callback(&blk) if blk }
|
164
169
|
end
|
165
170
|
end
|
166
171
|
end
|
data/spec/db_transaction_spec.rb
CHANGED
@@ -31,19 +31,19 @@ describe "PG::EM::Client::Helper#db_transaction" do
|
|
31
31
|
|
32
32
|
it "runs a BEGIN/COMMIT cycle by default" do
|
33
33
|
mock_query_chain([["BEGIN"], ["COMMIT"]]) do
|
34
|
-
|
34
|
+
EM::DefaultDeferrable.new.tap { |df| df.succeed }
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
it "rolls back if BEGIN fails" do
|
39
39
|
mock_query_chain([["BEGIN"], ["ROLLBACK"]], 0) do
|
40
|
-
|
40
|
+
EM::DefaultDeferrable.new.tap { |df| df.succeed }
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
44
|
it "rolls back if COMMIT fails" do
|
45
45
|
mock_query_chain([["BEGIN"], ["COMMIT"], ["ROLLBACK"]], 1) do
|
46
|
-
|
46
|
+
EM::DefaultDeferrable.new.tap { |df| df.succeed }
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-pg-client-helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-10-
|
12
|
+
date: 2014-10-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: git-version-bump
|
@@ -236,7 +236,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
236
236
|
version: '0'
|
237
237
|
segments:
|
238
238
|
- 0
|
239
|
-
hash:
|
239
|
+
hash: 3974973386782984490
|
240
240
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
241
241
|
none: false
|
242
242
|
requirements:
|
@@ -245,7 +245,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
245
245
|
version: '0'
|
246
246
|
segments:
|
247
247
|
- 0
|
248
|
-
hash:
|
248
|
+
hash: 3974973386782984490
|
249
249
|
requirements: []
|
250
250
|
rubyforge_project:
|
251
251
|
rubygems_version: 1.8.23
|