class DBPurger::PurgeTableScanner

DBPurger::PurgeTableScanner scans the entire table in batches use the search_proc to determine what ids to delete

Public Class Methods

new(database, table) click to toggle source
# File lib/db-purger/purge_table_scanner.rb, line 8
def initialize(database, table)
  @database = database
  @table = table
  @num_deleted = 0
end

Public Instance Methods

model() click to toggle source
# File lib/db-purger/purge_table_scanner.rb, line 14
def model
  @model ||= @database.models.detect { |m| m.table_name == @table.name.to_s }
end
purge!() click to toggle source
# File lib/db-purger/purge_table_scanner.rb, line 18
def purge!
  ActiveSupport::Notifications.instrument('purge.db_purger',
                                          table_name: @table.name) do |payload|
    purge_in_batches!
    purge_search_tables
    payload[:deleted] = @num_deleted
  end
  @num_deleted
end

Private Instance Methods

finish_instrumentation(name, payload) click to toggle source
# File lib/db-purger/purge_table_scanner.rb, line 80
def finish_instrumentation(name, payload)
  @instrumenter.finish(name, payload)
end
purge_in_batches!() click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/db-purger/purge_table_scanner.rb, line 32
def purge_in_batches!
  scope = model
  scope = scope.where(@table.conditions) if @table.conditions

  instrumentation_name = 'next_batch.db_purger'
  start_instrumentation(instrumentation_name)

  scope.find_in_batches(batch_size: @table.batch_size) do |batch|
    finish_instrumentation(
      instrumentation_name,
      table_name: @table.name,
      num_records: batch.size
    )

    batch = ActiveSupport::Notifications.instrument('search_filter.db_purger',
                                                    table_name: @table.name,
                                                    num_records: batch.size) do |payload|
      records_selected = @table.search_proc.call(batch)
      payload[:num_records_selected] = records_selected.size
      records_selected
    end

    if batch.empty?
      start_instrumentation(instrumentation_name)
      next
    end

    purge_nested_tables(batch) if @table.nested_tables?
    delete_records(batch)

    start_instrumentation(instrumentation_name)
  end

  finish_instrumentation(
    instrumentation_name,
    table_name: @table.name,
    num_records: 0,
    num_selected: 0
  )
end
start_instrumentation(name) click to toggle source

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength

# File lib/db-purger/purge_table_scanner.rb, line 75
def start_instrumentation(name)
  @instrumenter = ActiveSupport::Notifications.instrumenter
  @instrumenter.start(name, {})
end