Skip to content

Commit 415a996

Browse files
committed
Merge branch '50-stable' into 51-stable
* 50-stable: [ci] explicitly declare mysql service postgresql 9.6 [test] make MySQL assertion also pass under driver 8.0 [mysql] adjust for Connector/J driver (8.0) changes [mysql, postgre, mariadb] respecting driver_name convention
2 parents ae82935 + c996ee3 commit 415a996

File tree

5 files changed

+131
-9
lines changed

5 files changed

+131
-9
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
language: ruby
22
sudo: false
3+
services:
4+
- mysql
5+
addons:
6+
postgresql: 9.6
37
branches:
48
only:
59
- master

lib/arjdbc/mysql/connection_methods.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ def mysql_connection(config)
1010

1111
return jndi_connection(config) if jndi_config?(config)
1212

13-
driver = config[:driver] ||=
14-
defined?(::Jdbc::MySQL.driver_name) ? ::Jdbc::MySQL.driver_name : 'com.mysql.jdbc.Driver'
15-
16-
mysql_driver = driver.start_with?('com.mysql.')
17-
mariadb_driver = ! mysql_driver && driver.start_with?('org.mariadb.')
13+
driver = config[:driver]
14+
mysql_driver = driver.nil? || driver.to_s.start_with?('com.mysql.')
15+
mariadb_driver = ! mysql_driver && driver.to_s.start_with?('org.mariadb.')
1816

1917
begin
2018
require 'jdbc/mysql'
2119
::Jdbc::MySQL.load_driver(:require) if defined?(::Jdbc::MySQL.load_driver)
2220
rescue LoadError # assuming driver.jar is on the class-path
2321
end if mysql_driver
2422

23+
if driver.nil?
24+
config[:driver] ||=
25+
defined?(::Jdbc::MySQL.driver_name) ? ::Jdbc::MySQL.driver_name : 'com.mysql.jdbc.Driver'
26+
end
27+
2528
config[:username] = 'root' unless config.key?(:username)
2629
# jdbc:mysql://[host][,failoverhost...][:port]/[database]
2730
# - if the host name is not specified, it defaults to 127.0.0.1
@@ -36,7 +39,8 @@ def mysql_connection(config)
3639

3740
properties = ( config[:properties] ||= {} )
3841
if mysql_driver
39-
properties['zeroDateTimeBehavior'] ||= 'convertToNull'
42+
properties['zeroDateTimeBehavior'] ||=
43+
config[:driver].to_s.start_with?('com.mysql.cj.') ? 'CONVERT_TO_NULL' : 'convertToNull'
4044
properties['jdbcCompliantTruncation'] ||= false
4145
# NOTE: this is "better" than passing what users are used to set on MRI
4246
# e.g. 'utf8mb4' will fail cause the driver will check for a Java charset
@@ -108,7 +112,8 @@ def mariadb_connection(config)
108112
rescue LoadError # assuming driver.jar is on the class-path
109113
end
110114

111-
config[:driver] ||= 'org.mariadb.jdbc.Driver'
115+
config[:driver] ||=
116+
defined?(::Jdbc::MariaDB.driver_name) ? ::Jdbc::MariaDB.driver_name : 'org.mariadb.jdbc.Driver'
112117

113118
mysql_connection(config)
114119
end

lib/arjdbc/postgresql/connection_methods.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def postgresql_connection(config)
1616
::Jdbc::Postgres.load_driver(:require) if defined?(::Jdbc::Postgres.load_driver)
1717
rescue LoadError # assuming driver.jar is on the class-path
1818
end
19-
driver = config[:driver] ||= 'org.postgresql.Driver'
19+
driver = (config[:driver] ||=
20+
defined?(::Jdbc::Postgres.driver_name) ? ::Jdbc::Postgres.driver_name : 'org.postgresql.Driver')
2021

2122
host = config[:host] ||= ( config[:hostaddr] || ENV['PGHOST'] || 'localhost' )
2223
port = config[:port] ||= ( ENV['PGPORT'] || 5432 )

test/db/mysql/simple_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,10 @@ def test_jdbc_error
424424
assert error.error_code.is_a?(Fixnum)
425425
assert error.sql_state
426426

