Skip to content

Instantly share code, notes, and snippets.

@melihkocaturk
Forked from seyhunak/seeds.rb
Created November 2, 2016 13:38
Show Gist options
  • Save melihkocaturk/bd00ddb21391c3f0d1f4d54b606925d0 to your computer and use it in GitHub Desktop.
Save melihkocaturk/bd00ddb21391c3f0d1f4d54b606925d0 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment