Merge pull request #33691 from tgxworld/add_config_to_disable_advisory_locks

Add database configuration to disable advisory locks.
This commit is contained in:
Matthew Draper 2018-08-23 01:04:14 +09:30 committed by GitHub
commit 24f6bf0d96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 4 deletions

@ -1,3 +1,13 @@
* Add database configuration to disable advisory locks.
```
production:
adapter: postgresql
advisory_locks: false
```
*Guo Xiang*
* SQLite3 adapter `alter_table` method restores foreign keys.
*Yasuo Honda*

@ -77,7 +77,14 @@ class AbstractAdapter
SIMPLE_INT = /\A\d+\z/
attr_accessor :visitor, :pool
attr_reader :schema_cache, :owner, :logger, :prepared_statements, :lock
attr_reader :schema_cache,
:owner,
:logger,
:prepared_statements,
:lock,
:advisory_locks
alias :in_use? :owner
def self.type_cast_config_to_integer(config)
@ -119,6 +126,10 @@ def initialize(connection, logger = nil, config = {}) # :nodoc:
else
@prepared_statements = false
end
@advisory_locks_enabled = self.class.type_cast_config_to_boolean(
config.fetch(:advisory_locks, true)
)
end
def migrations_paths # :nodoc:

@ -111,7 +111,7 @@ def supports_virtual_columns?
end
def supports_advisory_locks?
true
@advisory_locks_enabled
end
def get_advisory_lock(lock_name, timeout = 0) # :nodoc:

@ -298,7 +298,7 @@ def supports_ddl_transactions?
end
def supports_advisory_locks?
true
@advisory_locks_enabled
end
def supports_explain?

@ -0,0 +1,25 @@
# 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

@ -0,0 +1,8 @@
# frozen_string_literal: true
require "cases/helper"
require "cases/adapters/helpers/test_supports_advisory_locks"
class Mysql2AdvisoryLocksDisabledTest < ActiveRecord::Mysql2TestCase
include TestSupportsAdvisoryLocks
end

@ -0,0 +1,8 @@
# frozen_string_literal: true
require "cases/helper"
require "cases/adapters/helpers/test_supports_advisory_locks"
class PostgresqlAdvisoryLocksDisabledTest < ActiveRecord::PostgreSQLTestCase
include TestSupportsAdvisoryLocks
end

@ -989,6 +989,14 @@ development:
If your development database has a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the `development` section as appropriate.
Advisory Locks are enabled by default on MySQL and are used to make database migrations concurrent safe. You can disable advisory locks by setting `advisory_locks` to `false`:
```yaml
production:
adapter: mysql2
advisory_locks: false
```
#### Configuring a PostgreSQL Database
If you choose to use PostgreSQL, your `config/database.yml` will be customized to use PostgreSQL databases:
@ -1001,12 +1009,13 @@ development:
pool: 5
```
Prepared Statements are enabled by default on PostgreSQL. You can disable prepared statements by setting `prepared_statements` to `false`:
By default Active Record uses database features like prepared statements and advisory locks. You might need to disable those features if you're using an external connection pooler like PgBouncer:
```yaml
production:
adapter: postgresql
prepared_statements: false
advisory_locks: false
```
If enabled, Active Record will create up to `1000` prepared statements per database connection by default. To modify this behavior you can set `statement_limit` to a different value: