Created
December 7, 2013 14:54
-
-
Save seyhunak/7843549 to your computer and use it in GitHub Desktop.
Rails - Import SQL file as seed
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
unless Rails.env.production? | |
connection = ActiveRecord::Base.connection | |
connection.tables.each do |table| | |
connection.execute("TRUNCATE #{table}") unless table == "schema_migrations" | |
end | |
sql = File.read('db/import.sql') | |
statements = sql.split(/;$/) | |
statements.pop | |
ActiveRecord::Base.transaction do | |
statements.each do |statement| | |
connection.execute(statement) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very helpful. Thank you @seyhunak.