Skip to content

Instantly share code, notes, and snippets.

@wycleffsean
Created September 1, 2016 17:25
Show Gist options
  • Save wycleffsean/cf8d110f93d0857a90665a8a921a2d31 to your computer and use it in GitHub Desktop.
Save wycleffsean/cf8d110f93d0857a90665a8a921a2d31 to your computer and use it in GitHub Desktop.
Dump entire database into seeds
namespace :db do
task dump_seeds: :environment do
Rails.application.eager_load!
path = 'db/fixtures/gen/%s.rb'
ObjectSpace.each_object(Class)
.select{|c| c < ActiveRecord::Base && !c.abstract_class? }
.each do |c|
# skip if it's a view
next if c.new.readonly?
next unless c.attribute_names.include?('id')
filename = c.name.underscore.gsub('/','_')
begin
SeedFu::Writer.write(path % filename, class_name: c.name, constraints: [:id]) do |writer|
c.find_each do |model|
writer.add model.attributes
end
end
rescue ActiveRecord::StatementInvalid => e
# probably a view we missed
puts e
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment