Skip to content

Commit c28a172

Browse files
authored
feat: use current connnection instead of the one in ActiveRecord::Base (#90)
1 parent 2b239cc commit c28a172

File tree

12 files changed

+35
-18
lines changed

12 files changed

+35
-18
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
- '3.1'
4040
- '3.0'
4141
- '2.7'
42+
- '3.3'
4243
- 'truffleruby'
4344
rails:
4445
- activerecord_7.1

lib/with_advisory_lock/base.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def already_locked?
5656
def with_advisory_lock_if_needed(&block)
5757
if disable_query_cache
5858
return lock_and_yield do
59-
ActiveRecord::Base.uncached(&block)
59+
connection.uncached do
60+
yield
61+
end
6062
end
6163
end
6264

test/test_helper.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
ActiveRecord::Base.configurations = {
1919
default_env: {
20-
url: ENV.fetch('DATABASE_URL', "sqlite3://#{Dir.tmpdir}/#{SecureRandom.hex}.sqlite3"),
20+
url: ENV.fetch('DATABASE_URL', "sqlite3://#{Dir.tmpdir}/with_advisory_lock_test#{RUBY_VERSION}-#{ActiveRecord.gem_version}.sqlite3"),
2121
properties: { allowPublicKeyRetrieval: true } # for JRuby madness
2222
}
2323
}
@@ -38,14 +38,24 @@ def env_db
3838
require 'mocha/minitest'
3939

4040
class GemTestCase < ActiveSupport::TestCase
41+
42+
parallelize(workers: 1)
43+
def adapter_support
44+
@adapter_support ||= WithAdvisoryLock::DatabaseAdapterSupport.new(ActiveRecord::Base.connection)
45+
end
46+
def is_sqlite3_adapter?; adapter_support.sqlite?; end
47+
def is_mysql_adapter?; adapter_support.mysql?; end
48+
def is_postgresql_adapter?; adapter_support.postgresql?; end
49+
4150
setup do
42-
ENV['FLOCK_DIR'] = Dir.mktmpdir
51+
ENV['FLOCK_DIR'] = Dir.mktmpdir if is_sqlite3_adapter?
4352
Tag.delete_all
4453
TagAudit.delete_all
4554
Label.delete_all
4655
end
56+
4757
teardown do
48-
FileUtils.remove_entry_secure ENV['FLOCK_DIR']
58+
FileUtils.remove_entry_secure(ENV['FLOCK_DIR'], true) if is_sqlite3_adapter?
4959
end
5060
end
5161

test/test_models.rb

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

3-
ActiveRecord::Schema.define(version: 0) do
3+
ActiveRecord::Schema.define(version: 1) do
44
create_table 'tags', force: true do |t|
55
t.string 'name'
66
end
@@ -12,15 +12,19 @@
1212
end
1313
end
1414

15-
class Tag < ActiveRecord::Base
15+
class ApplicationRecord < ActiveRecord::Base
16+
self.abstract_class = true
17+
end
18+
19+
class Tag < ApplicationRecord
1620
after_save do
1721
TagAudit.create(tag_name: name)
1822
Label.create(name: name)
1923
end
2024
end
2125

22-
class TagAudit < ActiveRecord::Base
26+
class TagAudit < ApplicationRecord
2327
end
2428

25-
class Label < ActiveRecord::Base
29+
class Label < ApplicationRecord
2630
end

test/concern_test.rb renamed to test/with_advisory_lock/concern_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class WithAdvisoryLockConcernTest < GemTestCase
2222

2323
class ActiveRecordQueryCacheTest < GemTestCase
2424
test 'does not disable quary cache by default' do
25-
ActiveRecord::Base.expects(:uncached).never
25+
Tag.connection.expects(:uncached).never
2626
Tag.with_advisory_lock('lock') { Tag.first }
2727
end
2828

2929
test 'can disable ActiveRecord query cache' do
30-
ActiveRecord::Base.expects(:uncached).once
30+
Tag.connection.expects(:uncached).once
3131
Tag.with_advisory_lock('a-lock', disable_query_cache: true) { Tag.first }
3232
end
3333
end
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/parallelism_test.rb renamed to test/with_advisory_lock/parallelism_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def initialize(name, use_advisory_lock)
1515

1616
def work_later
1717
sleep
18-
ActiveRecord::Base.connection_pool.with_connection do
18+
ApplicationRecord.connection_pool.with_connection do
1919
if @use_advisory_lock
2020
Tag.with_advisory_lock(@name) { work }
2121
else
@@ -46,11 +46,11 @@ def run_workers
4646
workers.each(&:join)
4747
end
4848
# Ensure we're still connected:
49-
ActiveRecord::Base.connection_pool.connection
49+
ApplicationRecord.connection_pool.connection
5050
end
5151

5252
setup do
53-
ActiveRecord::Base.connection.reconnect!
53+
ApplicationRecord.connection.reconnect!
5454
@workers = 10
5555
end
5656

test/shared_test.rb renamed to test/with_advisory_lock/shared_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def cleanup!
2424
private
2525

2626
def work
27-
ActiveRecord::Base.connection_pool.with_connection do
27+
ApplicationRecord.connection_pool.with_connection do
2828
Tag.with_advisory_lock('test', timeout_seconds: 0, shared: @shared) do
2929
@locked = true
3030
sleep 0.01 until @cleanup
@@ -117,7 +117,7 @@ class PostgreSQLTest < SupportedEnvironmentTest
117117
end
118118

119119
def pg_lock_modes
120-
ActiveRecord::Base.connection.select_values("SELECT mode FROM pg_locks WHERE locktype = 'advisory';")
120+
ApplicationRecord.connection.select_values("SELECT mode FROM pg_locks WHERE locktype = 'advisory';")
121121
end
122122

123123
test 'allows shared lock to be upgraded to an exclusive lock' do

test/thread_test.rb renamed to test/with_advisory_lock/thread_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class SeparateThreadTest < GemTestCase
1010
@t1_return_value = nil
1111

1212
@t1 = Thread.new do
13-
ActiveRecord::Base.connection_pool.with_connection do
13+
ApplicationRecord.connection_pool.with_connection do
1414
@t1_return_value = Label.with_advisory_lock(@lock_name) do
1515
@mutex.synchronize { @t1_acquired_lock = true }
1616
sleep
@@ -21,7 +21,7 @@ class SeparateThreadTest < GemTestCase
2121

2222
# Wait for the thread to acquire the lock:
2323
sleep(0.1) until @mutex.synchronize { @t1_acquired_lock }
24-
ActiveRecord::Base.connection.reconnect!
24+
ApplicationRecord.connection.reconnect!
2525
end
2626

2727
teardown do

test/transaction_test.rb renamed to test/with_advisory_lock/transaction_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PostgresqlTest < TransactionScopingTest
2525
setup do
2626
skip unless env_db == :postgresql
2727
@pg_lock_count = lambda do
28-
ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM pg_locks WHERE locktype = 'advisory';").to_i
28+
ApplicationRecord.connection.select_value("SELECT COUNT(*) FROM pg_locks WHERE locktype = 'advisory';").to_i
2929
end
3030
end
3131

0 commit comments

Comments
 (0)