Skip to content

Instantly share code, notes, and snippets.

@prdpspkt
Created April 29, 2020 02:06
Show Gist options
  • Save prdpspkt/b34f9015de503aa124b442a931047a66 to your computer and use it in GitHub Desktop.
Save prdpspkt/b34f9015de503aa124b442a931047a66 to your computer and use it in GitHub Desktop.
Rake Task: Adding custom rake task example
namespace :db do
desc 'Truncate all tables, except schema_migrations (customizable)'
task :truncate, [ :tables ] => 'db:load_config' do |t, args|
args.with_defaults(tables: 'schema_migrations')
skipped = args[:tables].split(' ')
config = ActiveRecord::Base.configurations[::Rails.env]
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.execute("TRUNCATE #{table}") unless skipped.include?(table)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment