aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/tasks/database_tasks.rb
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2019-04-08 15:44:51 -0400
committereileencodes <eileencodes@gmail.com>2019-04-19 13:14:24 -0400
commitf9244c65859c114d4a607823e31173ac4460a6e9 (patch)
treee7279489d298ed7e84b304cb5ae8699eefe0e4d0 /activerecord/lib/active_record/tasks/database_tasks.rb
parent1a5381ff0cf04af68a50bd04f265b9b8199e37b4 (diff)
downloadrails-f9244c65859c114d4a607823e31173ac4460a6e9.tar.gz
rails-f9244c65859c114d4a607823e31173ac4460a6e9.tar.bz2
rails-f9244c65859c114d4a607823e31173ac4460a6e9.zip
Handle up/down for multiple databases
This change adds the ability to run up/down for a database in a multi-db environment. If you have an app with a primary and animals database the following tasks will be generated: ``` VERSION=123 rake db:migrate:up:primary VERSION=123 rake db:migrate:up:primary VERSION=123 rake db:migrate:down:primary VERSION=123 rake db:migrate:up:animals ``` I didn't generate descriptions with them since we don't generate a description for a single database application. In addition to this change I've made it so if your application has multiple databases Rails will raise if you try to run `up` or `down` without a namespace. This is because we don't know which DB you want to run `up` or `down` against unless the app tells us, so it's safer to just block it and recommend using namespaced versions of up/down respectively. The output for the raise looks like: ``` You're using a multiple database application. To use `db:migrate:down` you must run the namespaced task with a VERSION. Available tasks are db:migrate:down:primary and db:migrate:down:animals. ```
Diffstat (limited to 'activerecord/lib/active_record/tasks/database_tasks.rb')
-rw-r--r--activerecord/lib/active_record/tasks/database_tasks.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb
index 53e7e1e6d7..c79ed8db60 100644
--- a/activerecord/lib/active_record/tasks/database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/database_tasks.rb
@@ -155,6 +155,20 @@ module ActiveRecord
end
end
+ def raise_for_multi_db(environment = env, command:)
+ db_configs = ActiveRecord::Base.configurations.configs_for(env_name: environment)
+
+ if db_configs.count > 1
+ dbs_list = []
+
+ db_configs.each do |db|
+ dbs_list << "#{command}:#{db.spec_name}"
+ end
+
+ raise "You're using a multiple database application. To use `#{command}` you must run the namespaced task with a VERSION. Available tasks are #{dbs_list.to_sentence}."
+ end
+ end
+
def create_current(environment = env)
each_current_configuration(environment) { |configuration|
create configuration