Skip to content

Commit 5bd5d70

Browse files
committed
connection initialization: deep_dup the config
AR tests do things like: ActiveRecord::Base.configurations["arunit"].merge(database: "inexistent_activerecord_unittest") which is also valid in applications. Not dup'ing the config is problematic: E.g. the mysql adapter has this (simplified): unless config[:url] config[:url] = "...#{config[:database]}" end So once a DB connection has been made with the original config, :url is set, above example that tries a different DB has no effect at all and gives a connection the the original database instead. Fun. .deep_dup solves the problem as the original config is left untouched.
1 parent 611324e commit 5bd5d70

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

lib/arjdbc/mysql/connection_methods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22
ArJdbc::ConnectionMethods.module_eval do
33
def mysql_connection(config)
4+
config = config.deep_dup
45
# NOTE: this isn't "really" necessary but Rails (in tests) assumes being able to :
56
# ActiveRecord::Base.mysql2_connection ActiveRecord::Base.configurations['arunit'].merge(database: ...)
67
config = symbolize_keys_if_necessary(config)

lib/arjdbc/postgresql/connection_methods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22
ArJdbc::ConnectionMethods.module_eval do
33
def postgresql_connection(config)
4+
config = config.deep_dup
45
# NOTE: this isn't "really" necessary but Rails (in tests) assumes being able to :
56
# ActiveRecord::Base.postgresql_connection ActiveRecord::Base.configurations['arunit'].merge(:insert_returning => false)
67
# ... while using symbols by default but than configurations returning string keys ;(

lib/arjdbc/sqlite3/connection_methods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22
ArJdbc::ConnectionMethods.module_eval do
33
def sqlite3_connection(config)
4+
config = config.deep_dup
45
config[:adapter_spec] ||= ::ArJdbc::SQLite3
56
config[:adapter_class] = ActiveRecord::ConnectionAdapters::SQLite3Adapter unless config.key?(:adapter_class)
67

test/db/mysql/unit_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def initialize; end
9696
connection_handler = connection_handler_stub
9797

9898
config = { :jndi => 'jdbc/TestDS' }
99-
connection_handler.expects(:jndi_connection)
99+
connection_handler.expects(:jndi_connection).with() { |c| config = c }
100100
connection_handler.mysql_connection config
101101

102102
# we do not complete username/database etc :
@@ -126,7 +126,7 @@ def initialize; end
126126
connection_handler = connection_handler_stub
127127

128128
config = { database: 'MyDB' }
129-
connection_handler.expects(:jdbc_connection)
129+
connection_handler.expects(:jdbc_connection).with() { |c| config = c }
130130
::Jdbc::MySQL.expects(:driver_name).returns('com.mysql.CustomDriver')
131131
connection_handler.mysql_connection config
132132
assert_equal 'com.mysql.CustomDriver', config[:driver]
@@ -138,7 +138,7 @@ def initialize; end
138138
connection_handler = connection_handler_stub
139139

140140
config = { database: 'MyDB' }
141-
connection_handler.expects(:jdbc_connection)
141+
connection_handler.expects(:jdbc_connection).with() { |c| config = c }
142142
::Jdbc::MySQL.expects(:driver_name).returns('com.mysql.cj.jdbc.Driver')
143143
connection_handler.mysql_connection config
144144
assert_equal 'com.mysql.cj.jdbc.Driver', config[:driver]
@@ -174,7 +174,7 @@ def teardown
174174
connection_handler = connection_handler_stub
175175

176176
config = { host: '127.0.0.1', database: 'MyDB' }
177-
connection_handler.expects(:jdbc_connection)
177+
connection_handler.expects(:jdbc_connection).with() { |c| config = c }
178178
connection_handler.mysql_connection config
179179

180180
# we do not complete username/database etc :
@@ -201,7 +201,7 @@ def teardown
201201
connection_handler = connection_handler_stub
202202

203203
config = { database: 'MyDB', driver: false }
204-
connection_handler.expects(:jdbc_connection)
204+
connection_handler.expects(:jdbc_connection).with() { |c| config = c }
205205
connection_handler.expects(:require).never
206206
connection_handler.mysql_connection config
207207
assert_not config[:driver] # allow Java's service discovery mechanism (with connector/j 8.0)
@@ -211,7 +211,7 @@ def teardown
211211
connection_handler = connection_handler_stub
212212

213213
config = { database: 'MyDB', driver: 'org.mariadb.jdbc.Driver' }
214-
connection_handler.expects(:jdbc_connection)
214+
connection_handler.expects(:jdbc_connection).with() { |c| config = c }
215215
connection_handler.mysql_connection config
216216

217217
# we do not complete username/database etc :
@@ -224,4 +224,4 @@ def teardown
224224

225225
end
226226

227-
end if defined? JRUBY_VERSION
227+
end if defined? JRUBY_VERSION

test/db/postgresql/unit_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PostgresUnitTest < Test::Unit::TestCase
2222
connection_handler = connection_handler_stub
2323

2424
config = { :jndi => 'jdbc/TestDS' }
25-
connection_handler.expects(:jndi_connection)
25+
connection_handler.expects(:jndi_connection).with() { |c| config = c }
2626
connection_handler.postgresql_connection config
2727

2828
# we do not complete username/database etc :

0 commit comments

Comments
 (0)