Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Data Export Using Migrations

DZone's Guide to

Data Export Using Migrations

·
Free Resource
// ...dumping the data to fixture files, comes in handy:


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('show tables').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
         puts "Writing #{file.to_s}"
      end
   end
end
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}