Class: LooseForeignKeys::DeletedRecord

Inherits:
Gitlab::Database::SharedModel show all
Includes:
FromUnion, PartitionedTable
Defined in:
app/models/loose_foreign_keys/deleted_record.rb

Constant Summary collapse

PARTITION_DURATION =
1.day

Class Method Summary collapse

Methods inherited from Gitlab::Database::SharedModel

connection, #connection_db_config, connection_pool, using_connection

Class Method Details

.increment_attempts(records) ⇒ Object



81
82
83
84
85
86
# File 'app/models/loose_foreign_keys/deleted_record.rb', line 81

def self.increment_attempts(records)
  update_by_partition(records) do |partitioned_scope|
    # Naive incrementing of the cleanup_attempts is good enough for us.
    partitioned_scope.update_all('cleanup_attempts = cleanup_attempts + 1')
  end
end

.load_batch_for_table(table, batch_size) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/loose_foreign_keys/deleted_record.rb', line 38

def self.load_batch_for_table(table, batch_size)
  if Feature.enabled?("loose_foreign_keys_batch_load_using_union")
    partition_names = Gitlab::Database::PostgresPartitionedTable.each_partition(table_name).map(&:name)

    unions = partition_names.map do |partition_name|
      partition_number = partition_name[/\d+/].to_i

      select(arel_table[Arel.star], arel_table[:partition].as('partition_number'))
        .from("#{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}.#{partition_name} AS #{table_name}")
        .for_table(table)
        .where(partition: partition_number)
        .status_pending
        .consume_order
        .limit(batch_size)
    end

    select(arel_table[Arel.star])
      .from_union(unions, remove_duplicates: false, remove_order: false)
      .limit(batch_size)
      .to_a
  else
    # selecting partition as partition_number to workaround the sliding partitioning column ignore
    select(arel_table[Arel.star], arel_table[:partition].as('partition_number'))
      .for_table(table)
      .status_pending
      .consume_order
      .limit(batch_size)
      .to_a
  end
end

.mark_records_processed(records) ⇒ Object



69
70
71
72
73
# File 'app/models/loose_foreign_keys/deleted_record.rb', line 69

def self.mark_records_processed(records)
  update_by_partition(records) do |partitioned_scope|
    partitioned_scope.update_all(status: :processed)
  end
end

.reschedule(records, consume_after) ⇒ Object



75
76
77
78
79
# File 'app/models/loose_foreign_keys/deleted_record.rb', line 75

def self.reschedule(records, consume_after)
  update_by_partition(records) do |partitioned_scope|
    partitioned_scope.update_all(consume_after: consume_after, cleanup_attempts: 0)
  end
end