Skip to content

Commit

Permalink
Remove duplicate code in dump task
Browse files Browse the repository at this point in the history
The `_dump` task is private so it shouldn't be called by apps. Because
of this were establishing a connection to the db_config twice, once in
`_dump` and once in `dump`. There's no reason to do this since `_dump`
always calls `dump` and apps should not be calling `_dump`.
  • Loading branch information
eileencodes committed Jul 19, 2022
1 parent a731c92 commit 31ab9cf
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions activerecord/lib/active_record/railties/databases.rake
Expand Up @@ -121,10 +121,7 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
# IMPORTANT: This task won't dump the schema if ActiveRecord.dump_schema_after_migration is set to false
task name do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name)

if ActiveRecord.dump_schema_after_migration && db_config.schema_dump
ActiveRecord::Base.establish_connection(db_config)
if ActiveRecord.dump_schema_after_migration
db_namespace["schema:dump:#{name}"].invoke
end

Expand Down Expand Up @@ -477,9 +474,14 @@ db_namespace = namespace :db do
desc "Creates a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) for #{name} database"
task name => :load_config do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name)
ActiveRecord::Base.establish_connection(db_config)
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, schema_format)

if db_config.schema_dump
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym

ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, schema_format)
end

db_namespace["schema:dump:#{name}"].reenable
end
end
Expand Down

0 comments on commit 31ab9cf

Please sign in to comment.