Skip to content

Commit

Permalink
feat: use current connnection instead of the one in ActiveRecord::Base (
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros authored Feb 8, 2024
1 parent 2b239cc commit c28a172
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- '3.1'
- '3.0'
- '2.7'
- '3.3'
- 'truffleruby'
rails:
- activerecord_7.1
Expand Down
4 changes: 3 additions & 1 deletion lib/with_advisory_lock/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def already_locked?
def with_advisory_lock_if_needed(&block)
if disable_query_cache
return lock_and_yield do
ActiveRecord::Base.uncached(&block)
connection.uncached do
yield
end
end
end

Expand Down
16 changes: 13 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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

class GemTestCase < ActiveSupport::TestCase

parallelize(workers: 1)
def adapter_support
@adapter_support ||= WithAdvisoryLock::DatabaseAdapterSupport.new(ActiveRecord::Base.connection)
end
def is_sqlite3_adapter?; adapter_support.sqlite?; end
def is_mysql_adapter?; adapter_support.mysql?; end
def is_postgresql_adapter?; adapter_support.postgresql?; end

setup do
ENV['FLOCK_DIR'] = Dir.mktmpdir
ENV['FLOCK_DIR'] = Dir.mktmpdir if is_sqlite3_adapter?
Tag.delete_all
TagAudit.delete_all
Label.delete_all
end

teardown do
FileUtils.remove_entry_secure ENV['FLOCK_DIR']
FileUtils.remove_entry_secure(ENV['FLOCK_DIR'], true) if is_sqlite3_adapter?
end
end

Expand Down
12 changes: 8 additions & 4 deletions test/test_models.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

ActiveRecord::Schema.define(version: 0) do
ActiveRecord::Schema.define(version: 1) do
create_table 'tags', force: true do |t|
t.string 'name'
end
Expand All @@ -12,15 +12,19 @@
end
end

class Tag < ActiveRecord::Base
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

class Tag < ApplicationRecord
after_save do
TagAudit.create(tag_name: name)
Label.create(name: name)
end
end

class TagAudit < ActiveRecord::Base
class TagAudit < ApplicationRecord
end

class Label < ActiveRecord::Base
class Label < ApplicationRecord
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class WithAdvisoryLockConcernTest < GemTestCase

class ActiveRecordQueryCacheTest < GemTestCase
test 'does not disable quary cache by default' do
ActiveRecord::Base.expects(:uncached).never
Tag.connection.expects(:uncached).never
Tag.with_advisory_lock('lock') { Tag.first }
end

test 'can disable ActiveRecord query cache' do
ActiveRecord::Base.expects(:uncached).once
Tag.connection.expects(:uncached).once
Tag.with_advisory_lock('a-lock', disable_query_cache: true) { Tag.first }
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(name, use_advisory_lock)

def work_later
sleep
ActiveRecord::Base.connection_pool.with_connection do
ApplicationRecord.connection_pool.with_connection do
if @use_advisory_lock
Tag.with_advisory_lock(@name) { work }
else
Expand Down Expand Up @@ -46,11 +46,11 @@ def run_workers
workers.each(&:join)
end
# Ensure we're still connected:
ActiveRecord::Base.connection_pool.connection
ApplicationRecord.connection_pool.connection
end

setup do
ActiveRecord::Base.connection.reconnect!
ApplicationRecord.connection.reconnect!
@workers = 10
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def cleanup!
private

def work
ActiveRecord::Base.connection_pool.with_connection do
ApplicationRecord.connection_pool.with_connection do
Tag.with_advisory_lock('test', timeout_seconds: 0, shared: @shared) do
@locked = true
sleep 0.01 until @cleanup
Expand Down Expand Up @@ -117,7 +117,7 @@ class PostgreSQLTest < SupportedEnvironmentTest
end

def pg_lock_modes
ActiveRecord::Base.connection.select_values("SELECT mode FROM pg_locks WHERE locktype = 'advisory';")
ApplicationRecord.connection.select_values("SELECT mode FROM pg_locks WHERE locktype = 'advisory';")
end

test 'allows shared lock to be upgraded to an exclusive lock' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SeparateThreadTest < GemTestCase
@t1_return_value = nil

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

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

teardown do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PostgresqlTest < TransactionScopingTest
setup do
skip unless env_db == :postgresql
@pg_lock_count = lambda do
ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM pg_locks WHERE locktype = 'advisory';").to_i
ApplicationRecord.connection.select_value("SELECT COUNT(*) FROM pg_locks WHERE locktype = 'advisory';").to_i
end
end

Expand Down

0 comments on commit c28a172

Please sign in to comment.