--- databases.rake.orig 2011-03-31 17:01:27.000000000 -0400 +++ databases.rake 2011-04-06 11:27:49.000000000 -0400 @@ -111,6 +111,7 @@ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil) Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby + Rake::Task["db:structure:dump"].invoke if ActiveRecord::Base.schema_format == :sql end namespace :migrate do @@ -134,6 +135,7 @@ raise "VERSION is required" unless version ActiveRecord::Migrator.run(:up, "db/migrate/", version) Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby + Rake::Task["db:structure:dump"].invoke if ActiveRecord::Base.schema_format == :sql end desc 'Runs the "down" for a given migration VERSION.' @@ -142,6 +144,7 @@ raise "VERSION is required" unless version ActiveRecord::Migrator.run(:down, "db/migrate/", version) Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby + Rake::Task["db:structure:dump"].invoke if ActiveRecord::Base.schema_format == :sql end end @@ -150,10 +153,14 @@ step = ENV['STEP'] ? ENV['STEP'].to_i : 1 ActiveRecord::Migrator.rollback('db/migrate/', step) Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby + Rake::Task["db:structure:dump"].invoke if ActiveRecord::Base.schema_format == :sql end - desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.' - task :reset => [ 'db:drop', 'db:setup' ] + desc 'Drops and recreates the database from db/schema.rb or structure.sql for the current environment and loads the seeds.' + task :reset => :environment do + Rake::Task["db:drop"].invoke + Rake::Task["db:setup"].invoke + end desc "Retrieves the charset for the current environment's database" task :charset => :environment do @@ -203,7 +210,12 @@ end desc 'Create the database, load the schema, and initialize with the seed data' - task :setup => [ 'db:create', 'db:schema:load', 'db:seed' ] + task :setup => :environment do + Rake::Task["db:create"].invoke + Rake::Task["db:schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby + Rake::Task["db:structure:load"].invoke if ActiveRecord::Base.schema_format == :sql + Rake::Task["db:seed"].invoke + end desc 'Load the seed data from db/seeds.rb' task :seed => :environment do @@ -276,79 +288,86 @@ case abcs[RAILS_ENV]["adapter"] when /^mysql/, "oci", "oracle" ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) - File.open("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } + File.open("#{RAILS_ROOT}/db/structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } when "postgresql" ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] + ENV['PGUSER'] = abcs[RAILS_ENV]["username"].to_s if abcs[RAILS_ENV]["username"] search_path = abcs[RAILS_ENV]["schema_search_path"] search_path = "--schema=#{search_path}" if search_path - `pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}` + `pg_dump -i -s -x -O -f db/structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}` raise "Error dumping database" if $?.exitstatus == 1 when "sqlite", "sqlite3" dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"] - `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/#{RAILS_ENV}_structure.sql` + `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/structure.sql` when "sqlserver" - `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r` + `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\structure.sql /q /A /r` `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r` when "firebird" set_firebird_env(abcs[RAILS_ENV]) db_string = firebird_db_string(abcs[RAILS_ENV]) - sh "isql -a #{db_string} > #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql" + sh "isql -a #{db_string} > #{RAILS_ROOT}/db/structure.sql" else raise "Task not supported by '#{abcs["test"]["adapter"]}'" end if ActiveRecord::Base.connection.supports_migrations? - File.open("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information } + File.open("#{RAILS_ROOT}/db/structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information } end end - end - - namespace :test do - desc "Recreate the test database from the current schema.rb" - task :load => 'db:test:purge' do - ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) - ActiveRecord::Schema.verbose = false - Rake::Task["db:schema:load"].invoke - end - - desc "Recreate the test database from the current environment's database schema" - task :clone => %w(db:schema:dump db:test:load) - desc "Recreate the test databases from the development structure" - task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do + desc "Load a structure.sql file into the database" + task :load => :environment do abcs = ActiveRecord::Base.configurations - case abcs["test"]["adapter"] + case abcs[RAILS_ENV]["adapter"] when /^mysql/ - ActiveRecord::Base.establish_connection(:test) + ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') - IO.readlines("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table| + IO.readlines("#{RAILS_ROOT}/db/structure.sql").join.split("\n\n").each do |table| ActiveRecord::Base.connection.execute(table) end when "postgresql" - ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"] - ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"] - ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] - `psql -U "#{abcs["test"]["username"]}" -f #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}` + ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] + ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] + ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] + ENV['PGUSER'] = abcs[RAILS_ENV]["username"] if abcs[RAILS_ENV]["username"] + + `psql -f #{RAILS_ROOT}/db/structure.sql #{abcs[RAILS_ENV]["database"]}` when "sqlite", "sqlite3" - dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] - `#{abcs["test"]["adapter"]} #{dbfile} < #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql` + dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"] + `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} < #{RAILS_ROOT}/db/structure.sql` when "sqlserver" - `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` + `osql -E -S #{abcs[RAILS_ENV]["host"]} -d #{abcs[RAILS_ENV]["database"]} -i db\\structure.sql` when "oci", "oracle" - ActiveRecord::Base.establish_connection(:test) - IO.readlines("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl| + ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) + IO.readlines("#{RAILS_ROOT}/db/structure.sql").join.split(";\n\n").each do |ddl| ActiveRecord::Base.connection.execute(ddl) end when "firebird" - set_firebird_env(abcs["test"]) - db_string = firebird_db_string(abcs["test"]) - sh "isql -i #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql #{db_string}" + set_firebird_env(abcs[RAILS_ENV]) + db_string = firebird_db_string(abcs[RAILS_ENV]) + sh "isql -i #{RAILS_ROOT}/db/structure.sql #{db_string}" else - raise "Task not supported by '#{abcs["test"]["adapter"]}'" + raise "Task not supported by '#{abcs[RAILS_ENV]["adapter"]}'" end end + end + + namespace :test do + desc "Recreate the test database from the current schema.rb or structure.sql" + task :load => 'db:test:purge' do + ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) + ActiveRecord::Schema.verbose = false + Rake::Task["db:schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby + Rake::Task["db:structure:load"].invoke if ActiveRecord::Base.schema_format == :sql + end + + desc "Recreate the test database from the current environment's database schema" + task :clone => %w(db:schema:dump db:test:load) + + desc "Recreate the test database from the structure.sql file" + task :clone_structure => [ "db:structure:dump", "db:test:load" ] desc "Empty the test database" task :purge => :environment do @@ -367,7 +386,7 @@ when "sqlserver" dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-') `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}` - `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` + `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\structure.sql` when "oci", "oracle" ActiveRecord::Base.establish_connection(:test) ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|