Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix: create_or_plus is broken when using makara adapter #16

Merged
merged 6 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ gemfile:
- gemfiles/5.2.gemfile
- gemfiles/6.0.gemfile
matrix:
include:
- env: DB=makara_mysql
gemfile: gemfiles/6.0.makara.gemfile
rvm: 2.6
- env: DB=makara_pg
gemfile: gemfiles/6.0.makara.gemfile
rvm: 2.6
exclude:
- gemfile: gemfiles/3.2.gemfile
rvm: 2.6
Expand Down
17 changes: 17 additions & 0 deletions gemfiles/6.0.makara.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
source 'https://rubygems.org'

gem 'activerecord', '~> 6.0.0'

group :test do
case ENV['DB']
when 'mysql' ; gem 'mysql2', '0.5.1'
when 'postgres' ; gem 'pg', '~> 0.18'
end
gem 'simplecov'
gem 'pluck_all', '>= 2.0.4'
gem 'timecop', '~> 0.9.1'
gem 'update_all_scope', '~> 0.1.0'
gem 'makara', '~> 0.4.1'
end

gemspec path: '../'
22 changes: 18 additions & 4 deletions lib/atomically/adapter_check_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@ def initialize(klass)
end

def pg?
return false if not defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
return @klass.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
possible_pg_klasses.any?{|s| @klass.connection.is_a?(s) }
end

def mysql?
return false if not defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
return @klass.connection.is_a?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
possible_mysql_klasses.any?{|s| @klass.connection.is_a?(s) }
end

private

def possible_pg_klasses
@possible_pg_klasses ||= [].tap do |result|
result << ActiveRecord::ConnectionAdapters::PostgreSQLAdapter if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
result << ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter if defined?(ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter)
end
end

def possible_mysql_klasses
@possible_mysql_klasses ||= [].tap do |result|
result << ActiveRecord::ConnectionAdapters::Mysql2Adapter if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
result << ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter if defined?(ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter)
end
end
end
14 changes: 5 additions & 9 deletions lib/atomically/query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def pay_all(hash, update_columns, primary_key: :id) # { id => pay_count }
end

raw_when_sql = hash.map{|id, pay_count| "WHEN #{sanitize(id)} THEN #{sanitize(-pay_count)}" }.join("\n")
no_var_in_sql = true if update_columns.size == 1 or db_is_pg?
no_var_in_sql = true if update_columns.size == 1 or adapter_check_service.pg?
update_sqls = update_columns.map.with_index do |column, idx|
if no_var_in_sql
value = "(\nCASE #{quote_column(primary_key)}\n#{raw_when_sql}\nEND)"
Expand Down Expand Up @@ -71,7 +71,7 @@ def decrement_unsigned_counters(counters)
end

def update_all_and_get_ids(*args)
if db_is_pg?
if adapter_check_service.pg?
scope = UpdateAllScope::UpdateAllScope.new(model: @model, relation: @relation.where(''))
scope.update(*args)
return @klass.connection.execute("#{scope.to_sql} RETURNING id", "#{@klass} Update All").map{|s| s['id'].to_i }
Expand All @@ -89,17 +89,13 @@ def update_all_and_get_ids(*args)

private

def db_is_pg?
Atomically::AdapterCheckService.new(@klass).pg?
end

def db_is_mysql?
Atomically::AdapterCheckService.new(@klass).mysql?
def adapter_check_service
@adapter_check_service ||= Atomically::AdapterCheckService.new(@klass)
end

def on_duplicate_key_plus_sql(columns, conflict_target)
service = Atomically::OnDuplicateSqlService.new(@klass, columns)
return service.mysql_quote_columns_for_plus.join(', ') if db_is_mysql?
return service.mysql_quote_columns_for_plus.join(', ') if adapter_check_service.mysql?
return {
conflict_target: conflict_target,
columns: service.pg_quote_columns_for_plus.join(', ')
Expand Down
16 changes: 16 additions & 0 deletions test/lib/makara_mysql_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'makara'

ActiveRecord::Base.establish_connection(
'adapter' => 'mysql2_makara',
'database' => 'travis_ci_test',
'username' => 'root',
'makara' => {
'connections' => [
{ 'role' => 'master' },
{ 'role' => 'slave' },
{ 'role' => 'slave' },
],
},
)
15 changes: 15 additions & 0 deletions test/lib/makara_pg_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'makara'

ActiveRecord::Base.establish_connection(
'adapter' => 'postgresql_makara',
'database' => 'travis_ci_test',
'makara' => {
'connections' => [
{ 'role' => 'master' },
{ 'role' => 'slave' },
{ 'role' => 'slave' },
],
},
)
17 changes: 12 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
require 'minitest/autorun'

case ENV['DB']
when 'mysql'
require 'lib/mysql2_connection'
when 'pg'
require 'lib/postgresql_connection'
when 'makara_mysql' ; require 'lib/makara_mysql_connection'
when 'makara_pg' ; require 'lib/makara_pg_connection'
when 'mysql' ; require 'lib/mysql2_connection'
when 'pg' ; require 'lib/postgresql_connection'
else
fail 'please run test cases by: `rake test DB=mysql` or `rake test DB=pg`'
fail [
'Unknown DB',
'Please run test cases by one of the following: ',
'- rake test DB=mysql',
'- rake test DB=pg',
'- rake test DB=makara_mysql',
'- rake test DB=makara_pg',
].join("\n")
end

require 'lib/patches'
Expand Down