提交 0f0aa6a2 编写于 作者: E eileencodes

Update schema/structure dump tasks for multi db

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.
上级 5eb4488d
...@@ -274,10 +274,15 @@ db_namespace = namespace :db do ...@@ -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" desc "Creates a db/schema.rb file that is portable against any DB supported by Active Record"
task dump: :load_config do task dump: :load_config do
require "active_record/schema_dumper" 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::Base.configs_for(Rails.env) do |spec_name, config|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file) 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 end
db_namespace["schema:dump"].reenable db_namespace["schema:dump"].reenable
end end
...@@ -304,22 +309,25 @@ db_namespace = namespace :db do ...@@ -304,22 +309,25 @@ db_namespace = namespace :db do
rm_f filename, verbose: false rm_f filename, verbose: false
end end
end end
end end
namespace :structure do namespace :structure do
desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql" desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
task dump: :load_config do task dump: :load_config do
filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql") ActiveRecord::Base.configs_for(Rails.env) do |spec_name, config|
current_config = ActiveRecord::Tasks::DatabaseTasks.current_config ActiveRecord::Base.establish_connection(config)
ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename) filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(spec_name, :sql)
current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
if ActiveRecord::SchemaMigration.table_exists? ActiveRecord::Tasks::DatabaseTasks.structure_dump(config, filename)
File.open(filename, "a") do |f|
f.puts ActiveRecord::Base.connection.dump_schema_information if ActiveRecord::SchemaMigration.table_exists?
f.print "\n" File.open(filename, "a") do |f|
f.puts ActiveRecord::Base.connection.dump_schema_information
f.print "\n"
end
end end
end end
db_namespace["structure:dump"].reenable db_namespace["structure:dump"].reenable
end end
......
...@@ -252,14 +252,28 @@ def load_schema(configuration, format = ActiveRecord::Base.schema_format, file = ...@@ -252,14 +252,28 @@ def load_schema(configuration, format = ActiveRecord::Base.schema_format, file =
end end
def schema_file(format = ActiveRecord::Base.schema_format) 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 case format
when :ruby when :ruby
File.join(db_dir, "schema.rb") "schema.rb"
when :sql when :sql
File.join(db_dir, "structure.sql") "structure.sql"
end end
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) def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
each_current_configuration(environment) { |configuration, spec_name, env| each_current_configuration(environment) { |configuration, spec_name, env|
load_schema configuration, format, file, env load_schema configuration, format, file, env
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册