Skip to content

Add database specific setup and reset tasks for multidb configurations #42938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2021
Merged

Add database specific setup and reset tasks for multidb configurations #42938

merged 1 commit into from
Aug 9, 2021

Conversation

ryanahall
Copy link
Contributor

@ryanahall ryanahall commented Aug 4, 2021

Summary

This is an attempt at enabling database specific setup and reset tasks. We use multiple databases pretty extensively and would like to avoid resetting all databases at once. This change adds database specific setup and reset tasks under each namespace. The default setup and reset tasks remain unchanged aside from their descriptions.

One thing that may be problematic is that the db:seed task is database agnostic, which means that the database scoped tasks will end up seeding everything. Would it be possible to seed only a specific database? At first glance, it doesn't seem like this is the case because the seed files could reference any active record model.

This results in the following tasks for an example configuration with two databases: primary and events.

rails db:reset                                  # Drops and recreates all databases from their schema for the current environment and loads the seeds
rails db:reset:events                           # Drops and recreates the events database from its schema for the current environment and loads the seeds
rails db:reset:primary                          # Drops and recreates the primary database from its schema for the current environment and loads the seeds
rails db:setup                                  # Creates all databases, loads all schemas, and initializes with the seed data (use db:reset to also drop all databases first)
rails db:setup:events                           # Creates the events database, loads the schema, and initializes with the seed data (use db:reset:events to also drop the database first)
rails db:setup:primary                          # Creates the primary database, loads the schema, and initializes with the seed data (use db:reset:primary to also drop the database first)

Other Information

Copy link
Member

@eileencodes eileencodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @ryanahall - can you add some tests in railties/test/application/rake/multi_dbs_test.rb. Active Record also needs a CHANGELOG entry.


ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Creates the #{name} database, loads the schema, and initializes with the seed data (use db:reset:#{name} to also drop the database first)"
task name => ["db:create:#{name}", :environment, "db:schema:load:#{name}", "db:seed"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will seed try to load seeds on all the dbs or just the named ones here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing @eileencodes! This will unfortunately seed all dbs. From my understanding of db/seeds.rb, it can reference any ActiveRecord model and there's no scope to which database will be touched. I don't think there's a way to enforce that a seed file will only touch a single database, but I could be mistaken.

@ryanahall
Copy link
Contributor Author

Thanks for the PR @ryanahall - can you add some tests in railties/test/application/rake/multi_dbs_test.rb. Active Record also needs a CHANGELOG entry.

Sure thing, I'll look into adding some tests and updating the CHANGELOG today. Thanks again!

@rails-bot rails-bot bot added the railties label Aug 4, 2021
@ryanahall ryanahall requested a review from eileencodes August 5, 2021 16:09
@eileencodes
Copy link
Member

Can you squash your commits into one?

@ryanahall
Copy link
Contributor Author

Can you squash your commits into one?

Sure thing @eileencodes, I rebased and squashed my commits.

@eileencodes eileencodes merged commit 843c0a3 into rails:main Aug 9, 2021
@ryanahall ryanahall deleted the rh/multidb-tasks branch August 9, 2021 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants