Ruby on Rails Rake Task2006-06-08
Posted by lamer0
This task will let you dump your Postgresql db into fixtures for rails application testing. I don't know why the Rails developers did not think of this. This task enables you to test against a production db. This prevents wasting your time testing against basic 'hello world' data in fixtures. Place dump_fixtures.rake into lib/tasks of your rails project
desc 'Dump a database to yaml fixtures. Set environment variables DB
and DEST to specify the target database and destination path for the
fixtures. DB defaults to development and DEST defaults to RAILS_ROOT/
test/fixtures.'
task :dump_fixtures => :environment do
path = ENV['DEST'] || "#{RAILS_ROOT}/test/fixtures"
db = ENV['DB'] || 'development'
sql = 'SELECT * FROM %s'
ActiveRecord::Base.establish_connection(db)
ActiveRecord::Base.connection.select_values('select table_name from information_schema.tables where table_type like \'BASE TABLE\' and table_schema like \'public\'').each do |table_name|
i = '000'
File.open("#{path}/#{table_name}.yml", 'wb') do |file|
file.write ActiveRecord::Base.connection.select_all(sql %
table_name).inject({}) { |hash, record|
hash["#{table_name}_#{i.succ!}"] = record
hash
}.to_yaml
end
end
end
Read comments on this article. (5) or Comment on this article.