Last active
August 29, 2015 14:05
-
-
Save twilliamsark/69530ba4d2f4d27b5920 to your computer and use it in GitHub Desktop.
Concern for adding ability to serialize a model's table to create a seed_fu insert script
This file contains hidden or 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
module SeedFuSerializeable | |
extend ActiveSupport::Concern | |
included do | |
define_method(:to_h) do | |
h = attributes.keys.select{|k| k != "created_at" && k != "updated_at" }.map{|k| "#{k}: #{k}"}.join(', ') | |
eval("{#{h}}") | |
end | |
end | |
# I got the to_seed_fu from http://stackoverflow.com/questions/7951746/seed-fu-from-existing-database | |
# I just wrapped it in a concern and tacked on a define to_h | |
module ClassMethods | |
def to_seed_fu(outfile=$stdout) | |
SeedFu::Writer.write(outfile, { class_name: self.to_s, constraints: [:id]}) do |writer| | |
all.order(:id).each do |obj| | |
writer.add(obj.to_h) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found myself needing to update Sqlite3 and MySql databases from a PostgreSQL db. About 2 mins into writing a script to make my sql dump generic I thought what I really need is a generic data inserter like seed_fu. This lead to some Googling and to me writing the above concern. I've made a few project specific modifications since then but this is the original.