427+
# MySQL driver 5.1 :
427428
# #<ActiveRecord::JDBCError: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'arjdbc_test.bogus' doesn't exist>
428429
unless mariadb_driver?
429-
assert_match /com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table '.*?bogus' doesn't exist/, error.message
430+
assert_match(/SQLSyntaxErrorException: Table '.*?bogus' doesn't exist/, error.message)
430431
else
431432
assert_match /java.sql.SQLSyntaxErrorException: .*Table '.*?bogus' doesn't exist/, error.message
432433
end

test/db/mysql/unit_test.rb

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,117 @@ def initialize; end
109109
assert config[:adapter_class]
110110
end
111111

112+
test 'configuration attempts to load MySQL driver by default' do
113+
load_jdbc_mysql
114+
115+
connection_handler = connection_handler_stub
116+
117+
config = { database: 'MyDB' }
118+
connection_handler.expects(:jdbc_connection)
119+
::Jdbc::MySQL.expects(:load_driver).with(:require)
120+
connection_handler.mysql_connection config
121+
end
122+
123+
test 'configuration uses driver_name from Jdbc::MySQL' do
124+
load_jdbc_mysql
125+
126+
connection_handler = connection_handler_stub
127+
128+
config = { database: 'MyDB' }
129+
connection_handler.expects(:jdbc_connection)
130+
::Jdbc::MySQL.expects(:driver_name).returns('com.mysql.CustomDriver')
131+
connection_handler.mysql_connection config
132+
assert_equal 'com.mysql.CustomDriver', config[:driver]
133+
end
134+
135+
test 'configuration sets up properties according to connector/j driver (>= 8.0)' do
136+
load_jdbc_mysql
137+
138+
connection_handler = connection_handler_stub
139+
140+
config = { database: 'MyDB' }
141+
connection_handler.expects(:jdbc_connection)
142+
::Jdbc::MySQL.expects(:driver_name).returns('com.mysql.cj.jdbc.Driver')
143+
connection_handler.mysql_connection config
144+
assert_equal 'com.mysql.cj.jdbc.Driver', config[:driver]
145+
assert_equal 'CONVERT_TO_NULL', config[:properties]['zeroDateTimeBehavior']
146+
assert_equal false, config[:properties]['useLegacyDatetimeCode']
147+
assert_equal false, config[:properties]['jdbcCompliantTruncation']
148+
assert_equal false, config[:properties]['useSSL']
149+
end
150+
151+
def load_jdbc_mysql
152+
require 'jdbc/mysql'
153+
end
154+
155+
end
156+
157+
context 'connection (Jdbc::MySQL missing)' do
158+
159+
module ::Jdbc; end
160+
161+
@@jdbc_mysql = ::Jdbc::MySQL rescue nil
162+
163+
def setup
164+
::Jdbc.send :remove_const, :MySQL if @@jdbc_mysql
165+
end
166+
167+
def teardown
168+
::Jdbc.const_set :MySQL, @@jdbc_mysql if @@jdbc_mysql
169+
end
170+
171+
test 'configuration sets url and properties assuming mysql driver (<= 5.1)' do
172+
connection_handler = connection_handler_stub
173+
174+
config = { host: '127.0.0.1', database: 'MyDB' }
175+
connection_handler.expects(:jdbc_connection)
176+
connection_handler.mysql_connection config
177+
178+
# we do not complete username/database etc :
179+
assert_equal 'root', config[:username]
180+
assert_equal 'com.mysql.jdbc.Driver', config[:driver]
181+
assert_equal 'jdbc:mysql://127.0.0.1/MyDB', config[:url]
182+
assert_equal 'UTF-8', config[:properties]['characterEncoding']
183+
assert_equal 'convertToNull', config[:properties]['zeroDateTimeBehavior']
184+
assert_equal false, config[:properties]['useLegacyDatetimeCode']
185+
assert_equal false, config[:properties]['jdbcCompliantTruncation']
186+
assert_equal false, config[:properties]['useSSL']
187+
end
188+
189+
test 'configuration attempts to load MySQL driver by default' do
190+
connection_handler = connection_handler_stub
191+
192+
config = { database: 'MyDB' }
193+
connection_handler.expects(:jdbc_connection)
194+
connection_handler.expects(:require).with('jdbc/mysql')
195+
connection_handler.mysql_connection config
196+
end
197+
198+
test 'configuration allows to skip driver loading' do
199+
connection_handler = connection_handler_stub
200+
201+
config = { database: 'MyDB', driver: false }
202+
connection_handler.expects(:jdbc_connection)
203+
connection_handler.expects(:require).never
204+
connection_handler.mysql_connection config
205+
assert_not config[:driver] # allow Java's service discovery mechanism (with connector/j 8.0)
206+
end
207+
208+
test 'configuration works with MariaDB driver specified' do
209+
connection_handler = connection_handler_stub
210+
211+
config = { database: 'MyDB', driver: 'org.mariadb.jdbc.Driver' }
212+
connection_handler.expects(:jdbc_connection)
213+
connection_handler.mysql_connection config
214+
215+
# we do not complete username/database etc :
216+
assert_equal 'root', config[:username]
217+
assert_equal 'org.mariadb.jdbc.Driver', config[:driver]
218+
assert_equal 'jdbc:mysql://localhost/MyDB', config[:url]
219+
assert_equal false, config[:properties]['useLegacyDatetimeCode']
220+
assert_equal false, config[:properties]['useSsl']
221+
end
222+
112223
end
113224

114225
end if defined? JRUBY_VERSION

0 commit comments

Comments
 (0)