From 186a72f454c596adc1b9b676d0617ce4e0cf32dd Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Thu, 31 Oct 2019 17:36:16 +0800 Subject: [PATCH 1/6] test makara --- .travis.yml | 5 +++++ gemfiles/6.0.makara.gemfile | 17 +++++++++++++++++ test/lib/makara_connection.rb | 16 ++++++++++++++++ test/test_helper.rb | 7 +++---- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 gemfiles/6.0.makara.gemfile create mode 100644 test/lib/makara_connection.rb diff --git a/.travis.yml b/.travis.yml index a254a4b..527ccbc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,12 @@ gemfile: - gemfiles/5.1.gemfile - gemfiles/5.2.gemfile - gemfiles/6.0.gemfile + - gemfiles/6.0.makara.gemfile matrix: + include: + - env: DB=makara + gemfile: gemfiles/6.0.makara.gemfile + rvm: 2.6 exclude: - gemfile: gemfiles/3.2.gemfile rvm: 2.6 diff --git a/gemfiles/6.0.makara.gemfile b/gemfiles/6.0.makara.gemfile new file mode 100644 index 0000000..447bd8b --- /dev/null +++ b/gemfiles/6.0.makara.gemfile @@ -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: '../' diff --git a/test/lib/makara_connection.rb b/test/lib/makara_connection.rb new file mode 100644 index 0000000..a3f3532 --- /dev/null +++ b/test/lib/makara_connection.rb @@ -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' }, + ], + }, +) diff --git a/test/test_helper.rb b/test/test_helper.rb index fd48f48..f4cb61f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,10 +9,9 @@ require 'minitest/autorun' case ENV['DB'] -when 'mysql' - require 'lib/mysql2_connection' -when 'pg' - require 'lib/postgresql_connection' +when 'makara' ; require 'lib/makara_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`' end From 07411f72fc3fce6f0ddc41ab770dc010f5369e1b Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Thu, 31 Oct 2019 17:49:08 +0800 Subject: [PATCH 2/6] test makara + pg --- .travis.yml | 5 ++++- ..._connection.rb => makara_mysql_connection.rb} | 0 test/lib/makara_pg_connection.rb | 15 +++++++++++++++ test/test_helper.rb | 16 ++++++++++++---- 4 files changed, 31 insertions(+), 5 deletions(-) rename test/lib/{makara_connection.rb => makara_mysql_connection.rb} (100%) create mode 100644 test/lib/makara_pg_connection.rb diff --git a/.travis.yml b/.travis.yml index 527ccbc..13f7caf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,10 @@ gemfile: - gemfiles/6.0.makara.gemfile matrix: include: - - env: DB=makara + - 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: diff --git a/test/lib/makara_connection.rb b/test/lib/makara_mysql_connection.rb similarity index 100% rename from test/lib/makara_connection.rb rename to test/lib/makara_mysql_connection.rb diff --git a/test/lib/makara_pg_connection.rb b/test/lib/makara_pg_connection.rb new file mode 100644 index 0000000..2426604 --- /dev/null +++ b/test/lib/makara_pg_connection.rb @@ -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' }, + ], + }, +) diff --git a/test/test_helper.rb b/test/test_helper.rb index f4cb61f..1df87a2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,11 +9,19 @@ require 'minitest/autorun' case ENV['DB'] -when 'makara' ; require 'lib/makara_connection' -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' From 293d06daaed5802d086a888da676e645ce8a010e Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Thu, 31 Oct 2019 17:51:26 +0800 Subject: [PATCH 3/6] remove makara from gemfile matrix --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 13f7caf..e5cada7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,6 @@ gemfile: - gemfiles/5.1.gemfile - gemfiles/5.2.gemfile - gemfiles/6.0.gemfile - - gemfiles/6.0.makara.gemfile matrix: include: - env: DB=makara_mysql From 2447943775dfb55417d7b53cfcc49306191cc4a6 Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Thu, 31 Oct 2019 17:56:16 +0800 Subject: [PATCH 4/6] check whether mysql, pg is wrapped by makara --- lib/atomically/adapter_check_service.rb | 10 ++++++++++ lib/atomically/query_service.rb | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/atomically/adapter_check_service.rb b/lib/atomically/adapter_check_service.rb index 077b65c..6b7539c 100644 --- a/lib/atomically/adapter_check_service.rb +++ b/lib/atomically/adapter_check_service.rb @@ -13,4 +13,14 @@ def mysql? return false if not defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) return @klass.connection.is_a?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) end + + def makara_mysql? + return false if not defined?(ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter) + return @klass.connection.is_a?(ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter) + end + + def makara_pg? + return false if not defined?(ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter) + return @klass.connection.is_a?(ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter) + end end diff --git a/lib/atomically/query_service.rb b/lib/atomically/query_service.rb index 13837f9..ceb29f1 100644 --- a/lib/atomically/query_service.rb +++ b/lib/atomically/query_service.rb @@ -90,11 +90,13 @@ def update_all_and_get_ids(*args) private def db_is_pg? - Atomically::AdapterCheckService.new(@klass).pg? + service = Atomically::AdapterCheckService.new(@klass) + return service.pg? || service.makara_pg? end def db_is_mysql? - Atomically::AdapterCheckService.new(@klass).mysql? + service = Atomically::AdapterCheckService.new(@klass) + return service.mysql? || service.makara_mysql? end def on_duplicate_key_plus_sql(columns, conflict_target) From 8d14ed252064d8ed4d739c2262c4e88f805cea46 Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Thu, 31 Oct 2019 18:06:13 +0800 Subject: [PATCH 5/6] Fix: some test cases will use old method to check whether db is pg --- lib/atomically/adapter_check_service.rb | 24 ++++++++++++++---------- lib/atomically/query_service.rb | 6 ++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/atomically/adapter_check_service.rb b/lib/atomically/adapter_check_service.rb index 6b7539c..ed6a402 100644 --- a/lib/atomically/adapter_check_service.rb +++ b/lib/atomically/adapter_check_service.rb @@ -5,22 +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 - def makara_mysql? - return false if not defined?(ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter) - return @klass.connection.is_a?(ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter) + private + + def possible_pg_klasses + result = [] + result << ActiveRecord::ConnectionAdapters::PostgreSQLAdapter if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) + result << ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter if defined?(ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter) + return result end - def makara_pg? - return false if not defined?(ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter) - return @klass.connection.is_a?(ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter) + def possible_mysql_klasses + result = [] + result << ActiveRecord::ConnectionAdapters::Mysql2Adapter if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) + result << ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter if defined?(ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter) + return result end end diff --git a/lib/atomically/query_service.rb b/lib/atomically/query_service.rb index ceb29f1..13837f9 100644 --- a/lib/atomically/query_service.rb +++ b/lib/atomically/query_service.rb @@ -90,13 +90,11 @@ def update_all_and_get_ids(*args) private def db_is_pg? - service = Atomically::AdapterCheckService.new(@klass) - return service.pg? || service.makara_pg? + Atomically::AdapterCheckService.new(@klass).pg? end def db_is_mysql? - service = Atomically::AdapterCheckService.new(@klass) - return service.mysql? || service.makara_mysql? + Atomically::AdapterCheckService.new(@klass).mysql? end def on_duplicate_key_plus_sql(columns, conflict_target) From 95e1c9b7978b4941fda801a1fd33d4d8ff26ec09 Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Thu, 31 Oct 2019 18:08:22 +0800 Subject: [PATCH 6/6] cache calculation result --- lib/atomically/adapter_check_service.rb | 16 ++++++++-------- lib/atomically/query_service.rb | 14 +++++--------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/atomically/adapter_check_service.rb b/lib/atomically/adapter_check_service.rb index ed6a402..34f330c 100644 --- a/lib/atomically/adapter_check_service.rb +++ b/lib/atomically/adapter_check_service.rb @@ -15,16 +15,16 @@ def mysql? private def possible_pg_klasses - result = [] - result << ActiveRecord::ConnectionAdapters::PostgreSQLAdapter if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) - result << ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter if defined?(ActiveRecord::ConnectionAdapters::MakaraPostgreSQLAdapter) - return result + @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 - result = [] - result << ActiveRecord::ConnectionAdapters::Mysql2Adapter if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) - result << ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter if defined?(ActiveRecord::ConnectionAdapters::MakaraMysql2Adapter) - return result + @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 diff --git a/lib/atomically/query_service.rb b/lib/atomically/query_service.rb index 13837f9..e612ff6 100644 --- a/lib/atomically/query_service.rb +++ b/lib/atomically/query_service.rb @@ -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)" @@ -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 } @@ -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(', ')