sequel-activerecord_connection 1.0.1 → 1.1.0

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
  SHA256:
3
- metadata.gz: 6cbc8358a3097618cd7acc4e8cf46c013ef4d8b6aefde8e05de0277ca585c0b9
4
- data.tar.gz: 2ba345ca95455c1acaabf7e2619b81b97b332d1c353bab2de50f700e9ba69148
3
+ metadata.gz: fba99c145ebf98ed9c3e26fdc96afc3bc5fa7820e01d466ab10320c72765c8ae
4
+ data.tar.gz: ae36bef3a63280cf54f1a92e814c1bf4928dc8050f572694aac4340389115f9b
5
5
  SHA512:
6
- metadata.gz: a9d3f729bfa2e5a82e78521856b2928887700a67129e47af6eb40d1bf4508b6deb7202928f7626ea3c4f6a424b23df8410756d2333295c1d56fee79b123dc7dd
7
- data.tar.gz: 6184d4e1dd21bc550ac99285fd379215dcd13b4474aeff39928bdc68cc25ac34b235f68bed03260fae3fea34ef203422e2665bb12c822479e6ce4cc8d54d1f59
6
+ metadata.gz: 073babf3f4e9d60dcb439eb0d9a7b801b9cf23b9be53366382cdd9e18234de66c329d0a975e68fb57d78cbf80f26ddafcd3f1bc90e8282a9a24b90cd1f8c0a43
7
+ data.tar.gz: 420a6326c6c0e0b85874b52c89e747a59702280e763d9f6a090dd649b17718771cefaa1d9d72d4244a92bcfbee7ff2c186480ad0005d17685abcd0a595ccb97c
@@ -1,3 +1,11 @@
1
+ ## 1.1.0 (2020-11-08)
2
+
3
+ * Drop support for Ruby 2.2 (@janko)
4
+
5
+ * Support transaction/savepoint hooks even when Active Record holds the transaction/savepoint (@janko)
6
+
7
+ * Don't test the connection on `Sequel.connect` by default (@janko)
8
+
1
9
  ## 1.0.1 (2020-10-28)
2
10
 
3
11
  * Use Active Record connection lock in `Database#synchronize` (@janko)
data/README.md CHANGED
@@ -4,9 +4,9 @@ This is an extension for [Sequel] that allows it to reuse an existing
4
4
  ActiveRecord connection for database interaction.
5
5
 
6
6
  This can be useful if you're using a library that uses Sequel for database
7
- interaction (e.g. [Rodauth]), but you want to avoid creating a separate
8
- database connection. Or if you're transitioning from ActiveRecord to Sequel,
9
- and want the database connection to be shared.
7
+ interaction (e.g. [Rodauth] or [rom-sql]), but you want to avoid creating a
8
+ separate database connection. Or if you're transitioning from ActiveRecord to
9
+ Sequel, and want the database connection to be shared.
10
10
 
11
11
  It works on ActiveRecord 4.2+ and fully supports PostgresSQL, MySQL and SQLite
12
12
  adapters, both the native ones and JDBC (JRuby). Other adapters might work too,
@@ -109,31 +109,70 @@ DB.transaction(isolation: :serializable) do
109
109
  end
110
110
  ```
111
111
 
112
- One caveat to keep in mind is that using Sequel's transaction/savepoint hooks
113
- currently don't work if ActiveRecord holds the corresponding
114
- transaction/savepoint. This is because it's difficult to be notified when
115
- ActiveRecord commits or rolls back the transaction/savepoint.
112
+ When combining Active Record and Sequel transactions, Sequel transaction hook
113
+ functionality will be utilized when possible.
116
114
 
117
115
  ```rb
116
+ # Sequel: An after_commit transaction hook will always get executed if the outer
117
+ # transaction commits, even if it's added inside a savepoint that's rolled back.
118
118
  DB.transaction do
119
- DB.after_commit { ... } # will get executed
119
+ ActiveRecord::Base.transaction(requires_new: true) do
120
+ DB.after_commit { puts "after commit" }
121
+ raise ActiveRecord::Rollback
122
+ end
120
123
  end
121
-
122
- DB.transaction do
123
- DB.transaction(savepoint: true) do
124
- DB.after_commit(savepoint: true) { ... } # will get executed
124
+ #>> BEGIN
125
+ #>> SAVEPOINT active_record_1
126
+ #>> ROLLBACK TO SAVEPOINT active_record_1
127
+ #>> COMMIT
128
+ #>> after commit
129
+
130
+ # Sequel: An after_commit savepoint hook will get executed only after the outer
131
+ # transaction commits, given that all enclosing savepoints have been released.
132
+ DB.transaction(auto_savepoint: true) do
133
+ DB.transaction do
134
+ DB.after_commit(savepoint: true) { puts "after commit" }
135
+ raise Sequel::Rollback
125
136
  end
126
137
  end
138
+ #>> BEGIN
139
+ #>> SAVEPOINT active_record_1
140
+ #>> RELEASE SAVEPOINT active_record_1
141
+ #>> COMMIT
142
+ #>> after commit
143
+ ```
144
+
145
+ In case of (a) adding a transaction hook while Active Record holds the
146
+ transaction, or (b) adding a savepoint hook when Active Record holds any
147
+ enclosing savepoint, Active Record transaction callbacks will be used instead
148
+ of Sequel hooks, which have slightly different behaviour in some circumstances.
127
149
 
150
+ ```rb
151
+ # ActiveRecord: An after_commit transaction callback is not executed if any
152
+ # if the enclosing savepoints have been rolled back
128
153
  ActiveRecord::Base.transaction do
129
- DB.after_commit { ... } # not allowed (will raise Sequel::ActiveRecordConnection::Error)
154
+ DB.transaction(savepoint: true) do
155
+ DB.after_commit { puts "after commit" }
156
+ raise Sequel::Rollback
157
+ end
130
158
  end
131
-
132
- DB.transaction do
133
- ActiveRecord::Base.transaction(requires_new: true) do
134
- DB.after_commit(savepoint: true) { ... } # not allowed (will raise Sequel::ActiveRecordConnection::Error)
159
+ #>> BEGIN
160
+ #>> SAVEPOINT active_record_1
161
+ #>> ROLLBACK TO SAVEPOINT active_record_1
162
+ #>> COMMIT
163
+
164
+ # ActiveRecord: An after_commit transaction callback can be executed already
165
+ # after a savepoint is released, if the enclosing transaction is not joinable.
166
+ ActiveRecord::Base.transaction(joinable: false) do
167
+ DB.transaction do
168
+ DB.after_commit { puts "after commit" }
135
169
  end
136
170
  end
171
+ #>> BEGIN
172
+ #>> SAVEPOINT active_record_1
173
+ #>> RELEASE SAVEPOINT active_record_1
174
+ #>> after commit
175
+ #>> COMMIT
137
176
  ```
138
177
 
139
178
  ### Model
@@ -183,3 +222,4 @@ Everyone interacting in this project's codebases, issue trackers, chat rooms and
183
222
 
184
223
  [Sequel]: https://github.com/jeremyevans/sequel
185
224
  [Rodauth]: https://github.com/jeremyevans/rodauth
225
+ [rom-sql]: https://github.com/rom-rb/rom-sql
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "after_commit_everywhere"
4
+
3
5
  module Sequel
4
6
  module ActiveRecordConnection
5
7
  Error = Class.new(Sequel::Error)
@@ -11,8 +13,11 @@ module Sequel
11
13
  serializable: :serializable,
12
14
  }
13
15
 
16
+ ACTIVERECORD_CALLBACKS = Object.new.extend(AfterCommitEverywhere)
17
+
14
18
  def self.extended(db)
15
19
  db.activerecord_model = ActiveRecord::Base
20
+ db.opts[:test] = false unless db.opts.key?(:test)
16
21
 
17
22
  begin
18
23
  require "sequel/extensions/activerecord_connection/#{db.adapter_scheme}"
@@ -97,20 +102,28 @@ module Sequel
97
102
  activerecord_connection.rollback_transaction
98
103
  end
99
104
 
105
+ # When Active Record holds the transaction, we cannot use Sequel hooks,
106
+ # because Sequel doesn't have knowledge of when the transaction is
107
+ # committed. So in this case we register an Active Record hook using the
108
+ # after_commit_everywhere gem.
100
109
  def add_transaction_hook(conn, type, block)
101
110
  if _trans(conn)[:activerecord]
102
- fail Error, "cannot add transaction hook when ActiveRecord holds the outer transaction"
111
+ ACTIVERECORD_CALLBACKS.public_send(type, &block)
112
+ else
113
+ super
103
114
  end
104
-
105
- super
106
115
  end
107
116
 
117
+ # When Active Record holds the savepoint, we cannot use Sequel hooks,
118
+ # because Sequel doesn't have knowledge of when the savepoint is
119
+ # released. So in this case we register an Active Record hook using the
120
+ # after_commit_everywhere gem.
108
121
  def add_savepoint_hook(conn, type, block)
109
122
  if _trans(conn)[:savepoints].last[:activerecord]
110
- fail Error, "cannot add savepoint hook when ActiveRecord holds the current savepoint"
123
+ ACTIVERECORD_CALLBACKS.public_send(type, &block)
124
+ else
125
+ super
111
126
  end
112
-
113
- super
114
127
  end
115
128
 
116
129
  # Active Record doesn't guarantee that a single connection can only be used
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "sequel-activerecord_connection"
3
- spec.version = "1.0.1"
3
+ spec.version = "1.1.0"
4
4
  spec.authors = ["Janko Marohnić"]
5
5
  spec.email = ["janko.marohnic@gmail.com"]
6
6
 
@@ -9,11 +9,13 @@ Gem::Specification.new do |spec|
9
9
  spec.homepage = "https://github.com/janko/sequel-activerecord_connection"
10
10
  spec.license = "MIT"
11
11
 
12
- spec.required_ruby_version = Gem::Requirement.new(">= 2.2.0")
12
+ spec.required_ruby_version = ">= 2.3"
13
13
 
14
14
  spec.add_dependency "sequel", "~> 5.16"
15
15
  spec.add_dependency "activerecord", ">= 4.2", "< 7"
16
+ spec.add_dependency "after_commit_everywhere", "~> 0.1.5"
16
17
 
18
+ spec.add_development_dependency "sequel", "~> 5.38"
17
19
  spec.add_development_dependency "minitest"
18
20
  spec.add_development_dependency "warning" if RUBY_VERSION >= "2.4"
19
21
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-activerecord_connection
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-29 00:00:00.000000000 Z
11
+ date: 2020-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -44,6 +44,34 @@ dependencies:
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '7'
47
+ - !ruby/object:Gem::Dependency
48
+ name: after_commit_everywhere
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 0.1.5
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 0.1.5
61
+ - !ruby/object:Gem::Dependency
62
+ name: sequel
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '5.38'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.38'
47
75
  - !ruby/object:Gem::Dependency
48
76
  name: minitest
49
77
  requirement: !ruby/object:Gem::Requirement
@@ -100,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
128
  requirements:
101
129
  - - ">="
102
130
  - !ruby/object:Gem::Version
103
- version: 2.2.0
131
+ version: '2.3'
104
132
  required_rubygems_version: !ruby/object:Gem::Requirement
105
133
  requirements:
106
134
  - - ">="