Skip to content

Commit c066d97

Browse files
committed
cleanup mysql implementation
1 parent 14c40fc commit c066d97

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

Makefile

Lines changed: 7 additions & 4 deletions
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:[email protected]:3306/with_advisory_lock_test appraisal activerecord-7.1 rake test
14+
DATABASE_URL=mysql2://with_advisory:[email protected]:3306/with_advisory_lock_test appraisal rake test
1215

1316

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

lib/with_advisory_lock/database_adapter_support.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# frozen_string_literal: true
1+
c# frozen_string_literal: true
22

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

Lines changed: 4 additions & 3 deletions
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.

0 commit comments

Comments
 (0)