aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDieter Komendera <dieter@komendera.com>2011-12-16 15:59:19 +0100
committerDieter Komendera <dieter@komendera.com>2011-12-16 16:15:16 +0100
commitaa92d3732cdc75c4b5788233cb86148c04871d0d (patch)
treec974ea453e7934c103963751793e81b0f8f0758d
parent7b1eeea5bc69747b5bfde2af2c7245339da2be74 (diff)
downloadrails-aa92d3732cdc75c4b5788233cb86148c04871d0d.tar.gz
rails-aa92d3732cdc75c4b5788233cb86148c04871d0d.tar.bz2
rails-aa92d3732cdc75c4b5788233cb86148c04871d0d.zip
Make structure file configureable in db:structure:dump analog to SCHEMA in db:schema dump and clean up a bit.
-rw-r--r--activerecord/lib/active_record/railties/databases.rake26
1 files changed, 14 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake
index 0aafb5184f..12ef100aa6 100644
--- a/activerecord/lib/active_record/railties/databases.rake
+++ b/activerecord/lib/active_record/railties/databases.rake
@@ -374,33 +374,34 @@ db_namespace = namespace :db do
desc 'Dump the database structure to an SQL file'
task :dump => :environment do
abcs = ActiveRecord::Base.configurations
+ filename = ENV['STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
case abcs[Rails.env]['adapter']
when /mysql/, 'oci', 'oracle'
ActiveRecord::Base.establish_connection(abcs[Rails.env])
- File.open("#{Rails.root}/db/structure.sql", "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
+ File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
when /postgresql/
set_psql_env(abcs[Rails.env])
search_path = abcs[Rails.env]['schema_search_path']
unless search_path.blank?
search_path = search_path.split(",").map{|search_path_part| "--schema=#{search_path_part.strip}" }.join(" ")
end
- `pg_dump -i -s -x -O -f db/structure.sql #{search_path} #{abcs[Rails.env]['database']}`
+ `pg_dump -i -s -x -O -f #{filename} #{search_path} #{abcs[Rails.env]['database']}`
raise 'Error dumping database' if $?.exitstatus == 1
when /sqlite/
dbfile = abcs[Rails.env]['database']
- `sqlite3 #{dbfile} .schema > db/structure.sql`
+ `sqlite3 #{dbfile} .schema > #{filename}`
when 'sqlserver'
- `smoscript -s #{abcs[Rails.env]['host']} -d #{abcs[Rails.env]['database']} -u #{abcs[Rails.env]['username']} -p #{abcs[Rails.env]['password']} -f db\\structure.sql -A -U`
+ `smoscript -s #{abcs[Rails.env]['host']} -d #{abcs[Rails.env]['database']} -u #{abcs[Rails.env]['username']} -p #{abcs[Rails.env]['password']} -f #{filename} -A -U`
when "firebird"
set_firebird_env(abcs[Rails.env])
db_string = firebird_db_string(abcs[Rails.env])
- sh "isql -a #{db_string} > #{Rails.root}/db/structure.sql"
+ sh "isql -a #{db_string} > #{filename}"
else
raise "Task not supported by '#{abcs[Rails.env]["adapter"]}'"
end
if ActiveRecord::Base.connection.supports_migrations?
- File.open("#{Rails.root}/db/structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
+ File.open(filename, "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
end
end
@@ -409,30 +410,31 @@ db_namespace = namespace :db do
env = ENV['RAILS_ENV'] || 'test'
abcs = ActiveRecord::Base.configurations
+ filename = ENV['STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
case abcs[env]['adapter']
when /mysql/
ActiveRecord::Base.establish_connection(abcs[env])
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
- IO.read("#{Rails.root}/db/structure.sql").split("\n\n").each do |table|
+ IO.read(filename).split("\n\n").each do |table|
ActiveRecord::Base.connection.execute(table)
end
when /postgresql/
set_psql_env(abcs[env])
- `psql -f "#{Rails.root}/db/structure.sql" #{abcs[env]['database']} #{abcs[env]['template']}`
+ `psql -f "#{filename}" #{abcs[env]['database']} #{abcs[env]['template']}`
when /sqlite/
dbfile = abcs[env]['database']
- `sqlite3 #{dbfile} < "#{Rails.root}/db/structure.sql"`
+ `sqlite3 #{dbfile} < "#{filename}"`
when 'sqlserver'
- `sqlcmd -S #{abcs[env]['host']} -d #{abcs[env]['database']} -U #{abcs[env]['username']} -P #{abcs[env]['password']} -i db\\structure.sql`
+ `sqlcmd -S #{abcs[env]['host']} -d #{abcs[env]['database']} -U #{abcs[env]['username']} -P #{abcs[env]['password']} -i #{filename}`
when 'oci', 'oracle'
ActiveRecord::Base.establish_connection(abcs[env])
- IO.read("#{Rails.root}/db/structure.sql").split(";\n\n").each do |ddl|
+ IO.read(filename).split(";\n\n").each do |ddl|
ActiveRecord::Base.connection.execute(ddl)
end
when 'firebird'
set_firebird_env(abcs[env])
db_string = firebird_db_string(abcs[env])
- sh "isql -i #{Rails.root}/db/structure.sql #{db_string}"
+ sh "isql -i #{filename} #{db_string}"
else
raise "Task not supported by '#{abcs[env]['adapter']}'"
end