diff --git a/lib/active_record/connection_adapters/odbc_adapter.rb b/lib/active_record/connection_adapters/odbc_adapter.rb index d57f80c8..175be59b 100644 --- a/lib/active_record/connection_adapters/odbc_adapter.rb +++ b/lib/active_record/connection_adapters/odbc_adapter.rb @@ -21,7 +21,7 @@ class << self def odbc_connection(config) config = config.symbolize_keys - connection, options = + connection, config = if config.key?(:dsn) odbc_dsn_connection(config) elsif config.key?(:conn_str) @@ -31,7 +31,7 @@ def odbc_connection(config) end database_metadata = ::ODBCAdapter::DatabaseMetadata.new(connection) - database_metadata.adapter_class.new(connection, logger, database_metadata) + database_metadata.adapter_class.new(connection, logger, config, database_metadata) end private @@ -40,8 +40,7 @@ def odbc_dsn_connection(config) username = config[:username] ? config[:username].to_s : nil password = config[:password] ? config[:password].to_s : nil connection = ODBC.connect(config[:dsn], username, password) - options = { dsn: config[:dsn], username: username, password: password } - [connection, options] + [connection, config.merge(username: username, password: password)] end # Connect using ODBC connection string @@ -61,8 +60,7 @@ def odbc_conn_str_connection(config) end connection = ODBC::Database.new.drvconnect(driver) - options = { conn_str: config[:conn_str], driver: driver } - [connection, options] + [connection, config.merge(driver: driver)] end end end @@ -83,9 +81,8 @@ class ODBCAdapter < AbstractAdapter attr_reader :database_metadata - def initialize(connection, logger, database_metadata) - super(connection, logger) - @connection = connection + def initialize(connection, logger, config, database_metadata) + super(connection, logger, config) @database_metadata = database_metadata end @@ -115,10 +112,10 @@ def active? def reconnect! disconnect! @connection = - if options.key?(:dsn) - ODBC.connect(options[:dsn], options[:username], options[:password]) + if @config.key?(:dsn) + ODBC.connect(@config[:dsn], @config[:username], @config[:password]) else - ODBC::Database.new.drvconnect(options[:driver]) + ODBC::Database.new.drvconnect(@config[:driver]) end super end diff --git a/test/connection_management_test.rb b/test/connection_management_test.rb new file mode 100644 index 00000000..57009a5a --- /dev/null +++ b/test/connection_management_test.rb @@ -0,0 +1,24 @@ +require 'test_helper' + +class ConnectionManagementTest < Minitest::Test + def test_connection_management + assert conn.active? + + conn.disconnect! + refute conn.active? + + conn.disconnect! + refute conn.active? + + conn.reconnect! + assert conn.active? + ensure + conn.reconnect! + end + + private + + def conn + ActiveRecord::Base.connection + end +end