Commit 8e4c4cf0 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch '344635-track-deleted-records-table' into 'master'

Track processed loose FK records

See merge request gitlab-org/gitlab!73541
parents 58b44a0e dac93679
......@@ -7,6 +7,10 @@ module LooseForeignKeys
@deleted_parent_records = deleted_parent_records
@modification_tracker = modification_tracker
@models_by_table_name = models_by_table_name
@deleted_records_counter = Gitlab::Metrics.counter(
:loose_foreign_key_processed_deleted_records,
'The number of processed loose foreign key deleted records'
)
end
def execute
......@@ -21,13 +25,15 @@ module LooseForeignKeys
return if modification_tracker.over_limit?
# At this point, all associations are cleaned up, we can update the status of the parent records
LooseForeignKeys::DeletedRecord
update_count = LooseForeignKeys::DeletedRecord
.mark_records_processed_for_table_between(deleted_parent_records.first.fully_qualified_table_name, deleted_parent_records.first, deleted_parent_records.last)
deleted_records_counter.increment({ table: parent_klass.table_name, db_config_name: LooseForeignKeys::DeletedRecord.connection.pool.db_config.name }, update_count)
end
private
attr_reader :parent_klass, :deleted_parent_records, :modification_tracker, :models_by_table_name
attr_reader :parent_klass, :deleted_parent_records, :modification_tracker, :models_by_table_name, :deleted_records_counter
def record_result(cleaner, result)
if cleaner.async_delete?
......
......@@ -79,6 +79,8 @@ RSpec.describe LooseForeignKeys::BatchCleanerService do
end
context 'when parent records are deleted' do
let(:deleted_records_counter) { Gitlab::Metrics.registry.get(:loose_foreign_key_processed_deleted_records) }
before do
parent_record_1.delete
......@@ -98,8 +100,15 @@ RSpec.describe LooseForeignKeys::BatchCleanerService do
expect(loose_fk_child_table_2.where(parent_id_with_different_column: nil).count).to eq(2)
end
it 'cleans up the parent DeletedRecord' do
it 'cleans up the pending parent DeletedRecord' do
expect(LooseForeignKeys::DeletedRecord.status_pending.count).to eq(0)
expect(LooseForeignKeys::DeletedRecord.status_processed.count).to eq(1)
end
it 'records the DeletedRecord status updates', :prometheus do
counter = Gitlab::Metrics.registry.get(:loose_foreign_key_processed_deleted_records)
expect(counter.get(table: parent_model.table_name, db_config_name: 'main')).to eq(1)
end
it 'does not delete unrelated records' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment