Class: OnlineMigrations::DataMigrations::SplitSharedElectricalSpecRows

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

Overview

Walks every existing product_specifications row whose token is in
Models::ItemSpecificationHelper::PER_ITEM_SPEC_TOKENS and clones it
so each item owns its own row. Idempotent: rows already on a single
item are skipped.

Background:
Before the consolidator skip-list landed, the nightly job collapsed
any electrical spec whose value coincided across unrelated items
into a single row. Production today has hundreds of such shared
rows, including five towel-warmer wattage rows shared with unrelated
cable kits. While shared, an edit to one item's wattage rewrites
every co-tenant's value at once.

Constant Summary collapse

BATCH_SIZE =

Spec rows are heavy on callbacks (translations, render refresh
enqueue), so keep batches small.

25
TOKENS =

Electrical spec tokens whose numeric values coincide across unrelated
SKUs. Stored as strings to match the product_specifications.token
column type at query time.

Models::ItemSpecificationHelper::PER_ITEM_SPEC_TOKENS.map(&:to_s).freeze

Instance Method Summary collapse

Instance Method Details

#collectionActiveRecord::Batches::BatchEnumerator

Yields batches of shared electrical-spec rows.

Returns:

  • (ActiveRecord::Batches::BatchEnumerator)

Raises:

  • (ActiveRecord::ActiveRecordError)

    on database error.



37
38
39
# File 'lib/online_migrations/data_migrations/split_shared_electrical_spec_rows.rb', line 37

def collection
  ProductSpecification.where(id: shared_spec_ids).in_batches(of: BATCH_SIZE)
end

#countInteger

Number of shared spec rows queued for splitting. Used by
online_migrations for progress reporting.

Returns:

  • (Integer)

Raises:

  • (ActiveRecord::ActiveRecordError)

    on database error.



58
# File 'lib/online_migrations/data_migrations/split_shared_electrical_spec_rows.rb', line 58

def count = shared_spec_ids.size

#process(specs) ⇒ void

This method returns an undefined value.

Splits each spec in the batch into per-item rows. Idempotent: a spec
that already has a single item is a no-op.

Parameters:

  • specs (ActiveRecord::Relation<ProductSpecification>)

    batch of
    shared spec rows yielded by #collection.

Raises:

  • (ActiveRecord::ActiveRecordError)

    propagated from
    transaction / update! / save! if a clone fails.



49
50
51
# File 'lib/online_migrations/data_migrations/split_shared_electrical_spec_rows.rb', line 49

def process(specs)
  specs.find_each { |spec| split_spec(spec) }
end