diff --git a/Makefile b/Makefile index 84bc7e4..797788b 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,17 @@ -.PHONY: test-pg test-mysql +.PHONY: test-sqlite test-pg test-mysql + +test-sqlite: + appraisal rake test test-pg: docker compose up -d pg sleep 10 # give some time for the service to start - DATABASE_URL=postgres://with_advisory:with_advisory_pass@localhost/with_advisory_lock_test appraisal activerecord-7.1 rake test + DATABASE_URL=postgres://with_advisory:with_advisory_pass@localhost/with_advisory_lock_test appraisal rake test test-mysql: docker compose up -d mysql sleep 10 # give some time for the service to start - DATABASE_URL=mysql2://with_advisory:with_advisory_pass@0.0.0.0:3306/with_advisory_lock_test appraisal activerecord-7.1 rake test + DATABASE_URL=mysql2://with_advisory:with_advisory_pass@0.0.0.0:3306/with_advisory_lock_test appraisal rake test -test: test-pg test-mysql \ No newline at end of file +test: test-sqlite test-pg test-mysql diff --git a/docker-compose.yml b/docker-compose.yml index 11ca78b..2440305 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,4 +17,4 @@ services: MYSQL_RANDOM_ROOT_PASSWORD: "yes" MYSQL_ROOT_HOST: '%' ports: - - "3306:3306" \ No newline at end of file + - "3306:3306" diff --git a/lib/with_advisory_lock/database_adapter_support.rb b/lib/with_advisory_lock/database_adapter_support.rb index dafd3ed..680bb68 100644 --- a/lib/with_advisory_lock/database_adapter_support.rb +++ b/lib/with_advisory_lock/database_adapter_support.rb @@ -3,9 +3,10 @@ module WithAdvisoryLock class DatabaseAdapterSupport attr_reader :adapter_name + def initialize(connection) @connection = connection - @adapter_name = connection.adapter_name.downcase.to_sym + @adapter_name = connection.adapter_name.downcase.to_sym end def mysql? @@ -17,7 +18,7 @@ def postgresql? end def sqlite? - [:sqlite3, :sqlite].include? adapter_name + %i[sqlite3 sqlite].include? adapter_name end end end diff --git a/lib/with_advisory_lock/mysql.rb b/lib/with_advisory_lock/mysql.rb index bdb35e7..9f2a810 100644 --- a/lib/with_advisory_lock/mysql.rb +++ b/lib/with_advisory_lock/mysql.rb @@ -5,7 +5,8 @@ class MySQL < Base # Caches nested lock support by MySQL reported version @@mysql_nl_cache = {} @@mysql_nl_cache_mutex = Mutex.new - # See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock + # See https://dev.mysql.com/doc/refman/5.7/en/locking-functions.html + # See https://dev.mysql.com/doc/refman/8.0/en/locking-functions.html def try_lock raise ArgumentError, 'shared locks are not supported on MySQL' if shared raise ArgumentError, 'transaction level locks are not supported on MySQL' if transaction @@ -18,8 +19,8 @@ def release_lock end def execute_successful?(mysql_function) - sql = "SELECT #{mysql_function} AS #{unique_column_name}" - connection.select_value(sql).to_i.positive? + sql = "SELECT #{mysql_function}" + connection.query_value(sql) == 1 end # MySQL wants a string as the lock key. diff --git a/test/test_helper.rb b/test/test_helper.rb index 3b4065f..f241feb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -18,7 +18,6 @@ ActiveRecord::Base.configurations = { default_env: { url: ENV.fetch('DATABASE_URL', "sqlite3://#{Dir.tmpdir}/with_advisory_lock_test#{RUBY_VERSION}-#{ActiveRecord.gem_version}.sqlite3"), - pool: 11, properties: { allowPublicKeyRetrieval: true } # for JRuby madness } }