Class: OnlineMigrations::DataMigrations::BackfillDoNotMergeOnElectricalSpecs

Inherits:
OnlineMigrations::DataMigration
  • Object
show all
Defined in:
lib/online_migrations/data_migrations/backfill_do_not_merge_on_electrical_specs.rb

Overview

Sets do_not_merge = true on every existing product_specifications
row whose token is in Models::ItemSpecificationHelper::PER_ITEM_SPEC_TOKENS
(watts, voltage, amps, ohms).

Background:
Maintenance::ItemMaintenance#consolidate_specs runs nightly and
collapses any rows whose (token, grouping, sku_regexp, method, text_blurb, units, image_id, formatter) tuple matches into a single
row — unless do_not_merge = true. For tokens whose values
coincidentally match across unrelated SKUs (a 175W cable kit and a
175W towel warmer are not "the same spec"), this collapse silently
welds independent items into a shared row. Editing one then mutates
the others.

The application code (Models::ItemSpecificationHelper#create_or_set_spec_value)
now sets do_not_merge: true for these tokens at write time. This
migration backfills the same flag onto pre-existing rows so the next
consolidate run won't re-merge them after the per-item split runs.

Constant Summary collapse

BATCH_SIZE =
1_000
TOKENS =
Models::ItemSpecificationHelper::PER_ITEM_SPEC_TOKENS.map(&:to_s).freeze

Delegated Instance Attributes collapse

Instance Method Summary collapse

Instance Method Details

#collectionActiveRecord::Batches::BatchEnumerator

Yields batches of ProductSpecification rows that still need the
flag flipped.

Returns:

  • (ActiveRecord::Batches::BatchEnumerator)

Raises:

  • (ActiveRecord::ActiveRecordError)

    on database error.



35
36
37
# File 'lib/online_migrations/data_migrations/backfill_do_not_merge_on_electrical_specs.rb', line 35

def collection
  candidate_relation.in_batches(of: BATCH_SIZE)
end

#countInteger

Number of rows that still need the flag flipped. Used by
online_migrations for progress reporting.

Returns:

  • (Integer)

Raises:

  • (ActiveRecord::ActiveRecordError)

    on database error.



56
# File 'lib/online_migrations/data_migrations/backfill_do_not_merge_on_electrical_specs.rb', line 56

delegate :count, to: :candidate_relation

#process(specs) ⇒ void

This method returns an undefined value.

Flips do_not_merge to true on every row in the batch via a single
update_all (no callbacks — this is a flag-only change).

Parameters:

Raises:

  • (ActiveRecord::ActiveRecordError)

    propagated from update_all.



46
47
48
# File 'lib/online_migrations/data_migrations/backfill_do_not_merge_on_electrical_specs.rb', line 46

def process(specs)
  specs.update_all(do_not_merge: true)
end