<commit_before># This migration is an OFN specific migration that enforces an order to have a single shipment at all times
class AddUniquenessOfOrderIdToSpreeShipments < ActiveRecord::Migration
  def change
    destroy_all_but_latest_shipments

    remove_index :spree_shipments, :order_id
    add_index :spree_shipments, :order_id, unique: true
  end

  private

  # Destroy all but the latest shipment in each order
  def destroy_all_but_latest_shipments
    latest_shipments = Spree::Shipment.
      select("order_id, MAX(updated_at) updated_at").
      group(:order_id).
      having("count(*) > 1")

    all_duplicated_shipments = Spree::Shipment.<commit_msg>Fix failing migration by clearing schema cache<commit_after># This migration is an OFN specific migration that enforces an order to have a single shipment at all times
class AddUniquenessOfOrderIdToSpreeShipments < ActiveRecord::Migration
  def change
    Spree::InventoryUnit.connection.schema_cache.clear!
    Spree::InventoryUnit.reset_column_information

    destroy_all_but_latest_shipments

    remove_index :spree_shipments, :order_id
    add_index :spree_shipments, :order_id, unique: true
  end

  private

  # Destroy all but the latest shipment in each order
  def destroy_all_but_latest_shipments
    latest_shipments = Spree::Shipment.
      select("order_id, MAX(updated_at) updated_at").
      group(:order_id).
      having("count(*) > 1")

    all_duplicated_shipments = Spree::Shipment.
{'fraction_non_alphanumeric': 0.06607629427792916, 'fraction_numerical': 0.0013623978201634877, 'mean_word_length': 4.73828125, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4334982', 'n_tokens_mistral': 450, 'n_tokens_neox': 440, 'n_words': 121}