Created
January 24, 2019 15:16
-
-
Save peteonrails/b10fa9f5674d2a080416e857bb1e9c0c to your computer and use it in GitHub Desktop.
Database Truncation
This file contains 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
# 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 |
This file contains 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
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