# Namespacing ActiveRecord rake tasks for multiple databases

**URL:** <https://discuss.rubyonrails.org/t/namespacing-activerecord-rake-tasks-for-multiple-databases/74163>\
**Category:** rubyonrails-core\
**Created:** [April 13, 2020, 4:54am UTC](https://discuss.rubyonrails.org/t/namespacing-activerecord-rake-tasks-for-multiple-databases/74163 "2020-04-13T04:54:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzunk](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/dzunk/32/2051_2.png) [@dzunk](https://discuss.rubyonrails.org/u/dzunk)\
**Post date:** [April 13, 2020, 4:54am UTC](https://discuss.rubyonrails.org/t/namespacing-activerecord-rake-tasks-for-multiple-databases/74163/1 "2020-04-13T04:54:58Z")

</div>

With multiple databases configured, working with the rake tasks provided by ActiveRecord can be a little confusing. Some of the tasks support an optional database name suffix (e.g. `db:migrate:primary`), some don’t (e.g. `db:schema:load`), and some now require it (e.g. `db:rollback` as of [#38770](https://github.com/rails/rails/pull/38770)).

As more tasks need to support multiple databases, we’re going to wind up with a lot of duplication in [databases.rake](https://github.com/rails/rails/blob/v6.0.2.2/activerecord/lib/active_record/railties/databases.rake) - there are already seven instances of `DatabaseTasks.for_each(databases)`. I originally started looking into this because I needed to do a `db:schema:load` for only one particular database, so I’d say there’s probably a need to extend multi-db support to more, if not all, of the tasks.

Looking at #38770 above, it seems like we might be OK with breaking changes in the tasks, so I wanted to suggest moving the database name to the second chunk of the task name (`db:primary:migrate` instead of `db:migrate:primary`) and raising an error for any use of non-namespaced tasks when multi-db support is enabled. This would let us define all the tasks inside a single loop, and also define all tasks for each database, making the interface consistent.

cc: @eileencodes - the multi-db expert. WDYT?

---

<div class="post-metadata">

**Author:** ![eileencodes](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/eileencodes/32/927_2.png) [@eileencodes](https://discuss.rubyonrails.org/u/eileencodes)\
**Post date:** [April 13, 2020, 11:53am UTC](https://discuss.rubyonrails.org/t/namespacing-activerecord-rake-tasks-for-multiple-databases/74163/2 "2020-04-13T11:53:20Z")

</div>

Hey @dzunk,

Thanks for the writeup.

I’m a 100% against changing the order of the Rake tasks from `db:migrate:primary` to `db:primary:migrate` as it will result in cleaner code for Rails but no benefit for the user. It would be a huge pain to communicate the changed tasks and a long deprecation cycle with little benefit. I purposefully named the tasks the way I did so they read like English, “database migrate the primary one”.

[Handle db:rollback and db:rollback:[NAME] for multi-db apps by eileencodes · Pull Request #38770 · rails/rails · GitHub](https://github.com/rails/rails/pull/38770) doesn’t count as a breaking change, that task never worked with multiple databases and never can and `schema:load` was fixed in [Adds additional database-specific rake tasks for multi-database users by kylekthompson · Pull Request #38449 · rails/rails · GitHub](https://github.com/rails/rails/pull/38449).

---

<div class="post-metadata">

**Author:** ![dzunk](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/dzunk/32/2051_2.png) [@dzunk](https://discuss.rubyonrails.org/u/dzunk)\
**Post date:** [April 13, 2020, 5:02pm UTC](https://discuss.rubyonrails.org/t/namespacing-activerecord-rake-tasks-for-multiple-databases/74163/3 "2020-04-13T17:02:13Z")

</div>

That was fast, thanks for the info. Won’t change anything. Looking forward to 6.1 to take advantage of those new tasks!
