diff options
author | eileencodes <eileencodes@gmail.com> | 2018-03-16 13:56:58 -0400 |
---|---|---|
committer | eileencodes <eileencodes@gmail.com> | 2018-03-21 13:53:24 -0400 |
commit | d79d6867a23b90e0f9e2670a3300c9a044c84dca (patch) | |
tree | 382f8326848c0e98ba609f54bb24527f9f85a161 | |
parent | 1756094b22bf81f15ffdfdb5208075b58c45296f (diff) | |
download | rails-d79d6867a23b90e0f9e2670a3300c9a044c84dca.tar.gz rails-d79d6867a23b90e0f9e2670a3300c9a044c84dca.tar.bz2 rails-d79d6867a23b90e0f9e2670a3300c9a044c84dca.zip |
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
```
-rw-r--r-- | activerecord/lib/active_record/railties/databases.rake | 25 |
1 files changed, 25 insertions, 0 deletions
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? |