diff --git a/.travis.yml b/.travis.yml index 08e48209..9e6a516f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,24 +3,49 @@ language: ruby cache: bundler matrix: include: - - rvm: 2.3.1 + - rvm: 2.3.6 env: - DB=mysql - CONN_STR='DRIVER=MySQL;SERVER=localhost;DATABASE=odbc_test;USER=root;PASSWORD=;' addons: - mysql: "5.5" apt: packages: - unixodbc - unixodbc-dev - libmyodbc - - mysql-client - - rvm: 2.3.1 + - mysql-client-5.6 + services: + - mysql + - rvm: 2.3.6 env: - DB=postgresql - CONN_STR='DRIVER={PostgreSQL ANSI};SERVER=localhost;PORT=5432;DATABASE=odbc_test;UID=postgres;' addons: - postgresql: "9.1" + postgresql: "9.2" + apt: + packages: + - unixodbc + - unixodbc-dev + - odbc-postgresql + - rvm: 2.4.3 + env: + - DB=mysql + - CONN_STR='DRIVER=MySQL;SERVER=localhost;DATABASE=odbc_test;USER=root;PASSWORD=;' + addons: + apt: + packages: + - unixodbc + - unixodbc-dev + - libmyodbc + - mysql-client-5.6 + services: + - mysql + - rvm: 2.4.3 + env: + - DB=postgresql + - CONN_STR='DRIVER={PostgreSQL ANSI};SERVER=localhost;PORT=5432;DATABASE=odbc_test;UID=postgres;' + addons: + postgresql: "9.2" apt: packages: - unixodbc diff --git a/README.md b/README.md index 6390a403..363b7947 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Or install it yourself as: ## Development -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. +After checking out the repo, run `bin/setup` to install dependencies. Next, configure your system with a PostgreSQL data source called `ODBCAdapterPostgreSQLTest` (you can alternatively set the environment variables `CONN_STR` or `DSN`). Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). diff --git a/lib/active_record/connection_adapters/odbc_adapter.rb b/lib/active_record/connection_adapters/odbc_adapter.rb index c1163245..db907c76 100644 --- a/lib/active_record/connection_adapters/odbc_adapter.rb +++ b/lib/active_record/connection_adapters/odbc_adapter.rb @@ -30,7 +30,7 @@ def odbc_connection(config) # :nodoc: end dbms = ::ODBCAdapter::DBMS.new(connection) - dbms.adapter_class.new(connection, logger, dbms) + dbms.adapter_class.new(connection, logger, dbms, options) end private @@ -79,10 +79,11 @@ class ODBCAdapter < AbstractAdapter attr_reader :dbms - def initialize(connection, logger, dbms) + def initialize(connection, logger, dbms, options) super(connection, logger) @connection = connection @dbms = dbms + @options = options @visitor = self.class::BindSubstitution.new(self) end @@ -112,10 +113,10 @@ def active? def reconnect! disconnect! @connection = - if options.key?(:dsn) - ODBC.connect(options[:dsn], options[:username], options[:password]) + if @options.key?(:dsn) + ODBC.connect(@options[:dsn], @options[:username], @options[:password]) else - ODBC::Database.new.drvconnect(options[:driver]) + ODBC::Database.new.drvconnect(@options[:driver]) end super end diff --git a/test/connections_test.rb b/test/connections_test.rb new file mode 100644 index 00000000..d1239fa7 --- /dev/null +++ b/test/connections_test.rb @@ -0,0 +1,44 @@ +require 'test_helper' + +# Dummy class for this test +class ConnectionsTestDummyActiveRecordModel < ActiveRecord::Base + self.abstract_class = true +end + +# This test makes sure that all of the connection methods work properly +class ConnectionsTest < Minitest::Test + def setup + @options = { adapter: 'odbc' } + @options[:conn_str] = ENV['CONN_STR'] if ENV['CONN_STR'] + @options[:dsn] = ENV['DSN'] if ENV['DSN'] + @options[:dsn] = 'ODBCAdapterPostgreSQLTest' if @options.values_at(:conn_str, :dsn).compact.empty? + + ConnectionsTestDummyActiveRecordModel.establish_connection @options + + @connection = ConnectionsTestDummyActiveRecordModel.connection + end + + def teardown + @connection.disconnect! + end + + def test_active? + assert_equal @connection.raw_connection.connected?, @connection.active? + end + + def test_disconnect! + @raw_connection = @connection.raw_connection + + assert_equal true, @raw_connection.connected? + @connection.disconnect! + assert_equal false, @raw_connection.connected? + end + + def test_reconnect! + @old_raw_connection = @connection.raw_connection + assert_equal true, @connection.active? + @connection.reconnect! + refute_equal @old_raw_connection, @connection.raw_connection + assert_equal true, @connection.active? + end +end \ No newline at end of file