Created
April 1, 2015 18:08
-
-
Save romansklenar/c6d661f2dde1b152aba3 to your computer and use it in GitHub Desktop.
Useful rake task for maintain PostgreSQL databases
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
# lib/tasks/db.rake | |
namespace :db do | |
desc 'Maintains database by running command as ANALYZE, VACUUM and REINDEX' | |
task maintain: :environment do | |
verbose = ENV['VERBOSE'].present? | |
connection = ActiveRecord::Base.connection | |
puts "Maintaining database #{connection.current_database} ..." | |
connection.execute("VACUUM FULL #{'VERBOSE' if verbose}") | |
connection.execute("ANALYZE #{'VERBOSE' if verbose}") | |
connection.execute("REINDEX DATABASE #{connection.current_database}") | |
puts "DONE." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment