Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

[WIP] Support purge:orphaned for postgresql #376

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions lib/tasks/prune_reports.rake
Original file line number Diff line number Diff line change
Expand Up @@ -81,41 +81,35 @@ UNITS:
namespace :prune do
desc 'Delete orphaned records whose report has already been deleted'
task :orphaned => :environment do
report_dependent_deletion = 'report_id not in (select id from reports)'
report_dependent_deletion = 'e LEFT JOIN reports r ON (e.reports_id = r.id) WHERE r.id IS NULL'

orphaned_tables = ActiveSupport::OrderedHash[
Metric, report_dependent_deletion,
ReportLog, report_dependent_deletion,
ResourceStatus, report_dependent_deletion,
ResourceEvent, 'resource_status_id not in (select id from resource_statuses)'
ResourceEvent, 'e LEFT JOIN resource_statuses s ON (e.resource_status_id = s.id) WHERE resource_status_id.id IS NULL'
]

puts "Going to delete orphaned records from #{orphaned_tables.keys.map(&:table_name).join(', ')}\n"

orphaned_tables.each do |model, deletion_where_clause|
puts "Preparing to delete from #{model.table_name}"
start_time = Time.now
deletion_count = model.where(deletion_where_clause).to_a.size
if deletion_count > 0
puts "#{start_time.to_s(:db)}: Deleting #{deletion_count} orphaned records from #{model.table_name}"
pbar = ProgressBar.new('Deleting', deletion_count, STDOUT)

# Deleting a very large group of records in MySQL can be very slow with no feedback
# Breaking the deletion up into blocks turns out to be overall faster
# and allows for progress feedback
DELETION_BATCH_SIZE = 1000
while deletion_count > DELETION_BATCH_SIZE
ActiveRecord::Base.connection.execute(
"delete from #{model.table_name} where #{deletion_where_clause} limit #{DELETION_BATCH_SIZE}"
)
pbar.inc(DELETION_BATCH_SIZE)
deletion_count -= DELETION_BATCH_SIZE
end

pbar.finish
puts
else
puts 'No records to delete'
deletion_count = model.count(:joins => deletion_where_clause)

puts "#{start_time.to_s(:db)}: Deleting #{deletion_count} orphaned records from #{model.table_name}"
pbar = ProgressBar.new('Deleting', deletion_count, STDOUT)

# Deleting a very large group of records can be very slow with no feedback
# Breaking the deletion up into blocks turns out to be overall faster
# and allows for progress feedback
DELETION_BATCH_SIZE = 1000
while deletion_count > 0
ActiveRecord::Base.connection.execute(
"DELETE FROM #{model.table_name} USING #{model.table_name} WHERE #{deletion_where_clause}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this removes the batch part of the delete

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah postgresql doesn't support that! yikes!

)
pbar.inc(DELETION_BATCH_SIZE)
deletion_count -= DELETION_BATCH_SIZE
end
end
end
Expand Down