Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Created December 7, 2013 14:54
Show Gist options
  • Save seyhunak/7843549 to your computer and use it in GitHub Desktop.
Save seyhunak/7843549 to your computer and use it in GitHub Desktop.
Rails - Import SQL file as seed
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
@davidjeddy
Copy link

Very helpful. Thank you @seyhunak.

@marviorocha
Copy link

Very helpful, Thank you, @seyhunak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment