Class: OnlineMigrations::BackgroundMigrations::ResetCounters

Inherits:
OnlineMigrations::BackgroundMigration show all
Defined in:
lib/online_migrations/background_migrations/reset_counters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from OnlineMigrations::BackgroundMigration

named

Constructor Details

#initialize(model_name, counters, options = {}) ⇒ ResetCounters

Returns a new instance of ResetCounters.



9
10
11
12
13
# File 'lib/online_migrations/background_migrations/reset_counters.rb', line 9

def initialize(model_name, counters, options = {})
  @model = Object.const_get(model_name, false)
  @counters = counters
  @touch = options[:touch]
end

Instance Attribute Details

#countersObject (readonly)

Returns the value of attribute counters.



7
8
9
# File 'lib/online_migrations/background_migrations/reset_counters.rb', line 7

def counters
  @counters
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/online_migrations/background_migrations/reset_counters.rb', line 7

def model
  @model
end

#touchObject (readonly)

Returns the value of attribute touch.



7
8
9
# File 'lib/online_migrations/background_migrations/reset_counters.rb', line 7

def touch
  @touch
end

Instance Method Details

#countObject



50
51
52
53
54
55
# File 'lib/online_migrations/background_migrations/reset_counters.rb', line 50

def count
  # Exact counts are expensive on large tables, since PostgreSQL
  # needs to do a full scan. An estimated count should give a pretty decent
  # approximation of rows count in this case.
  Utils.estimated_count(connection, model.table_name)
end

#process_batch(relation) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/online_migrations/background_migrations/reset_counters.rb', line 19

def process_batch(relation)
  updates = counters.map do |counter_association|
    has_many_association = has_many_association(counter_association)

    foreign_key  = has_many_association.foreign_key.to_s
    child_class  = has_many_association.klass
    reflection   = child_class._reflections.values.find { |e| e.belongs_to? && e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
    counter_name = reflection.counter_cache_column

    quoted_association_table = connection.quote_table_name(has_many_association.table_name)
    count_subquery = "      SELECT COUNT(*)\n      FROM \#{quoted_association_table}\n      WHERE \#{quoted_association_table}.\#{connection.quote_column_name(foreign_key)} =\n        \#{model.quoted_table_name}.\#{model.quoted_primary_key}\n    SQL\n\n    \"\#{connection.quote_column_name(counter_name)} = (\#{count_subquery})\"\n  end\n\n  if touch\n    names = touch if touch != true\n    names = Array.wrap(names)\n    options = names.extract_options!\n    touch_updates = touch_attributes_with_time(*names, **options)\n    updates << model.sanitize_sql_for_assignment(touch_updates)\n  end\n\n  relation.update_all(updates.join(\", \"))\nend\n"

#relationObject



15
16
17
# File 'lib/online_migrations/background_migrations/reset_counters.rb', line 15

def relation
  model.unscoped
end