sequel-activerecord_connection 0.2.5 → 1.0.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 +58 -0
- data/README.md +45 -61
- data/lib/sequel/extensions/activerecord_connection.rb +99 -44
- data/lib/sequel/extensions/activerecord_connection/jdbc.rb +4 -25
- data/lib/sequel/extensions/activerecord_connection/mysql2.rb +10 -24
- data/lib/sequel/extensions/activerecord_connection/postgres.rb +90 -10
- data/lib/sequel/extensions/activerecord_connection/sqlite.rb +6 -33
- data/sequel-activerecord_connection.gemspec +3 -2
- metadata +21 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 929f5047395464d1d3616aeab41742b6766ae14b96ca1f5b7ac47bb1991f3391
|
4
|
+
data.tar.gz: c931c23d71b73ce2bb35118e1d02ecc2df529f2c297b995c9c5ee46c5b0bfe85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 062f919b4268a57e392bad5dd7d4a988adc90a8df2d4076f5abfa9afe7c125534f9e7ad1ca16733d0539e622f2b988f95d4653d6b076cd356809d95daf1c24d2
|
7
|
+
data.tar.gz: 1f1efabd6b3ae74c6d35b3d0a23ad87a9d1c939e209afc2f24267840c6c72b2ff60fb1ca86010335b65a251c3c57fa81b300aa4cf38b9ad49df53b00f931d89f
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,61 @@
|
|
1
|
+
## 1.0.0 (2020-10-25)
|
2
|
+
|
3
|
+
* Clear AR statement cache on `ActiveRecord::PreparedStatementCacheExpired` when Sequel holds the transaction (@janko)
|
4
|
+
|
5
|
+
* Pick up `ActiveRecord::Base.default_timezone` being changed on runtime (@janko)
|
6
|
+
|
7
|
+
* Support prepared statements and bound variables in all adapters (@janko)
|
8
|
+
|
9
|
+
* Correctly identify identity columns as primary keys in Postgres adapter (@janko)
|
10
|
+
|
11
|
+
* Avoid using deprecated `sqlite3` API in SQLite adapter (@janko)
|
12
|
+
|
13
|
+
* Allow using any external Active Record adapters (@janko)
|
14
|
+
|
15
|
+
* Avoid potential bugs when converting Active Record exceptions into Sequel exceptions (@janko)
|
16
|
+
|
17
|
+
* Don't use Active Record locks when executing queries with Sequel (@janko)
|
18
|
+
|
19
|
+
* Support `Database#valid_connection?` in Postgres adapter (@janko)
|
20
|
+
|
21
|
+
* Fully utilize Sequel's logic for detecting disconnects in Postgres adapter (@janko)
|
22
|
+
|
23
|
+
* Support `Database#{copy_table,copy_into,listen}` in Postgres adapter (@janko)
|
24
|
+
|
25
|
+
* Log all queries executed by Sequel (@janko)
|
26
|
+
|
27
|
+
* Log executed queries to Sequel logger(s) as well (@janko)
|
28
|
+
|
29
|
+
* Specially label queries executed by Sequel in Active Record logs (@janko)
|
30
|
+
|
31
|
+
## 0.4.1 (2020-09-28)
|
32
|
+
|
33
|
+
* Require Sequel version 5.16.0 or above (@janko)
|
34
|
+
|
35
|
+
## 0.4.0 (2020-09-28)
|
36
|
+
|
37
|
+
* Return correct result of `Database#in_transaction?` after ActiveRecord transaction exited (@janko)
|
38
|
+
|
39
|
+
* Make ActiveRecord create a savepoint inside a Sequel transaction with `auto_savepoint: true` (@janko)
|
40
|
+
|
41
|
+
* Make Sequel create a savepoint inside ActiveRecord transaction with `joinable: false` (@janko)
|
42
|
+
|
43
|
+
* Improve reliability of nested transactions when combining Sequel and ActiveRecord (@janko)
|
44
|
+
|
45
|
+
* Raise error when attempting to add an `after_commit`/`after_rollback` hook on ActiveRecord transaction (@janko)
|
46
|
+
|
47
|
+
* Fix infinite loop that could happen with transactional Rails tests (@janko)
|
48
|
+
|
49
|
+
## 0.3.0 (2020-07-24)
|
50
|
+
|
51
|
+
* Fully support Sequel transaction API (all transaction options, transaction/savepoint hooks etc.) (@janko)
|
52
|
+
|
53
|
+
## 0.2.6 (2020-07-19)
|
54
|
+
|
55
|
+
* Return block result in `Sequel::Database#transaction` (@zabolotnov87, @janko)
|
56
|
+
|
57
|
+
* Fix `Sequel::Model#save_changes` or `#save` with additional options not executing (@zabolotnov87, @janko)
|
58
|
+
|
1
59
|
## 0.2.5 (2020-06-04)
|
2
60
|
|
3
61
|
* Use `#current_timestamp_utc` for the JDBC SQLite adapter as well (@HoneyryderChuck)
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
This is an extension for [Sequel] that allows it to reuse an existing
|
4
4
|
ActiveRecord connection for database interaction. It works on ActiveRecord 4.2
|
5
5
|
or higher, and supports the built-in `postgresql`, `mysql2` and `sqlite3`
|
6
|
-
adapters.
|
6
|
+
adapters, as well as JDBC adapter for JRuby.
|
7
7
|
|
8
8
|
This can be useful if you're using a library that uses Sequel for database
|
9
9
|
interaction (e.g. [Rodauth]), but you want to avoid creating a separate
|
@@ -11,15 +11,15 @@ database connection. Or if you're transitioning from ActiveRecord to Sequel,
|
|
11
11
|
and want the database connection to be shared.
|
12
12
|
|
13
13
|
Note that this is a best-effort implementation, so some discrepancies are still
|
14
|
-
possible. That being said, this implementation passes
|
15
|
-
(for all adapters), which has fairly advanced Sequel usage.
|
14
|
+
possible. That being said, this implementation passes Rodauth's test suite
|
15
|
+
(for all adapters), which has some fairly advanced Sequel usage.
|
16
16
|
|
17
17
|
## Installation
|
18
18
|
|
19
19
|
Add this line to your application's Gemfile:
|
20
20
|
|
21
21
|
```ruby
|
22
|
-
gem "sequel-activerecord_connection"
|
22
|
+
gem "sequel-activerecord_connection", "~> 0.3"
|
23
23
|
```
|
24
24
|
|
25
25
|
And then execute:
|
@@ -42,8 +42,7 @@ appropriate Sequel adapter and load the `activerecord_connection` extension:
|
|
42
42
|
```rb
|
43
43
|
require "sequel"
|
44
44
|
|
45
|
-
DB = Sequel.postgres(
|
46
|
-
DB.extension :activerecord_connection
|
45
|
+
DB = Sequel.postgres(extensions: :activerecord_connection)
|
47
46
|
```
|
48
47
|
|
49
48
|
Now any Sequel operations that you make will internaly be done using the
|
@@ -75,70 +74,68 @@ ActiveRecord adapters, just make sure to initialize the corresponding Sequel
|
|
75
74
|
adapter before loading the extension.
|
76
75
|
|
77
76
|
```rb
|
78
|
-
|
79
|
-
#
|
80
|
-
|
81
|
-
# or
|
82
|
-
DB = Sequel.sqlite(test: false) # for "sqlite3" adapter
|
77
|
+
Sequel.postgres(extensions: :activerecord_connection) # for "postgresql" adapter
|
78
|
+
Sequel.mysql2(extensions: :activerecord_connection) # for "mysql2" adapter
|
79
|
+
Sequel.sqlite(extensions: :activerecord_connection) # for "sqlite3" adapter
|
83
80
|
```
|
84
81
|
|
85
|
-
|
86
|
-
|
87
|
-
The database extension overrides Sequel transactions to use ActiveRecord
|
88
|
-
transcations, which allows using ActiveRecord inside Sequel transactions (and
|
89
|
-
vice-versa), and have things like ActiveRecord's transactional callbacks still
|
90
|
-
work correctly.
|
82
|
+
If you're on JRuby, you should be using the JDBC adapters:
|
91
83
|
|
92
84
|
```rb
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
97
|
-
end
|
85
|
+
Sequel.connect("jdbc:postgresql://", extensions: :activerecord_connection) # for "jdbcpostgresql" adapter
|
86
|
+
Sequel.connect("jdbc:mysql://", extensions: :activerecord_connection) # for "jdbcmysql" adapter
|
87
|
+
Sequel.connect("jdbc:sqlite://", extensions: :activerecord_connection) # for "jdbcsqlite3" adapter
|
98
88
|
```
|
99
89
|
|
100
|
-
|
90
|
+
### Transactions
|
101
91
|
|
102
|
-
|
103
|
-
|
104
|
-
|
92
|
+
This database extension keeps the transaction state of Sequel and ActiveRecord
|
93
|
+
in sync, allowing you to use Sequel and ActiveRecord transactions
|
94
|
+
interchangeably (including nesting them), and have things like ActiveRecord's
|
95
|
+
and Sequel's transactional callbacks still work correctly.
|
105
96
|
|
106
97
|
```rb
|
107
98
|
ActiveRecord::Base.transaction do
|
108
|
-
DB.
|
109
|
-
DB.transaction do # will not create a savepoint
|
110
|
-
# ...
|
111
|
-
end
|
112
|
-
end
|
99
|
+
DB.in_transaction? #=> true
|
113
100
|
end
|
114
101
|
```
|
115
102
|
|
116
|
-
|
103
|
+
Sequel's transaction API is fully supported:
|
117
104
|
|
118
105
|
```rb
|
119
|
-
|
120
|
-
DB.
|
106
|
+
DB.transaction(isolation: :serializable) do
|
107
|
+
DB.after_commit { ... } # executed after transaction commits
|
108
|
+
DB.transaction(savepoint: true) do # creates a savepoint
|
109
|
+
DB.after_commit(savepoint: true) { ... } # executed if all enclosing savepoints have been released
|
110
|
+
end
|
121
111
|
end
|
122
112
|
```
|
123
113
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
### Exceptions
|
129
|
-
|
130
|
-
To ensure Sequel compatibility, any `ActiveRecord::StatementInvalid` exceptions
|
131
|
-
will be translated into Sequel exceptions:
|
114
|
+
One caveat to keep in mind is that using Sequel's transaction/savepoint hooks
|
115
|
+
currently don't work if ActiveRecord holds the corresponding
|
116
|
+
transaction/savepoint. This is because it's difficult to be notified when
|
117
|
+
ActiveRecord commits or rolls back the transaction/savepoint.
|
132
118
|
|
133
119
|
```rb
|
134
|
-
DB
|
135
|
-
|
120
|
+
DB.transaction do
|
121
|
+
DB.after_commit { ... } # will get executed
|
122
|
+
end
|
136
123
|
|
137
|
-
DB
|
138
|
-
|
124
|
+
DB.transaction do
|
125
|
+
DB.transaction(savepoint: true) do
|
126
|
+
DB.after_commit(savepoint: true) { ... } # will get executed
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
ActiveRecord::Base.transaction do
|
131
|
+
DB.after_commit { ... } # not allowed (will raise Sequel::ActiveRecordConnection::Error)
|
132
|
+
end
|
139
133
|
|
140
|
-
DB
|
141
|
-
|
134
|
+
DB.transaction do
|
135
|
+
ActiveRecord::Base.transaction(requires_new: true) do
|
136
|
+
DB.after_commit(savepoint: true) { ... } # not allowed (will raise Sequel::ActiveRecordConnection::Error)
|
137
|
+
end
|
138
|
+
end
|
142
139
|
```
|
143
140
|
|
144
141
|
### Model
|
@@ -156,19 +153,6 @@ end
|
|
156
153
|
DB.activerecord_model = MyModel
|
157
154
|
```
|
158
155
|
|
159
|
-
### Timezone
|
160
|
-
|
161
|
-
Sequel's database timezone will be automatically set to ActiveRecord's default
|
162
|
-
timezone (`:utc` by default) when the extension is loaded.
|
163
|
-
|
164
|
-
If you happen to be changing ActiveRecord's default timezone after you've
|
165
|
-
loaded the extension, make sure to reflect that in your Sequel database object,
|
166
|
-
for example:
|
167
|
-
|
168
|
-
```rb
|
169
|
-
DB.timezone = :local
|
170
|
-
```
|
171
|
-
|
172
156
|
## Tests
|
173
157
|
|
174
158
|
You'll first want to run the rake tasks for setting up databases and users:
|
@@ -1,16 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Sequel
|
2
4
|
module ActiveRecordConnection
|
3
5
|
Error = Class.new(Sequel::Error)
|
4
6
|
|
7
|
+
TRANSACTION_ISOLATION_MAP = {
|
8
|
+
uncommitted: :read_uncommitted,
|
9
|
+
committed: :read_committed,
|
10
|
+
repeatable: :repeatable_read,
|
11
|
+
serializable: :serializable,
|
12
|
+
}
|
13
|
+
|
5
14
|
def self.extended(db)
|
6
15
|
db.activerecord_model = ActiveRecord::Base
|
7
|
-
db.timezone = ActiveRecord::Base.default_timezone
|
8
16
|
|
9
17
|
begin
|
10
18
|
require "sequel/extensions/activerecord_connection/#{db.adapter_scheme}"
|
11
19
|
db.extend Sequel::ActiveRecordConnection.const_get(db.adapter_scheme.capitalize)
|
12
20
|
rescue LoadError
|
13
|
-
|
21
|
+
# assume the Sequel adapter already works with Active Record
|
14
22
|
end
|
15
23
|
end
|
16
24
|
|
@@ -21,71 +29,118 @@ module Sequel
|
|
21
29
|
raise Error, "creating a Sequel connection is not allowed"
|
22
30
|
end
|
23
31
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
server = options.delete(:server)
|
32
|
+
# Avoid calling Sequel's connection pool, instead use Active Record's.
|
33
|
+
def synchronize(*)
|
34
|
+
yield activerecord_connection.raw_connection
|
35
|
+
end
|
29
36
|
|
30
|
-
|
37
|
+
# Log executed queries into Active Record logger as well.
|
38
|
+
def log_connection_yield(sql, conn, args = nil)
|
39
|
+
sql += "; #{args.inspect}" if args
|
31
40
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
41
|
+
activerecord_log(sql) { super }
|
42
|
+
end
|
43
|
+
|
44
|
+
# Match database timezone with Active Record.
|
45
|
+
def timezone
|
46
|
+
@timezone || ActiveRecord::Base.default_timezone
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
# Synchronizes transaction state with ActiveRecord. Sequel uses this
|
52
|
+
# information to know whether we're in a transaction, whether to create a
|
53
|
+
# savepoint, when to run transaction/savepoint hooks etc.
|
54
|
+
def _trans(conn)
|
55
|
+
hash = super || { savepoints: [], activerecord: true }
|
56
|
+
|
57
|
+
# add any ActiveRecord transactions/savepoints that have been opened
|
58
|
+
# directly via ActiveRecord::Base.transaction
|
59
|
+
while hash[:savepoints].length < activerecord_connection.open_transactions
|
60
|
+
hash[:savepoints] << { activerecord: true }
|
36
61
|
end
|
37
62
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
raise if rollback == :reraise
|
44
|
-
raise ActiveRecord::Rollback, exception.message, exception.backtrace
|
45
|
-
ensure
|
46
|
-
Thread.current[:sequel_activerecord_auto_savepoint] = nil if auto_savepoint
|
47
|
-
end
|
63
|
+
# remove any ActiveRecord transactions/savepoints that have been closed
|
64
|
+
# directly via ActiveRecord::Base.transaction
|
65
|
+
while hash[:savepoints].length > activerecord_connection.open_transactions && hash[:savepoints].last[:activerecord]
|
66
|
+
hash[:savepoints].pop
|
67
|
+
end
|
48
68
|
|
49
|
-
|
69
|
+
# sync knowledge about joinability of current ActiveRecord transaction/savepoint
|
70
|
+
if activerecord_connection.transaction_open? && !activerecord_connection.current_transaction.joinable?
|
71
|
+
hash[:savepoints].last[:auto_savepoint] = true
|
50
72
|
end
|
73
|
+
|
74
|
+
if hash[:savepoints].empty? && hash[:activerecord]
|
75
|
+
Sequel.synchronize { @transactions.delete(conn) }
|
76
|
+
else
|
77
|
+
Sequel.synchronize { @transactions[conn] = hash }
|
78
|
+
end
|
79
|
+
|
80
|
+
super
|
51
81
|
end
|
52
82
|
|
53
|
-
def
|
54
|
-
|
83
|
+
def begin_transaction(conn, opts = OPTS)
|
84
|
+
isolation = TRANSACTION_ISOLATION_MAP.fetch(opts[:isolation]) if opts[:isolation]
|
85
|
+
joinable = !opts[:auto_savepoint]
|
86
|
+
|
87
|
+
activerecord_connection.begin_transaction(isolation: isolation, joinable: joinable)
|
55
88
|
end
|
56
89
|
|
57
|
-
|
58
|
-
|
59
|
-
fail Error, "Database##{meth} is currently not supported"
|
60
|
-
end
|
90
|
+
def commit_transaction(conn, opts = OPTS)
|
91
|
+
activerecord_connection.commit_transaction
|
61
92
|
end
|
62
93
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
yield activerecord_raw_connection
|
94
|
+
def rollback_transaction(conn, opts = OPTS)
|
95
|
+
activerecord_connection.rollback_transaction
|
96
|
+
end
|
97
|
+
|
98
|
+
def add_transaction_hook(conn, type, block)
|
99
|
+
if _trans(conn)[:activerecord]
|
100
|
+
fail Error, "cannot add transaction hook when ActiveRecord holds the outer transaction"
|
71
101
|
end
|
102
|
+
|
103
|
+
super
|
72
104
|
end
|
73
105
|
|
74
|
-
|
106
|
+
def add_savepoint_hook(conn, type, block)
|
107
|
+
if _trans(conn)[:savepoints].last[:activerecord]
|
108
|
+
fail Error, "cannot add savepoint hook when ActiveRecord holds the current savepoint"
|
109
|
+
end
|
75
110
|
|
76
|
-
|
77
|
-
activerecord_connection.raw_connection
|
111
|
+
super
|
78
112
|
end
|
79
113
|
|
80
114
|
def activerecord_connection
|
81
115
|
activerecord_model.connection
|
82
116
|
end
|
83
117
|
|
84
|
-
def
|
85
|
-
|
86
|
-
|
87
|
-
|
118
|
+
def activerecord_log(sql, &block)
|
119
|
+
ActiveSupport::Notifications.instrument(
|
120
|
+
"sql.active_record",
|
121
|
+
sql: sql,
|
122
|
+
name: "Sequel",
|
123
|
+
connection: activerecord_connection,
|
124
|
+
&block
|
125
|
+
)
|
126
|
+
end
|
127
|
+
|
128
|
+
module Utils
|
129
|
+
def self.set_value(object, name, new_value)
|
130
|
+
original_value = object.send(name)
|
131
|
+
object.send(:"#{name}=", new_value)
|
88
132
|
yield
|
133
|
+
ensure
|
134
|
+
object.send(:"#{name}=", original_value)
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.add_prepared_statements_cache(conn)
|
138
|
+
return if conn.respond_to?(:prepared_statements)
|
139
|
+
|
140
|
+
class << conn
|
141
|
+
attr_accessor :prepared_statements
|
142
|
+
end
|
143
|
+
conn.prepared_statements = {}
|
89
144
|
end
|
90
145
|
end
|
91
146
|
end
|
@@ -7,32 +7,11 @@ module Sequel
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
rescue ActiveRecord::StatementInvalid => exception
|
14
|
-
raise_error(exception.cause, classes: database_error_classes)
|
15
|
-
rescue *database_error_classes => e
|
16
|
-
raise_error(e, classes: database_error_classes)
|
17
|
-
ensure
|
18
|
-
stmt.close if stmt
|
19
|
-
end
|
20
|
-
|
21
|
-
def execute(sql, opts=OPTS)
|
22
|
-
activerecord_connection.send(:log, sql) do
|
23
|
-
super
|
24
|
-
end
|
25
|
-
rescue ActiveRecord::StatementInvalid => exception
|
26
|
-
raise_error(exception.cause, classes: database_error_classes)
|
27
|
-
end
|
28
|
-
|
29
|
-
def execute_dui(sql, opts=OPTS)
|
30
|
-
activerecord_connection.send(:log, sql) do
|
31
|
-
super
|
10
|
+
def synchronize(*)
|
11
|
+
super do |conn|
|
12
|
+
yield conn.connection
|
32
13
|
end
|
33
|
-
rescue ActiveRecord::StatementInvalid => exception
|
34
|
-
raise_error(exception.cause, classes: database_error_classes)
|
35
14
|
end
|
36
15
|
end
|
37
16
|
end
|
38
|
-
end
|
17
|
+
end
|
@@ -1,34 +1,20 @@
|
|
1
1
|
module Sequel
|
2
2
|
module ActiveRecordConnection
|
3
3
|
module Mysql2
|
4
|
-
def
|
5
|
-
|
4
|
+
def synchronize(*)
|
5
|
+
super do |conn|
|
6
|
+
# required for prepared statements
|
7
|
+
conn.instance_variable_set(:@sequel_default_query_options, conn.query_options.dup)
|
8
|
+
Utils.add_prepared_statements_cache(conn)
|
6
9
|
|
7
|
-
|
8
|
-
as: :hash,
|
9
|
-
symbolize_keys: true,
|
10
|
-
cache_rows: false,
|
11
|
-
)
|
10
|
+
conn.query_options.merge!(as: :hash, symbolize_keys: true, cache_rows: false)
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
yield result
|
18
|
-
else
|
19
|
-
result
|
12
|
+
begin
|
13
|
+
yield conn
|
14
|
+
ensure
|
15
|
+
conn.query_options.replace(conn.instance_variable_get(:@sequel_default_query_options))
|
20
16
|
end
|
21
|
-
elsif block_given?
|
22
|
-
yield activerecord_raw_connection
|
23
|
-
end
|
24
|
-
rescue ActiveRecord::StatementInvalid => exception
|
25
|
-
if exception.cause.is_a?(::Mysql2::Error)
|
26
|
-
raise_error(exception.cause)
|
27
|
-
else
|
28
|
-
raise
|
29
17
|
end
|
30
|
-
ensure
|
31
|
-
activerecord_raw_connection.query_options.replace(original_query_options)
|
32
18
|
end
|
33
19
|
end
|
34
20
|
end
|
@@ -1,18 +1,98 @@
|
|
1
1
|
module Sequel
|
2
2
|
module ActiveRecordConnection
|
3
3
|
module Postgres
|
4
|
-
def
|
5
|
-
|
4
|
+
def synchronize(*)
|
5
|
+
super do |conn|
|
6
|
+
conn.extend(ConnectionMethods)
|
7
|
+
conn.instance_variable_set(:@db, self)
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
Utils.add_prepared_statements_cache(conn)
|
10
|
+
|
11
|
+
Utils.set_value(conn, :type_map_for_results, PG::TypeMapAllStrings.new) do
|
12
|
+
yield conn
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Reject unsupported Postgres-specific transaction options.
|
18
|
+
def transaction(opts = OPTS)
|
19
|
+
%i[deferrable read_only synchronous].each do |key|
|
20
|
+
fail Error, "#{key.inspect} transaction option is currently not supported" if opts.key?(key)
|
21
|
+
end
|
22
|
+
|
23
|
+
super
|
24
|
+
rescue => e
|
25
|
+
activerecord_connection.clear_cache! if e.class.name == "ActiveRecord::PreparedStatementCacheExpired" && !in_transaction?
|
26
|
+
raise
|
27
|
+
end
|
28
|
+
|
29
|
+
# Copy-pasted from Sequel::Postgres::Adapter.
|
30
|
+
module ConnectionMethods
|
31
|
+
# The underlying exception classes to reraise as disconnect errors
|
32
|
+
# instead of regular database errors.
|
33
|
+
DISCONNECT_ERROR_CLASSES = [IOError, Errno::EPIPE, Errno::ECONNRESET, ::PG::ConnectionBad].freeze
|
34
|
+
|
35
|
+
# Since exception class based disconnect checking may not work,
|
36
|
+
# also trying parsing the exception message to look for disconnect
|
37
|
+
# errors.
|
38
|
+
DISCONNECT_ERROR_REGEX = /\A#{Regexp.union([
|
39
|
+
"ERROR: cached plan must not change result type",
|
40
|
+
"could not receive data from server",
|
41
|
+
"no connection to the server",
|
42
|
+
"connection not open",
|
43
|
+
"connection is closed",
|
44
|
+
"terminating connection due to administrator command",
|
45
|
+
"PQconsumeInput() "
|
46
|
+
])}/
|
47
|
+
|
48
|
+
def async_exec_params(sql, args)
|
49
|
+
defined?(super) ? super : async_exec(sql, args)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Raise a Sequel::DatabaseDisconnectError if a one of the disconnect
|
53
|
+
# error classes is raised, or a PG::Error is raised and the connection
|
54
|
+
# status cannot be determined or it is not OK.
|
55
|
+
def check_disconnect_errors
|
56
|
+
begin
|
57
|
+
yield
|
58
|
+
rescue *DISCONNECT_ERROR_CLASSES => e
|
59
|
+
disconnect = true
|
60
|
+
raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError))
|
61
|
+
rescue PG::Error => e
|
62
|
+
disconnect = false
|
63
|
+
begin
|
64
|
+
s = status
|
65
|
+
rescue PG::Error
|
66
|
+
disconnect = true
|
67
|
+
end
|
68
|
+
status_ok = (s == PG::CONNECTION_OK)
|
69
|
+
disconnect ||= !status_ok
|
70
|
+
disconnect ||= e.message =~ DISCONNECT_ERROR_REGEX
|
71
|
+
disconnect ? raise(Sequel.convert_exception_class(e, Sequel::DatabaseDisconnectError)) : raise
|
72
|
+
ensure
|
73
|
+
block if status_ok && !disconnect
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Execute the given SQL with this connection. If a block is given,
|
78
|
+
# yield the results, otherwise, return the number of changed rows.
|
79
|
+
def execute(sql, args = nil)
|
80
|
+
args = args.map { |v| @db.bound_variable_arg(v, self) } if args
|
81
|
+
result = check_disconnect_errors { execute_query(sql, args) }
|
82
|
+
|
83
|
+
block_given? ? yield(result) : result.cmd_tuples
|
84
|
+
ensure
|
85
|
+
result.clear if result
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
# Return the PG::Result containing the query results.
|
91
|
+
def execute_query(sql, args)
|
92
|
+
@db.log_connection_yield(sql, self, args) do
|
93
|
+
args ? async_exec_params(sql, args) : async_exec(sql)
|
94
|
+
end
|
11
95
|
end
|
12
|
-
rescue ActiveRecord::StatementInvalid => exception
|
13
|
-
raise_error(exception.cause, classes: database_error_classes)
|
14
|
-
ensure
|
15
|
-
result.clear if result
|
16
96
|
end
|
17
97
|
end
|
18
98
|
end
|
@@ -7,43 +7,16 @@ module Sequel
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
private
|
15
|
-
|
16
|
-
# ActiveRecord doesn't send SQLite methods Sequel expects, so we need to
|
17
|
-
# try to replicate what ActiveRecord does around connection excecution.
|
18
|
-
def _execute(type, sql, opts, &block)
|
19
|
-
if activerecord_raw_connection.respond_to?(:extended_result_codes=)
|
20
|
-
activerecord_raw_connection.extended_result_codes = true
|
21
|
-
end
|
10
|
+
def synchronize(*)
|
11
|
+
super do |conn|
|
12
|
+
conn.extended_result_codes = true if conn.respond_to?(:extended_result_codes=)
|
22
13
|
|
23
|
-
|
24
|
-
activerecord_connection.materialize_transactions
|
25
|
-
end
|
14
|
+
Utils.add_prepared_statements_cache(conn)
|
26
15
|
|
27
|
-
|
28
|
-
|
29
|
-
case type
|
30
|
-
when :select
|
31
|
-
activerecord_raw_connection.query(sql, &block)
|
32
|
-
when :insert
|
33
|
-
activerecord_raw_connection.execute(sql)
|
34
|
-
activerecord_raw_connection.last_insert_row_id
|
35
|
-
when :update
|
36
|
-
activerecord_raw_connection.execute_batch(sql)
|
37
|
-
activerecord_raw_connection.changes
|
38
|
-
end
|
16
|
+
Utils.set_value(conn, :results_as_hash, nil) do
|
17
|
+
yield conn
|
39
18
|
end
|
40
19
|
end
|
41
|
-
rescue ActiveRecord::StatementInvalid => exception
|
42
|
-
if exception.cause.is_a?(SQLite3::Exception)
|
43
|
-
raise_error(exception.cause)
|
44
|
-
else
|
45
|
-
raise exception
|
46
|
-
end
|
47
20
|
end
|
48
21
|
end
|
49
22
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "sequel-activerecord_connection"
|
3
|
-
spec.version = "0.
|
3
|
+
spec.version = "1.0.0"
|
4
4
|
spec.authors = ["Janko Marohnić"]
|
5
5
|
spec.email = ["janko.marohnic@gmail.com"]
|
6
6
|
|
@@ -11,10 +11,11 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.2.0")
|
13
13
|
|
14
|
-
spec.add_dependency "sequel", "
|
14
|
+
spec.add_dependency "sequel", "~> 5.16"
|
15
15
|
spec.add_dependency "activerecord", ">= 4.2", "< 7"
|
16
16
|
|
17
17
|
spec.add_development_dependency "minitest"
|
18
|
+
spec.add_development_dependency "warning" if RUBY_VERSION >= "2.4"
|
18
19
|
|
19
20
|
spec.files = Dir["README.md", "LICENSE.txt", "CHANGELOG.md", "lib/**/*.rb", "*.gemspec"]
|
20
21
|
spec.require_paths = ["lib"]
|
metadata
CHANGED
@@ -1,35 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel-activerecord_connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.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-
|
11
|
+
date: 2020-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '4.0'
|
20
|
-
- - "<"
|
17
|
+
- - "~>"
|
21
18
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
19
|
+
version: '5.16'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
|
-
- - "
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '4.0'
|
30
|
-
- - "<"
|
24
|
+
- - "~>"
|
31
25
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
26
|
+
version: '5.16'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: activerecord
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,6 +58,20 @@ dependencies:
|
|
64
58
|
- - ">="
|
65
59
|
- !ruby/object:Gem::Version
|
66
60
|
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: warning
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
67
75
|
description: Allows Sequel to use ActiveRecord connection for database interaction.
|
68
76
|
email:
|
69
77
|
- janko.marohnic@gmail.com
|
@@ -99,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
107
|
- !ruby/object:Gem::Version
|
100
108
|
version: '0'
|
101
109
|
requirements: []
|
102
|
-
rubygems_version: 3.1.
|
110
|
+
rubygems_version: 3.1.4
|
103
111
|
signing_key:
|
104
112
|
specification_version: 4
|
105
113
|
summary: Allows Sequel to use ActiveRecord connection for database interaction.
|