Skip to content

Instantly share code, notes, and snippets.

@alexanderadam
Forked from merqlove/optimization.rake
Created July 17, 2018 08:13
Show Gist options
  • Select an option

  • Save alexanderadam/e078028b1a4bbb21a7e747cc77c0f8c7 to your computer and use it in GitHub Desktop.

Select an option

Save alexanderadam/e078028b1a4bbb21a7e747cc77c0f8c7 to your computer and use it in GitHub Desktop.

Revisions

  1. @merqlove merqlove created this gist Jan 9, 2015.
    33 changes: 33 additions & 0 deletions optimization.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    namespace :optimization do
    desc "Provide DB vacuum for production environment"
    task :vacuum => :environment do
    begin
    tables = ActiveRecord::Base.connection.tables
    tables.each do |table|
    ActiveRecord::Base.connection.execute("VACUUM FULL ANALYZE #{table};")
    end
    rescue Exception => exc
    Rails.logger.error("Database VACUUM error: #{exc.message}")
    end
    end

    desc "Provide DB reindex for production environment"
    task :reindex => :environment do
    begin
    tables = ActiveRecord::Base.connection.tables
    tables.each do |table|
    ActiveRecord::Base.connection.execute("REINDEX TABLE #{table};")
    end
    rescue Exception => exc
    Rails.logger.error("Database REINDEX error: #{exc.message}")
    end
    end

    desc "Reset PRIMARY KEY sequences "
    task :resetpk => :environment do
    tables = ActiveRecord::Base.connection.tables
    tables.each do |table|
    ActiveRecord::Base.connection.reset_pk_sequence!(table)
    end
    end
    end