Skip to content

Instantly share code, notes, and snippets.

@romansklenar
Created April 1, 2015 18:08
Show Gist options
  • Save romansklenar/c6d661f2dde1b152aba3 to your computer and use it in GitHub Desktop.
Save romansklenar/c6d661f2dde1b152aba3 to your computer and use it in GitHub Desktop.
Useful rake task for maintain PostgreSQL databases
# 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