Skip to content

Commit 3b50594

Browse files
committedFeb 11, 2024·
cleanup mysql implementation
1 parent 14c40fc commit 3b50594

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed
 

‎Makefile

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
.PHONY: test-pg test-mysql
1+
.PHONY: test-sqlite test-pg test-mysql
2+
3+
test-sqlite:
4+
appraisal rake test
25

36
test-pg:
47
docker compose up -d pg
58
sleep 10 # give some time for the service to start
6-
DATABASE_URL=postgres://with_advisory:with_advisory_pass@localhost/with_advisory_lock_test appraisal activerecord-7.1 rake test
9+
DATABASE_URL=postgres://with_advisory:with_advisory_pass@localhost/with_advisory_lock_test appraisal rake test
710

811
test-mysql:
912
docker compose up -d mysql
1013
sleep 10 # give some time for the service to start
11-
DATABASE_URL=mysql2://with_advisory:with_advisory_pass@0.0.0.0:3306/with_advisory_lock_test appraisal activerecord-7.1 rake test
14+
DATABASE_URL=mysql2://with_advisory:with_advisory_pass@0.0.0.0:3306/with_advisory_lock_test appraisal rake test
1215

1316

14-
test: test-pg test-mysql
17+
test: test-sqlite test-pg test-mysql

‎docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ services:
1717
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
1818
MYSQL_ROOT_HOST: '%'
1919
ports:
20-
- "3306:3306"
20+
- "3306:3306"

‎lib/with_advisory_lock/database_adapter_support.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
module WithAdvisoryLock
44
class DatabaseAdapterSupport
55
attr_reader :adapter_name
6+
67
def initialize(connection)
78
@connection = connection
8-
@adapter_name = connection.adapter_name.downcase.to_sym
9+
@adapter_name = connection.adapter_name.downcase.to_sym
910
end
1011

1112
def mysql?
@@ -17,7 +18,7 @@ def postgresql?
1718
end
1819

1920
def sqlite?
20-
[:sqlite3, :sqlite].include? adapter_name
21+
%i[sqlite3 sqlite].include? adapter_name
2122
end
2223
end
2324
end

‎lib/with_advisory_lock/mysql.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class MySQL < Base
55
# Caches nested lock support by MySQL reported version
66
@@mysql_nl_cache = {}
77
@@mysql_nl_cache_mutex = Mutex.new
8-
# See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
8+
# See https://dev.mysql.com/doc/refman/5.7/en/locking-functions.html
9+
# See https://dev.mysql.com/doc/refman/8.0/en/locking-functions.html
910
def try_lock
1011
raise ArgumentError, 'shared locks are not supported on MySQL' if shared
1112
raise ArgumentError, 'transaction level locks are not supported on MySQL' if transaction
@@ -18,8 +19,8 @@ def release_lock
1819
end
1920

2021
def execute_successful?(mysql_function)
21-
sql = "SELECT #{mysql_function} AS #{unique_column_name}"
22-
connection.select_value(sql).to_i.positive?
22+
sql = "SELECT #{mysql_function}"
23+
connection.query_value(sql) == 1
2324
end
2425

2526
# MySQL wants a string as the lock key.

‎test/test_helper.rb

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
ActiveRecord::Base.configurations = {
1919
default_env: {
2020
url: ENV.fetch('DATABASE_URL', "sqlite3://#{Dir.tmpdir}/with_advisory_lock_test#{RUBY_VERSION}-#{ActiveRecord.gem_version}.sqlite3"),
21-
pool: 11,
2221
properties: { allowPublicKeyRetrieval: true } # for JRuby madness
2322
}
2423
}

0 commit comments

Comments
 (0)