mirror of https://github.com/rails/rails
Implement db config option `database_tasks: false`
This commit is contained in:
parent
b669e87071
commit
a77dd104ea
|
@ -1,3 +1,24 @@
|
|||
* Add database config option `database_tasks`
|
||||
|
||||
If you would like to connect to an external database without any database
|
||||
mangement tasks such as schema management, migrations, seeds, etc. you can set
|
||||
the per database config option `database_tasks: false`
|
||||
|
||||
```yaml
|
||||
# config/database.yml
|
||||
|
||||
production:
|
||||
primary:
|
||||
database: my_database
|
||||
adapter: mysql2
|
||||
animals:
|
||||
database: my_animals_database
|
||||
adapter: mysql2
|
||||
database_tasks: false
|
||||
```
|
||||
|
||||
*Weston Ganger*
|
||||
|
||||
* Fix `ActiveRecord::InternalMetadata` to not be broken by `config.active_record.record_timestamps = false`
|
||||
|
||||
Since the model always create the timestamp columns, it has to set them, otherwise it breaks
|
||||
|
|
|
@ -48,7 +48,7 @@ module ActiveRecord
|
|||
|
||||
unless include_replicas
|
||||
configs = configs.select do |db_config|
|
||||
!db_config.replica?
|
||||
db_config.database_tasks?
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -113,6 +113,10 @@ module ActiveRecord
|
|||
def schema_dump
|
||||
configuration_hash.fetch(:schema_dump, true)
|
||||
end
|
||||
|
||||
def database_tasks? # :nodoc:
|
||||
!replica? && !!configuration_hash.fetch(:database_tasks, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -178,6 +178,8 @@ module ActiveRecord
|
|||
return if database_configs.count == 1
|
||||
|
||||
database_configs.each do |db_config|
|
||||
next unless db_config.database_tasks?
|
||||
|
||||
yield db_config.name
|
||||
end
|
||||
end
|
||||
|
|
|
@ -122,6 +122,19 @@ module ActiveRecord
|
|||
config = HashConfig.new("default_env", "primary", { schema_dump: false })
|
||||
assert_equal false, config.schema_dump
|
||||
end
|
||||
|
||||
def test_database_tasks_defaults_to_true
|
||||
config = HashConfig.new("default_env", "primary", {})
|
||||
assert_equal true, config.database_tasks?
|
||||
end
|
||||
|
||||
def test_database_tasks_overrides_with_value
|
||||
config = HashConfig.new("default_env", "primary", database_tasks: false)
|
||||
assert_equal false, config.database_tasks?
|
||||
|
||||
config = HashConfig.new("default_env", "primary", database_tasks: "str")
|
||||
assert_equal true, config.database_tasks?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -202,6 +202,24 @@ Note that there is no command for creating the database users, and you'll need t
|
|||
to support the readonly users for your replicas. If you want to create just the animals
|
||||
database you can run `bin/rails db:create:animals`.
|
||||
|
||||
## Connecting to Databases without Managing Schema and Migrations
|
||||
|
||||
If you would like to connect to an external database without any database
|
||||
mangement tasks such as schema management, migrations, seeds, etc. you can set
|
||||
the per database config option `database_tasks: false`. By default it is
|
||||
set to true.
|
||||
|
||||
```yaml
|
||||
production:
|
||||
primary:
|
||||
database: my_database
|
||||
adapter: mysql2
|
||||
animals:
|
||||
database: my_animals_database
|
||||
adapter: mysql2
|
||||
database_tasks: false
|
||||
```
|
||||
|
||||
## Generators and Migrations
|
||||
|
||||
Migrations for multiple databases should live in their own folders prefixed with the
|
||||
|
|
|
@ -1065,6 +1065,45 @@ module ApplicationTests
|
|||
|
||||
db_migrate_and_schema_dump_and_load
|
||||
end
|
||||
|
||||
test "when database_tasks is false, then do not run the database tasks on that db" do
|
||||
app_file "config/database.yml", <<-YAML
|
||||
development:
|
||||
primary:
|
||||
database: db/default.sqlite3
|
||||
adapter: sqlite3
|
||||
animals:
|
||||
database: db/development_animals.sqlite3
|
||||
adapter: sqlite3
|
||||
database_tasks: false
|
||||
schema_dump: true ### database_tasks should override all sub-settings
|
||||
YAML
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
animals_db_exists = lambda{ rails("runner", "puts !!(AnimalsBase.connection rescue false)").strip }
|
||||
|
||||
generate_models_for_animals
|
||||
|
||||
assert_equal "true", animals_db_exists.call
|
||||
|
||||
assert_not File.exist?("db/animals_schema.yml")
|
||||
|
||||
begin
|
||||
assert_raise RuntimeError do
|
||||
rails "db:migrate:animals" ### Task not defined
|
||||
end
|
||||
rescue RuntimeError => e
|
||||
assert_includes e.message, "See the list of available tasks"
|
||||
end
|
||||
|
||||
rails "db:schema:dump"
|
||||
assert_not File.exist?("db/animals_schema.yml")
|
||||
|
||||
rails "db:drop"
|
||||
assert_equal "true", animals_db_exists.call
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue