From 95c540e4edec1e90238e93ddcf9a99d291bd2238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 29 Dec 2020 22:07:14 +0000 Subject: [PATCH] Extract method to the DatabaseTasks class It make easier to find and elimite duplication inside that class. --- .../lib/active_record/railties/databases.rake | 31 +---------------- .../lib/active_record/tasks/database_tasks.rb | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 504b3eaa15..65c2570d59 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -354,36 +354,7 @@ db_namespace = namespace :db do desc "Runs setup if database does not exist, or runs migrations if it does" task prepare: :load_config do - seed = false - - ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config| - ActiveRecord::Base.establish_connection(db_config) - - # Skipped when no database - ActiveRecord::Tasks::DatabaseTasks.migrate - - if ActiveRecord::Base.dump_schema_after_migration - ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, ActiveRecord::Base.schema_format) - end - rescue ActiveRecord::NoDatabaseError - config_name = db_config.name - ActiveRecord::Tasks::DatabaseTasks.create_current(db_config.env_name, config_name) - - if File.exist?(ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_name)) - ActiveRecord::Tasks::DatabaseTasks.load_schema( - db_config, - ActiveRecord::Base.schema_format, - nil - ) - else - ActiveRecord::Tasks::DatabaseTasks.migrate - end - - seed = true - end - - ActiveRecord::Base.establish_connection - ActiveRecord::Tasks::DatabaseTasks.load_seed if seed + ActiveRecord::Tasks::DatabaseTasks.prepare_all end desc "Loads the seed data from db/seeds.rb" diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 367bd2fd73..40a605106a 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -194,6 +194,39 @@ module ActiveRecord ActiveRecord::Base.establish_connection(environment.to_sym) end + def prepare_all + seed = false + + ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config| + ActiveRecord::Base.establish_connection(db_config) + + # Skipped when no database + ActiveRecord::Tasks::DatabaseTasks.migrate + + if ActiveRecord::Base.dump_schema_after_migration + ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, ActiveRecord::Base.schema_format) + end + rescue ActiveRecord::NoDatabaseError + config_name = db_config.name + ActiveRecord::Tasks::DatabaseTasks.create_current(db_config.env_name, config_name) + + if File.exist?(ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_name)) + ActiveRecord::Tasks::DatabaseTasks.load_schema( + db_config, + ActiveRecord::Base.schema_format, + nil + ) + else + ActiveRecord::Tasks::DatabaseTasks.migrate + end + + seed = true + end + + ActiveRecord::Base.establish_connection + ActiveRecord::Tasks::DatabaseTasks.load_seed if seed + end + def drop(configuration, *arguments) db_config = resolve_configuration(configuration) database_adapter_for(db_config, *arguments).drop