module DBPurger::PurgeTableHelper
DBPurger::PurgeTableHelper
keeps track of common code between purgers
Public Instance Methods
model()
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 6 def model @model ||= @database.models.detect { |m| m.table_name == @table.name.to_s } end
Private Instance Methods
batch_values(batch, field)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 41 def batch_values(batch, field) batch.map { |record| record.send(field) }.compact end
delete_records(batch)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 99 def delete_records(batch) if foreign_tables? delete_records_and_foreign_tables(batch) else delete_records_by_primary_key(batch) end end
delete_records_and_foreign_tables(batch)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 107 def delete_records_and_foreign_tables(batch) model.transaction do delete_records_by_primary_key(batch) purge_foreign_tables(batch) end end
delete_records_by_primary_key(batch)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 114 def delete_records_by_primary_key(batch) ids = batch_values(batch, model.primary_key) scope = model.where(model.primary_key => ids) delete_records_with_instrumentation(scope, ids.size) end
delete_records_with_instrumentation(scope, num_records = nil)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 49 def delete_records_with_instrumentation(scope, num_records = nil) @num_deleted ||= 0 ActiveSupport::Notifications.instrument('delete_records.db_purger', table_name: @table.name, num_records: num_records) do |payload| records_deleted = if ::DBPurger.config.explain? explain(scope) elsif @table.mark_deleted_field scope.update_all(@table.mark_deleted_field => @table.mark_deleted_value) else scope.delete_all end @num_deleted += records_deleted payload[:records_deleted] = records_deleted payload[:deleted] = @num_deleted end end
explain(scope)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 68 def explain(scope) sql = if @table.mark_deleted_field explain_update_sql(scope) else scope.to_sql.sub(/SELECT .*?FROM/, 'DELETE FROM') end ::DBPurger.config.explain_file.puts(sql + ';') scope.count end
explain_update_sql(scope)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 79 def explain_update_sql(scope) sql = scope.to_sql.dup sql.sub!(/SELECT .*?FROM/, 'UPDATE') sql.sub!('WHERE', "SET #{mark_deleted_field_quoted} = #{mark_deleted_value_quoted} WHERE") sql end
foreign_tables?()
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 45 def foreign_tables? @table.nested_plan.child_tables.any?(&:foreign_key) end
mark_deleted_field_quoted()
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 86 def mark_deleted_field_quoted @mark_deleted_field_quoted ||= model.connection.quote_column_name(@table.mark_deleted_field) end
mark_deleted_value_quoted()
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 90 def mark_deleted_value_quoted @mark_deleted_value_quoted ||= if @table.mark_deleted_value.is_a?(Time) model.connection.quote(@table.mark_deleted_value.strftime(::DBPurger.config.datetime_format)) else model.connection.quote(@table.mark_deleted_value) end end
purge_child_tables(batch)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 16 def purge_child_tables(batch) ids = batch_values(batch, model.primary_key) @table.nested_plan.child_tables.each do |table| next if table.foreign_key PurgeTable.new(@database, table, table.field, ids).purge! end end
purge_foreign_tables(batch)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 32 def purge_foreign_tables(batch) @table.nested_plan.child_tables.each do |table| next unless table.foreign_key next if (purge_values = batch_values(batch, table.foreign_key)).empty? PurgeTable.new(@database, table, table.field, purge_values).purge! end end
purge_nested_tables(batch)
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 12 def purge_nested_tables(batch) purge_child_tables(batch) unless @table.nested_plan.child_tables.empty? end
purge_parent_tables()
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 26 def purge_parent_tables @table.nested_plan.parent_tables.each do |table| PurgeTable.new(@database, table, table.field, @purge_value).purge! end end
purge_search_tables()
click to toggle source
# File lib/db-purger/purge_table_helper.rb, line 120 def purge_search_tables @table.nested_plan.search_tables.each do |table| PurgeTableScanner.new(@database, table).purge! end end