Skip to content

Instantly share code, notes, and snippets.

@rthbound
Created August 13, 2015 19:10
Show Gist options
  • Save rthbound/417641c0fc95e7751e97 to your computer and use it in GitHub Desktop.
Save rthbound/417641c0fc95e7751e97 to your computer and use it in GitHub Desktop.
Procedure for seeding using psql's `copy`
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