<commit_before>class AddStandardVariantToProducts < ActiveRecord::Migration
  def up
    # Find products without any standard variants
    products_with_only_master = Spree::Product.includes(:variants).where('spree_variants.id IS NULL')

    products_with_only_master.each do |product|
      # Run the callback to add a copy of the master variant as a standard variant
      product.send(:ensure_standard_variant)
<commit_msg>Prepare master variants for duplication as standard variant by ensuring they have a unit value and that the product has a variant unit<commit_after>class AddStandardVariantToProducts < ActiveRecord::Migration
  def up
    # Make sure that all products have a variant_unit
    Spree::Product.where("variant_unit IS NULL OR variant_unit = ''").update_all(variant_unit: "items", variant_unit_name: "each")

    # Find products without any standard variants
    products_with_only_master = Spree::Product.includes(:variants).where('spree_variants.id IS NULL').select('DISTINCT spree_products.*')

    products_with_only_master.each do |product|
      # Add a unit_value to the master variant if it doesn't have one
      if product.unit_value.blank?
        if product.variant_unit == "weight" && match = product.unit_description.andand.match(/^(\d+(\.\d*)?)(k?g) ?(.*)$/)
          scale = (match[3] == "kg" ? 1000 : 1)
          product.unit_value = (match[1].to_i*scale)
          product.unit_description = match[4]
          product.save!
        else
          unless product.variant_unit == "items" && product.unit_description.present?
            product.unit_value = 1
            product.save!
          end
        end
      end

      # Run the callback to add a copy of the master variant as a standard variant
      product.send(:ensure_standard_variant)

{'fraction_non_alphanumeric': 0.09156895589056394, 'fraction_numerical': 0.005025125628140704, 'mean_word_length': 3.491228070175439, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5711026', 'n_tokens_mistral': 504, 'n_tokens_neox': 487, 'n_words': 148}