<commit_before>class AssociateFieldsToAlternatives < ActiveRecord::Migration
  class CustomFormsPlugin::Field < ActiveRecord::Base
    set_table_name :custom_forms_plugin_fields
    has_many :alternatives, :class_name => 'CustomFormsPlugin::Alternative'
    serialize :choices, Hash
  end

  def self.up
    CustomFormsPlugin::Field.reset_column_information

    CustomFormsPlugin::Field.find_each do |f|
      f.choices.each do |key, value|
        CustomFormsPlugin::Alternative.create!(:label => key, :field_id => f.id)
      end
    end

    change_table :custom_forms_plugin_fields do |t|
      t.remove :choices
    end
  end

  def self.down
    raise ActiveRecord::IrreversibleMigration
  end
end<commit_msg>custom-forms: update answers with the alternatives ids<commit_after>class AssociateFieldsToAlternatives < ActiveRecord::Migration
  class CustomFormsPlugin::Field < ActiveRecord::Base
    set_table_name :custom_forms_plugin_fields
    has_many :alternatives, :class_name => 'CustomFormsPlugin::Alternative'
    serialize :choices, Hash
  end

  def self.up
    CustomFormsPlugin::Field.reset_column_information

    CustomFormsPlugin::Field.find_each do |f|
      f.choices.each do |key, value|
        CustomFormsPlugin::Alternative.create!(:label => key, :field_id => f.id)
      end
    end

    CustomFormsPlugin::Answer.find_each do |answer|
      labels = []
      answer.value.split(',').each do |value|
        labels << answer.field.choices.invert[value]
      end
      labels.compact!
      if labels.present?
        answer.value = answer.field.alternatives.where('label IN (?)', labels).map(&:id).join(',')
        answer.save!
      end
    end

    change_table :custom_forms_plugin_fields do |t|
      t.remove :choices
    end
  end

  def self.down
    raise ActiveRecord::IrreversibleMigration
  end
end
{'fraction_non_alphanumeric': 0.09521218715995647, 'fraction_numerical': 0.0, 'mean_word_length': 3.691326530612245, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3113472', 'n_tokens_mistral': 565, 'n_tokens_neox': 545, 'n_words': 117}