Created
April 29, 2020 02:06
-
-
Save prdpspkt/b34f9015de503aa124b442a931047a66 to your computer and use it in GitHub Desktop.
Rake Task: Adding custom rake task example
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| | |
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