Class: LooseForeignKeys::ModificationTracker

Inherits:
Object
  • Object
show all
Defined in:
app/models/loose_foreign_keys/modification_tracker.rb

Direct Known Subclasses

TurboModificationTracker

Instance Method Summary collapse

Constructor Details

#initializeModificationTracker

Returns a new instance of ModificationTracker.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/loose_foreign_keys/modification_tracker.rb', line 7

def initialize
  @delete_count_by_table = Hash.new { |h, k| h[k] = 0 }
  @update_count_by_table = Hash.new { |h, k| h[k] = 0 }
  @start_time = monotonic_time
  @deletes_counter = Gitlab::Metrics.counter(
    :loose_foreign_key_deletions,
    'The number of loose foreign key deletions'
  )
  @updates_counter = Gitlab::Metrics.counter(
    :loose_foreign_key_updates,
    'The number of loose foreign key updates'
  )
end

Instance Method Details

#add_deletions(table, count) ⇒ Object



33
34
35
36
# File 'app/models/loose_foreign_keys/modification_tracker.rb', line 33

def add_deletions(table, count)
  @delete_count_by_table[table] += count
  @deletes_counter.increment({ table: table }, count)
end

#add_updates(table, count) ⇒ Object



38
39
40
41
# File 'app/models/loose_foreign_keys/modification_tracker.rb', line 38

def add_updates(table, count)
  @update_count_by_table[table] += count
  @updates_counter.increment({ table: table }, count)
end

#max_deletesObject



25
26
27
# File 'app/models/loose_foreign_keys/modification_tracker.rb', line 25

def max_deletes
  100_000
end

#max_runtimeObject



21
22
23
# File 'app/models/loose_foreign_keys/modification_tracker.rb', line 21

def max_runtime
  30.seconds
end

#max_updatesObject



29
30
31
# File 'app/models/loose_foreign_keys/modification_tracker.rb', line 29

def max_updates
  50_000
end

#over_limit?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'app/models/loose_foreign_keys/modification_tracker.rb', line 43

def over_limit?
  @delete_count_by_table.values.sum >= max_deletes ||
    @update_count_by_table.values.sum >= max_updates ||
    monotonic_time - @start_time >= max_runtime
end

#statsObject



49
50
51
52
53
54
55
56
57
# File 'app/models/loose_foreign_keys/modification_tracker.rb', line 49

def stats
  {
    over_limit: over_limit?,
    delete_count_by_table: @delete_count_by_table,
    update_count_by_table: @update_count_by_table,
    delete_count: @delete_count_by_table.values.sum,
    update_count: @update_count_by_table.values.sum
  }
end