1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Extract method to the DatabaseTasks class

It make easier to find and elimite duplication inside that class.
This commit is contained in:
Rafael Mendonça França 2020-12-29 22:07:14 +00:00
parent 936833fa3e
commit 95c540e4ed
No known key found for this signature in database
GPG key ID: FC23B6D0F1EEE948
2 changed files with 34 additions and 30 deletions

View file

@ -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"

View file

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