Created
August 13, 2015 19:10
-
-
Save rthbound/417641c0fc95e7751e97 to your computer and use it in GitHub Desktop.
Procedure for seeding using psql's `copy`
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
models = %w{ | |
skins | |
themes | |
options | |
site_settings | |
links | |
faq_categories | |
faq_questions | |
faq_answers | |
completion_indicators | |
privacy_policy_sections | |
terms_and_conditions_sections | |
support_items | |
contact_methods | |
} | |
models.each do |model| | |
klass = model.singularize.camelize.constantize | |
columns = klass.attribute_names - ["created_at", "updated_at"] | |
columns = columns.map {|x| "\"#{x}\"" }.join(",") # Double-quote column names | |
# Import records from csv | |
ActiveRecord::Base.connection.execute("copy #{model} (#{columns}) from '#{Rails.root.join("fluff/#{model}.csv")}' CSV HEADER QUOTE '~';") | |
# Kick primary key sequence (important due to nature of psql copy command) | |
ActiveRecord::Base.connection.execute(%Q[ select setval('#{model}_id_seq', max(id)) from #{model}]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment