Skip to content

Commit

Permalink
Merge pull request #18 from Shopify/update-for-newer-active-record
Browse files Browse the repository at this point in the history
Test newer rails and rubies
  • Loading branch information
casperisfine committed Aug 31, 2020
2 parents b7089fa + 82d4a89 commit 948dc55
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 52 deletions.
19 changes: 10 additions & 9 deletions .travis.yml
@@ -1,23 +1,24 @@
services:
- mysql

rvm:
- 2.2.2
- 2.3.0
- 2.4.3
- 2.5.0
- '2.5'
- '2.6'
- '2.7'

gemfile:
- gemfiles/Gemfile.activerecord50
- gemfiles/Gemfile.activerecord51
- gemfiles/Gemfile.activerecord52
- gemfiles/Gemfile.activerecord60
- gemfiles/Gemfile.activerecord-edge

matrix:
exclude:
- rvm: 2.2.2
- rvm: '2.5'
gemfile: gemfiles/Gemfile.activerecord-edge
- rvm: 2.3.0
- rvm: '2.6'
gemfile: gemfiles/Gemfile.activerecord-edge

before_script:
- mysql -e 'create database pedant_mysql2_test;'
- mysql -u root -h 127.0.0.1 -e 'create database pedant_mysql2_test;'

sudo: false
4 changes: 3 additions & 1 deletion activerecord-pedantmysql2-adapter.gemspec
Expand Up @@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/Shopify/activerecord-pedantmysql2-adapter'
spec.license = 'MIT'

spec.metadata['allowed_push_host'] = 'https://rubygems.org'

spec.required_ruby_version = '>= 2.2.2'

spec.files = `git ls-files -z`.split("\x0")
Expand All @@ -22,7 +24,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'activerecord', '>= 5.0'
spec.add_dependency 'mysql2', '>= 0.3.12'
spec.add_development_dependency 'bundler', '~> 1.5'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec', '>= 3.0'
spec.add_development_dependency 'rspec-its'
Expand Down
5 changes: 0 additions & 5 deletions gemfiles/Gemfile.activerecord50

This file was deleted.

5 changes: 0 additions & 5 deletions gemfiles/Gemfile.activerecord51

This file was deleted.

3 changes: 2 additions & 1 deletion gemfiles/Gemfile.activerecord52
@@ -1,4 +1,5 @@
source 'https://rubygems.org'
gemspec path: '..'

gem 'activerecord', github: 'rails/rails', branch: '5-2-0'
gem 'activerecord', '~> 5.2.0'
gem 'mysql2'
5 changes: 5 additions & 0 deletions gemfiles/Gemfile.activerecord60
@@ -0,0 +1,5 @@
source 'https://rubygems.org'
gemspec path: '..'

gem 'activerecord', '~> 6.0.0'
gem 'mysql2'
54 changes: 37 additions & 17 deletions lib/active_record/connection_adapters/pedant_mysql2_adapter.rb
Expand Up @@ -2,24 +2,44 @@

module ActiveRecord
module ConnectionHandling
def pedant_mysql2_connection(config)
config = config.symbolize_keys

config[:username] = 'root' if config[:username].nil?

if Mysql2::Client.const_defined? :FOUND_ROWS
config[:flags] = Mysql2::Client::FOUND_ROWS
if ConnectionAdapters::Mysql2Adapter.respond_to?(:new_client)
def pedant_mysql2_connection(config)
config = config.symbolize_keys
config[:flags] ||= 0

if config[:flags].kind_of? Array
config[:flags].push "FOUND_ROWS"
else
config[:flags] |= Mysql2::Client::FOUND_ROWS
end

ConnectionAdapters::PedantMysql2Adapter.new(
ConnectionAdapters::Mysql2Adapter.new_client(config),
logger,
nil,
config,
)
end

client = Mysql2::Client.new(config)

options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
ActiveRecord::ConnectionAdapters::PedantMysql2Adapter.new(client, logger, options, config)
rescue Mysql2::Error => error
if error.message.include?("Unknown database") && defined?(ActiveRecord::NoDatabaseError)
raise ActiveRecord::NoDatabaseError.new(error.message)
else
raise
else
def pedant_mysql2_connection(config)
config = config.symbolize_keys

config[:username] = 'root' if config[:username].nil?

if Mysql2::Client.const_defined? :FOUND_ROWS
config[:flags] = Mysql2::Client::FOUND_ROWS
end

client = Mysql2::Client.new(config)

options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
ActiveRecord::ConnectionAdapters::PedantMysql2Adapter.new(client, logger, options, config)
rescue Mysql2::Error => error
if error.message.include?("Unknown database") && defined?(ActiveRecord::NoDatabaseError)
raise ActiveRecord::NoDatabaseError.new(error.message)
else
raise
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pedant_mysql2.rb
Expand Up @@ -46,11 +46,11 @@ def whitelist
end

def ignored?(warning)
whitelist.any? { |matcher| matcher =~ warning.message } || drop_table_warning(warning)
whitelist.any? { |matcher| warning.message.match?(matcher) } || drop_table_warning(warning)
end

def drop_table_warning(warning)
warning.query =~ /\ADROP TABLE IF EXISTS/ || warning.message =~ /\AUnknown table/
warning.query.match?(/\ADROP TABLE IF EXISTS/) || warning.message.match?(/\AUnknown table/)
end

def setup_capture
Expand Down
1 change: 1 addition & 0 deletions spec/adapter_spec.rb
Expand Up @@ -7,6 +7,7 @@
before :each do
PedantMysql2.raise_warnings!
PedantMysql2.instance_variable_set(:@whitelist, nil)
PedantMysql2.ignore(/They will be merged with strict mode in a future release/)
connection.execute('SET SESSION binlog_format = "STATEMENT"')
if connection.execute('SHOW TABLES LIKE "comment"').size == 0
connection.execute('CREATE TABLE comment (id int)')
Expand Down
21 changes: 9 additions & 12 deletions spec/support/database.rb
@@ -1,12 +1,9 @@
ActiveRecord::Base.configurations = {
'test' => {
'adapter' => 'pedant_mysql2',
'database' => 'pedant_mysql2_test',
'username' => 'travis',
'encoding' => 'utf8',
'strict' => false,
'pool' => 5,
}
}

ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.establish_connection(
'adapter' => 'pedant_mysql2',
'database' => 'pedant_mysql2_test',
'username' => 'root',
'encoding' => 'utf8',
'host' => 'localhost',
'strict' => false,
'pool' => 5,
)

0 comments on commit 948dc55

Please sign in to comment.