From d79d6867a23b90e0f9e2670a3300c9a044c84dca Mon Sep 17 00:00:00 2001 From: eileencodes Date: Fri, 16 Mar 2018 13:56:58 -0400 Subject: [PATCH] Add create/drop/migrate db tasks for each database in the environment If we have a three-tier yaml file like this: ``` development: primary: database: "development" animals: database: "development_animals" migrations_paths: "db/animals_migrate" ``` This will add db create/drop/and migrate tasks for each level of the config under that environment. ``` bin/rails db:drop:primary bin/rails db:drop:animals bin/rails db:create:primary bin/rails db:create:animals bin/rails db:migrate:primary bin/rails db:migrate:animals ``` --- .../lib/active_record/railties/databases.rake | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 662a8bc720..176e617258 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -22,6 +22,14 @@ db_namespace = namespace :db do task all: :load_config do ActiveRecord::Tasks::DatabaseTasks.create_all end + + databases = Rails.application.config.database_configuration + ActiveRecord::Base.configs_for(Rails.env, databases) do |spec_name, config| + desc "Create #{spec_name} database for current environment" + task spec_name do + ActiveRecord::Tasks::DatabaseTasks.create(config) + end + end end desc "Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to creating the development and test databases." @@ -33,6 +41,14 @@ db_namespace = namespace :db do task all: [:load_config, :check_protected_environments] do ActiveRecord::Tasks::DatabaseTasks.drop_all end + + databases = Rails.application.config.database_configuration + ActiveRecord::Base.configs_for(Rails.env, databases) do |spec_name, config| + desc "Drop #{spec_name} database for current environment" + task spec_name => :check_protected_environments do + ActiveRecord::Tasks::DatabaseTasks.drop(config) + end + end end desc "Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to dropping the development and test databases." @@ -77,6 +93,15 @@ db_namespace = namespace :db do end namespace :migrate do + databases = Rails.application.config.database_configuration + ActiveRecord::Base.configs_for(Rails.env, databases) do |spec_name, config| + desc "Migrate #{spec_name} database for current environment" + task spec_name do + ActiveRecord::Base.establish_connection(config) + ActiveRecord::Tasks::DatabaseTasks.migrate + end + end + # desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).' task redo: :load_config do raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty? -- GitLab