<commit_before>class EnterpriseConfigRefactor < ActiveRecord::Migration
  def up
    add_column :enterprises, :sells, :string, null: false, default: 'none'

    Enterprise.all.each do |enterprise|
      enterprise.sells = sells_what?(enterprise)
      enterprise.save!
    end

    remove_column :enterprises, :type
    remove_column :enterprises, :is_distributor
  end

  def down
  end

  def sells_what?(enterprise)
    is_distributor = enterprise.read_attribute(:is_distributor)
    type = enterprise.read_attribute(:type)
    return "none" if !is_distributor || type == "profile"
    return "own" if type == "single" || enterprise.suppliers == [enterprise]
    return "any"
  end
end<commit_msg>add rollback to migration<commit_after>class EnterpriseConfigRefactor < ActiveRecord::Migration
  def up
    add_column :enterprises, :sells, :string, null: false, default: 'none'

    Enterprise.all.each do |enterprise|
      enterprise.sells = sells_what?(enterprise)
      enterprise.save!
    end

    remove_column :enterprises, :type
    remove_column :enterprises, :is_distributor
  end

  def down
    # This process is lossy. Producer profiles wont exist.
    add_column :enterprises, :type, :string, null: false, default: 'profile'
    add_column :enterprises, :is_distributor, :boolean

    Enterprise.all.each do |enterprise|
      enterprise.type = type?(enterprise)
      enterprise.is_distributor = distributes?(enterprise)
      enterprise.save!
    end

    remove_column :enterprises, :sells
  end

  def sells_what?(enterprise)
    is_distributor = enterprise.read_attribute(:is_distributor)
    type = enterprise.read_attribute(:type)
    return "none" if !is_distributor || type == "profile"
    return "own" if type == "single"
    return "any"
  end

  def distributes?(enterprise)
    return true if enterprise.read_attribute(:sells) != "none"
    return false
  end

  def type?(enterprise)
    sells = enterprise.read_attribute(:sells)
    return "profile" if !enterprise.is_primary_producer and sells == "none"
    return "single" if sells == "own"
    return "full"
  end
end
{'fraction_non_alphanumeric': 0.09890632429862102, 'fraction_numerical': 0.0, 'mean_word_length': 3.927400468384075, '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': '6033313', 'n_tokens_mistral': 655, 'n_tokens_neox': 645, 'n_words': 167}