Created
September 1, 2016 17:25
-
-
Save wycleffsean/cf8d110f93d0857a90665a8a921a2d31 to your computer and use it in GitHub Desktop.
Dump entire database into seeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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