Add database config option `database_tasks: false` by westonganger · Pull Reques...
source link: https://github.com/rails/rails/pull/42794
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Conversation
Attempting to solve #42787
WIP. This is probably a reasonable start. Need help with testing. Please let me know if anyone wants to pick up the torch or help out.
I believe the tests for this need to be added within activerecord/test/cases/tasks/database_tasks_test.rb
though im not sure exactly how to go about this.
@@ -103,6 +103,14 @@ def adapter
def schema_cache_path
configuration_hash[:schema_cache_path]
end
def database_tasks
ghiculescu on Jul 15
Member
I think this would give a better idea of the method's purpose:
eileencodes on Jul 16
Member
Also I think this should match the other tasks and use fetch
and include replica
since those dont run tasks either: Ex: configuration_hash.fetch(:database_tasks, true) || replica?
westonganger on Jul 16
Author
Contributor
Ok I have made it database_tasks?
and changed the method to use replica and fetch. Doesnt look exactly as you described it though, but I think its correct.
There's one other place that needs to be updated - the configs_for
method isn't taking the database tasks into account and needs to be updated as well.
if db_config.database_tasks
yield db_config.name
else
next ### Skip registering and using this DB's tasks
eileencodes on Jul 16
Member
I'd prefer an early return here like this:
database_configs.each do |db_config| next unless db_config.database_tasks yield db_config.name end
@@ -103,6 +103,14 @@ def adapter
def schema_cache_path
configuration_hash[:schema_cache_path]
end
def database_tasks
eileencodes on Jul 16
Member
Also I think this should match the other tasks and use fetch
and include replica
since those dont run tasks either: Ex: configuration_hash.fetch(:database_tasks, true) || replica?
@@ -196,6 +196,21 @@ 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 use the per database config option `database_tasks: false` by default it is considered true.
Ok thanks for the tip regarding configs_for
I have now updated that along with the other commented items.
I left some comments. This also needs a CHANGELOG entry in Active Record. This also needs a test confirming that tasks aren't run on configs with database_tasks: false
(for example on db:migrate
). You can add a test for that in railties/test//application/rake/multi_dbs_test.rb.
def test_database_tasks_defaults_to_true
config = HashConfig.new("default_env", "primary", {})
assert_equal config.database_tasks, true
eileencodes on Jul 20
Member
These asserts are backwards they should be written as assert_equal true, config.database_tasks
.
config = HashConfig.new("default_env", "primary", database_tasks: false)
assert_equal config.database_tasks, false
config = HashConfig.new("default_env", "primary", database_tasks: 'str')
eileencodes on Jul 20
Member
This should use double quotes.
```yaml
production:
primary:
database: my_animals_database
eileencodes on Jul 20
Member
primary
and animals
shouldn't have the same db name.
@@ -103,6 +103,10 @@ def adapter
def schema_cache_path
configuration_hash[:schema_cache_path]
end
def 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 use
the per database config option `database_tasks: false` by default it is
eileencodes on Jul 20
Member
If you would like to connect to an external database without any database
mangement tasks such as schema management, migrations, seeds, etc. you can use
the per database config option `database_tasks: false` by default it is
considered true.
## 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 use
eileencodes on Jul 20
Member
force-pushed the
westonganger:multi_db_config_database_tasks_option
@eileencodes I have made the requested changes.
I have also added a better test, however I am getting an test failure that I am not expecting. Maybe my check for database existence is not appropriate?
Its currently failing on Line 116
Checking for the connection isn't going to be a good way to test whether the db exists because ActiveRecord::Base.connection
will always exist, it's the default. Since you are really only testing that the tasks are skipped for animals, you can remove the connection checks for the primary. I also think the test can be simplified a bit - we don't need to test every rake task, just a few.
changed the title
[WIP] Add database config option database_tasks: false
Add database config option database_tasks: false
@eileencodes ok I have updated the test and its now passing.
No one assigned
None yet
No milestone
Successfully merging this pull request may close these issues.
None yet
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK