Skip to content

Instantly share code, notes, and snippets.

@peteonrails
Created January 24, 2019 15:16
Show Gist options
  • Save peteonrails/b10fa9f5674d2a080416e857bb1e9c0c to your computer and use it in GitHub Desktop.
Save peteonrails/b10fa9f5674d2a080416e857bb1e9c0c to your computer and use it in GitHub Desktop.
Database Truncation
# frozen-string-literal: true
class DatabaseTruncator
def self.truncate
skipped = 'schema_migrations'
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.tables.each do |table|
puts "Truncating #{table}"
ActiveRecord::Base.connection.execute("TRUNCATE #{table} CASCADE") unless skipped.include?(table) # rubocop:disable Metrics/LineLength
end
end
end
namespace :db do
desc 'Truncate all tables, except schema_migrations (customizable)'
task :truncate, [:tables] => 'db:load_config' do |_t, args|
require 'database_truncator'
DatabaseTruncator.truncate
puts 'Database truncated'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment