From af0f104f2cc03edfcb63c19281dceaffcf888e06 Mon Sep 17 00:00:00 2001 From: Keith Gable Date: Wed, 31 Jan 2018 13:46:48 -0800 Subject: [PATCH 1/9] Update README with additional instructions for local setup so that the test suite works. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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). From 2ade848de4bd04eeed2d6ee953d4f8fe12e32b7b Mon Sep 17 00:00:00 2001 From: Keith Gable Date: Wed, 31 Jan 2018 14:52:27 -0800 Subject: [PATCH 2/9] Create a failing test for the connection methods implemented by this adapter --- test/connections_test.rb | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 test/connections_test.rb diff --git a/test/connections_test.rb b/test/connections_test.rb new file mode 100644 index 00000000..a7bedf52 --- /dev/null +++ b/test/connections_test.rb @@ -0,0 +1,43 @@ +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 + end +end \ No newline at end of file From 920860fdadbc02c82b885eb18bd26608d9fce4ba Mon Sep 17 00:00:00 2001 From: Keith Gable Date: Wed, 31 Jan 2018 14:53:12 -0800 Subject: [PATCH 3/9] Make reconnect! work (this breaks the API of adapters if you override initialize in the adapter) --- lib/active_record/connection_adapters/odbc_adapter.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/active_record/connection_adapters/odbc_adapter.rb b/lib/active_record/connection_adapters/odbc_adapter.rb index c1163245..cf7c00a8 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, config) 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 From 896938c4c6f1a0de0886805b01edf087395d83ee Mon Sep 17 00:00:00 2001 From: Keith Gable Date: Wed, 31 Jan 2018 15:43:56 -0800 Subject: [PATCH 4/9] Ask Travis to test in Ruby 2.3 and 2.4. --- .travis.yml | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 08e48209..78d5f824 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ 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=;' @@ -15,7 +15,30 @@ matrix: - unixodbc-dev - libmyodbc - mysql-client - - rvm: 2.3.1 + - 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" + 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: + mysql: "5.5" + apt: + packages: + - unixodbc + - unixodbc-dev + - libmyodbc + - mysql-client + - rvm: 2.4.3 env: - DB=postgresql - CONN_STR='DRIVER={PostgreSQL ANSI};SERVER=localhost;PORT=5432;DATABASE=odbc_test;UID=postgres;' From c3ef1849108de9b4c7d387ed558895eaaeac0670 Mon Sep 17 00:00:00 2001 From: Keith Gable Date: Wed, 31 Jan 2018 15:52:12 -0800 Subject: [PATCH 5/9] Update Travis config so it works with latest Travis LTS images --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 78d5f824..b51f9b25 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ matrix: - DB=mysql - CONN_STR='DRIVER=MySQL;SERVER=localhost;DATABASE=odbc_test;USER=root;PASSWORD=;' addons: - mysql: "5.5" + mysql: "5.6" apt: packages: - unixodbc @@ -20,7 +20,7 @@ matrix: - 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 @@ -31,7 +31,7 @@ matrix: - DB=mysql - CONN_STR='DRIVER=MySQL;SERVER=localhost;DATABASE=odbc_test;USER=root;PASSWORD=;' addons: - mysql: "5.5" + mysql: "5.6" apt: packages: - unixodbc @@ -43,7 +43,7 @@ matrix: - 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 From 070b2e144a0121129ba5cac943aaef67baa1a4f6 Mon Sep 17 00:00:00 2001 From: Keith Gable Date: Wed, 31 Jan 2018 15:57:59 -0800 Subject: [PATCH 6/9] (Hopefully) make MySQL install on the latest Travis images --- .travis.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b51f9b25..e31e21bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,13 +8,14 @@ matrix: - DB=mysql - CONN_STR='DRIVER=MySQL;SERVER=localhost;DATABASE=odbc_test;USER=root;PASSWORD=;' addons: - mysql: "5.6" apt: packages: - unixodbc - unixodbc-dev - libmyodbc - mysql-client + services: + - mysql - rvm: 2.3.6 env: - DB=postgresql @@ -31,13 +32,14 @@ matrix: - DB=mysql - CONN_STR='DRIVER=MySQL;SERVER=localhost;DATABASE=odbc_test;USER=root;PASSWORD=;' addons: - mysql: "5.6" apt: packages: - unixodbc - unixodbc-dev - libmyodbc - mysql-client + services: + - mysql - rvm: 2.4.3 env: - DB=postgresql From b263288e1d4a6de3531a43ef8a8f94fdb217f487 Mon Sep 17 00:00:00 2001 From: Keith Gable Date: Wed, 31 Jan 2018 16:04:46 -0800 Subject: [PATCH 7/9] Add an additional test that we're reconnected successfully --- test/connections_test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/connections_test.rb b/test/connections_test.rb index a7bedf52..d1239fa7 100644 --- a/test/connections_test.rb +++ b/test/connections_test.rb @@ -39,5 +39,6 @@ def test_reconnect! 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 From 8a8ea96a2e02d061286aa24fb1e31fab8ef49184 Mon Sep 17 00:00:00 2001 From: Keith Gable Date: Wed, 31 Jan 2018 16:05:23 -0800 Subject: [PATCH 8/9] Pass options because it's been extended by the connect code --- lib/active_record/connection_adapters/odbc_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_record/connection_adapters/odbc_adapter.rb b/lib/active_record/connection_adapters/odbc_adapter.rb index cf7c00a8..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, config) + dbms.adapter_class.new(connection, logger, dbms, options) end private From 622105e07c7d8a80f0940daa93d78d15bc315180 Mon Sep 17 00:00:00 2001 From: Keith Gable Date: Wed, 31 Jan 2018 16:10:45 -0800 Subject: [PATCH 9/9] Postgres passes, let's see if I can get MySQL to pass :) --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e31e21bc..9e6a516f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ matrix: - unixodbc - unixodbc-dev - libmyodbc - - mysql-client + - mysql-client-5.6 services: - mysql - rvm: 2.3.6 @@ -37,7 +37,7 @@ matrix: - unixodbc - unixodbc-dev - libmyodbc - - mysql-client + - mysql-client-5.6 services: - mysql - rvm: 2.4.3