diff options
8 files changed, 32 insertions, 44 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index a62651daff..c2972a3b83 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -360,6 +360,10 @@ module ActiveRecord def enable_extension(name) end + def advisory_locks_enabled? # :nodoc: + supports_advisory_locks? && @advisory_locks_enabled + end + # This is meant to be implemented by the adapters that support advisory # locks # diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index ad045f85ef..88fff83a9e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -111,7 +111,7 @@ module ActiveRecord end def supports_advisory_locks? - @advisory_locks_enabled + true end def get_advisory_lock(lock_name, timeout = 0) # :nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 30e651ee63..3ee344a249 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -298,7 +298,7 @@ module ActiveRecord end def supports_advisory_locks? - @advisory_locks_enabled + true end def supports_explain? diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index d68ed3cad9..ea53324829 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -1357,7 +1357,7 @@ module ActiveRecord end def use_advisory_lock? - Base.connection.supports_advisory_locks? + Base.connection.advisory_locks_enabled? end def with_advisory_lock diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 1c461a0459..a93e5e2b40 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "cases/helper" +require "support/connection_helper" require "models/book" require "models/post" require "models/author" @@ -446,3 +447,27 @@ module ActiveRecord end end end + +if ActiveRecord::Base.connection.supports_advisory_locks? + class AdvisoryLocksEnabledTest < ActiveRecord::TestCase + include ConnectionHelper + + def test_advisory_locks_enabled? + assert ActiveRecord::Base.connection.advisory_locks_enabled? + + run_without_connection do |orig_connection| + ActiveRecord::Base.establish_connection( + orig_connection.merge(advisory_locks: false) + ) + + assert_not ActiveRecord::Base.connection.advisory_locks_enabled? + + ActiveRecord::Base.establish_connection( + orig_connection.merge(advisory_locks: true) + ) + + assert ActiveRecord::Base.connection.advisory_locks_enabled? + end + end + end +end diff --git a/activerecord/test/cases/adapters/helpers/test_supports_advisory_locks.rb b/activerecord/test/cases/adapters/helpers/test_supports_advisory_locks.rb deleted file mode 100644 index 4905e17725..0000000000 --- a/activerecord/test/cases/adapters/helpers/test_supports_advisory_locks.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require "support/connection_helper" - -module TestSupportsAdvisoryLocks - include ConnectionHelper - - def test_supports_advisory_locks? - assert ActiveRecord::Base.connection.supports_advisory_locks? - - run_without_connection do |orig_connection| - ActiveRecord::Base.establish_connection( - orig_connection.merge(advisory_locks: false) - ) - - assert_not ActiveRecord::Base.connection.supports_advisory_locks? - - ActiveRecord::Base.establish_connection( - orig_connection.merge(advisory_locks: true) - ) - - assert ActiveRecord::Base.connection.supports_advisory_locks? - end - end -end diff --git a/activerecord/test/cases/adapters/mysql2/test_advisory_locks_disabled_test.rb b/activerecord/test/cases/adapters/mysql2/test_advisory_locks_disabled_test.rb deleted file mode 100644 index 4857900820..0000000000 --- a/activerecord/test/cases/adapters/mysql2/test_advisory_locks_disabled_test.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -require "cases/helper" -require "cases/adapters/helpers/test_supports_advisory_locks" - -class Mysql2AdvisoryLocksDisabledTest < ActiveRecord::Mysql2TestCase - include TestSupportsAdvisoryLocks -end diff --git a/activerecord/test/cases/adapters/postgresql/advisory_locks_disabled_test.rb b/activerecord/test/cases/adapters/postgresql/advisory_locks_disabled_test.rb deleted file mode 100644 index f14e9baeb9..0000000000 --- a/activerecord/test/cases/adapters/postgresql/advisory_locks_disabled_test.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -require "cases/helper" -require "cases/adapters/helpers/test_supports_advisory_locks" - -class PostgresqlAdvisoryLocksDisabledTest < ActiveRecord::PostgreSQLTestCase - include TestSupportsAdvisoryLocks -end |