<commit_before>class AddLineItemIdToSpreeInventoryUnits < ActiveRecord::Migration
  def change
    # Stores running the product-assembly extension already have a line_item_id column
    unless column_exists? Spree::InventoryUnit.table_name, :line_item_id
      add_column :spree_inventory_units, :line_item_id, :integer
      add_index :spree_inventory_units, :line_item_id

      shipments = Spree::Shipment.includes({ :inventory_units => :variant }, :order)

      shipments.find_each do |shipment|
        shipment.inventory_units.group_by(&:variant).each do |variant, units|

          line_item = shipment.order.find_line_item_by_variant(variant)
          Spree::InventoryUnit.where(id: units.map(&:id)).update_all(line_item_id: line_item.id)
        end<commit_msg>Fix migration to support legacy stores with deleted variants and line items<commit_after>class AddLineItemIdToSpreeInventoryUnits < ActiveRecord::Migration
  def change
    # Stores running the product-assembly extension already have a line_item_id column
    unless column_exists? Spree::InventoryUnit.table_name, :line_item_id
      add_column :spree_inventory_units, :line_item_id, :integer
      add_index :spree_inventory_units, :line_item_id

      shipments = Spree::Shipment.includes(:inventory_units, :order)

      shipments.find_each do |shipment|
        shipment.inventory_units.group_by(&:variant).each do |variant, units|

          line_item = shipment.order.find_line_item_by_variant(variant)
          next unless line_item

          Spree::InventoryUnit.where(id: units.map(&:id)).update_all(line_item_id: line_item.id)
        end
{'fraction_non_alphanumeric': 0.08317929759704251, 'fraction_numerical': 0.0, 'mean_word_length': 4.561643835616438, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6274560', 'n_tokens_mistral': 501, 'n_tokens_neox': 494, 'n_words': 99}