Skip to content

Commit

Permalink
Update schema/structure dump tasks for multi db
Browse files Browse the repository at this point in the history
Adds the ability to dump the schema or structure files for mulitple
databases. Loops through the configs for a given env and sets a filename
based on the format, then establishes a connection for that config and
dumps into the file.
  • Loading branch information
eileencodes committed Mar 21, 2018
1 parent 5eb4488 commit 0f0aa6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
32 changes: 20 additions & 12 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,15 @@ db_namespace = namespace :db do
desc "Creates a db/schema.rb file that is portable against any DB supported by Active Record"
task dump: :load_config do
require "active_record/schema_dumper"
filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema.rb")
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)

ActiveRecord::Base.configs_for(Rails.env) do |spec_name, config|
filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(spec_name, :ruby)
File.open(filename, "w:utf-8") do |file|
ActiveRecord::Base.establish_connection(config)
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end

db_namespace["schema:dump"].reenable
end

Expand All @@ -304,22 +309,25 @@ db_namespace = namespace :db do
rm_f filename, verbose: false
end
end

end

namespace :structure do
desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
task dump: :load_config do
filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql")
current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename)

if ActiveRecord::SchemaMigration.table_exists?
File.open(filename, "a") do |f|
f.puts ActiveRecord::Base.connection.dump_schema_information
f.print "\n"
ActiveRecord::Base.configs_for(Rails.env) do |spec_name, config|
ActiveRecord::Base.establish_connection(config)
filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(spec_name, :sql)
current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
ActiveRecord::Tasks::DatabaseTasks.structure_dump(config, filename)

if ActiveRecord::SchemaMigration.table_exists?
File.open(filename, "a") do |f|
f.puts ActiveRecord::Base.connection.dump_schema_information
f.print "\n"
end
end
end

db_namespace["structure:dump"].reenable
end

Expand Down
18 changes: 16 additions & 2 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,28 @@ def load_schema(configuration, format = ActiveRecord::Base.schema_format, file =
end

def schema_file(format = ActiveRecord::Base.schema_format)
File.join(db_dir, schema_file_type(format))
end

def schema_file_type(format = ActiveRecord::Base.schema_format)
case format
when :ruby
File.join(db_dir, "schema.rb")
"schema.rb"
when :sql
File.join(db_dir, "structure.sql")
"structure.sql"
end
end

def dump_filename(namespace, format = ActiveRecord::Base.schema_format)
filename = if namespace == "primary"
schema_file_type(format)
else
"#{namespace}_#{schema_file_type(format)}"
end

ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
end

def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
each_current_configuration(environment) { |configuration, spec_name, env|
load_schema configuration, format, file, env
Expand Down

0 comments on commit 0f0aa6a

Please sign in to comment.