Class: Maintenance::VariantGroupBackfill

Inherits:
Object
  • Object
show all
Defined in:
app/services/maintenance/variant_group_backfill.rb

Overview

Seeds the canonical VariantGroup layer from the two persisted grouping
mechanisms that predate it (Phase 1 of
doc/tasks/202607051428_VARIANT_GROUPING_UNIFICATION.md):

  1. Amazon — the legacy_amazon_variations backup table (via
    LegacyAmazonVariation) grouped by parent sku (NA marketplaces
    share one parent SKU, so US/CA/MX rows of the same family collapse into
    one VariantGroup); every variation row is linked via variant_group_id
    and the members come from the variations' items.
  2. Wayfaircatalog_items in the Wayfair catalogs grouped by their
    mirrored wayfair_display_sku (families with more than one member).

An item already claimed by a group is never re-claimed (item_id is
globally unique across memberships), so overlapping sources merge into the
first-seen family: if a Wayfair display-SKU group shares any member with an
Amazon-seeded family, the remaining Wayfair members join that family instead
of forming a duplicate.

Idempotent — re-running skips existing groups/members. Dry-run by default.

Examples:

Report what would be created

Maintenance::VariantGroupBackfill.new.call

Apply

Maintenance::VariantGroupBackfill.new(dry_run: false).call

Defined Under Namespace

Classes: Result

Constant Summary collapse

WAYFAIR_CATALOG_IDS =

Wayfair catalogs to read mirrored display-SKU families from (US, CA).

CatalogConstants::WAYFAIR_CATALOGS

Instance Method Summary collapse

Constructor Details

#initialize(dry_run: true) ⇒ VariantGroupBackfill

Returns a new instance of VariantGroupBackfill.

Parameters:

  • dry_run (Boolean) (defaults to: true)

    when true (default) nothing is written



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/maintenance/variant_group_backfill.rb', line 45

def initialize(dry_run: true)
  @dry_run = dry_run
  @groups_created = 0
  @members_added = 0
  @variations_linked = 0
  @skipped = []
  @log = []
  # Item ids already claimed by a family — pre-loaded once (kills the
  # per-item exists? round trips) and maintained in memory so a dry-run
  # previews exactly what a real run would do across both seed sources.
  @claimed_item_ids = VariantGroupMember.pluck(:item_id).to_set
  # item_id => group (nil in dry-run) for overlap-merges within one run.
  @groups_by_claimed_item = {}
end

Instance Method Details

#callResult

Runs both seeds (Amazon first — it carries the richer family identity).

Returns:



62
63
64
65
66
67
# File 'app/services/maintenance/variant_group_backfill.rb', line 62

def call
  backfill_from_amazon_variations
  backfill_from_wayfair_display_skus
  Result.new(groups_created: @groups_created, members_added: @members_added,
             variations_linked: @variations_linked, skipped: @skipped, log: @log)
end