Skip to content

Commit

Permalink
cleanup mysql implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros committed Feb 11, 2024
1 parent 14c40fc commit 3b50594
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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:[email protected]:3306/with_advisory_lock_test appraisal activerecord-7.1 rake test
DATABASE_URL=mysql2://with_advisory:[email protected]:3306/with_advisory_lock_test appraisal rake test


test: test-pg test-mysql
test: test-sqlite test-pg test-mysql
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ services:
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
MYSQL_ROOT_HOST: '%'
ports:
- "3306:3306"
- "3306:3306"
5 changes: 3 additions & 2 deletions lib/with_advisory_lock/database_adapter_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -17,7 +18,7 @@ def postgresql?
end

def sqlite?
[:sqlite3, :sqlite].include? adapter_name
%i[sqlite3 sqlite].include? adapter_name
end
end
end
7 changes: 4 additions & 3 deletions lib/with_advisory_lock/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 3b50594

Please sign in to comment.