Skip to content

Instantly share code, notes, and snippets.

@gregblake
Forked from romanlehnert/truncate_rails_tables.rb
Created December 10, 2019 13:28
Show Gist options
  • Save gregblake/8d66b95f7e96d47c40033be4f0c21ca7 to your computer and use it in GitHub Desktop.
Save gregblake/8d66b95f7e96d47c40033be4f0c21ca7 to your computer and use it in GitHub Desktop.
Truncate all tables in rails
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.execute("SET FOREIGN_KEY_CHECKS=0;")
ActiveRecord::Base.connection.tables.each do |table|
next if table == 'schema_migrations'
case ActiveRecord::Base.connection.adapter_name.downcase.to_sym
when :mysql2 || :postgresql
ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
when :sqlite
ActiveRecord::Base.connection.execute("DELETE FROM #{table}")
end
end
ActiveRecord::Base.connection.execute("SET FOREIGN_KEY_CHECKS=1;")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment