Namespacing ActiveRecord rake tasks for multiple databases

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).

As more tasks need to support multiple databases, we’re going to wind up with a lot of duplication in 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?

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 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.

1 Like

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