<commit_before>class AddPrimaryToGroups < ActiveRecord::Migration
  def up
    add_column :groups, :primary, :boolean, default: false, null: false

    Threadable::Transactions.in_migration = true
    threadable = Threadable.new(threadable_env)

    threadable.organizations.all.each do |organization|
      puts "#{organization.name} starting"

      attributes = {
        name: organization.name,
        subject_tag: organization.subject_tag,
        email_address_tag: organization.email_address_username,
        auto_join: false,
        primary: true,
        color: '#7f8c8d',
      }

      group = organization.groups.create(attributes)

      organization.members.all.each do |member|
        next if member.organization_membership_record.ungrouped_mail_delivery == 0
        puts "    #{member.email_address}"
        membership = group.members.add member, send_notice: false
        if member.organization_membership_record.ungrouped_mail_delivery == 2
          membership.gets_in_summary!
        end
      end

      group.update(auto_join: true)

      # now move mail
      conversation_fragments = organization.conversations.ungrouped.map do |conversation|
        "(#{group.id},#{conversation.id})"
      end.join(',')

      execute "insert into conversation_groups (group_id, conversation_id) values #{conversation_fragments}"

      puts "#{organization.name} done"
    end
  end

  def down
    Threadable::Transactions.in_migration = true
    threadable = Threadable.new(threadable_env)
    Group.where(primary: true).each do |group_record|<commit_msg>Fixed some more content-related bugs in the biggest migration ever to migrate<commit_after>class AddPrimaryToGroups < ActiveRecord::Migration
  def up
    add_column :groups, :primary, :boolean, default: false, null: false

    Threadable::Transactions.in_migration = true
    threadable = Threadable.new(threadable_env)

    threadable.organizations.all.each do |organization|
      puts "#{organization.name} starting"

      group = organization.groups.all.find { |g| g.name == organization.name }

      if group
        group.update(auto_join: false, primary: true)
      else
        attributes = {
          name: organization.name,
          subject_tag: organization.subject_tag,
          email_address_tag: organization.email_address_username,
          auto_join: false,
          primary: true,
          color: '#7f8c8d',
        }

        group = organization.groups.create(attributes)
      end

      organization.members.all.each do |member|
        next if member.organization_membership_record.ungrouped_mail_delivery == 0
        puts "    #{member.email_address}"
        membership = group.members.add member, send_notice: false
        if member.organization_membership_record.ungrouped_mail_delivery == 2
          membership.gets_in_summary!
        end
      end

      group.update(auto_join: true)

      # now move mail
      conversation_fragments = organization.conversations.ungrouped.map do |conversation|
        "(#{group.id},#{conversation.id})"
      end.join(',')

      if conversation_fragments.present?
        execute "insert into conversation_groups (group_id, conversation_id) values #{conversation_fragments}"
      end

      puts "#{organization.name} done"
    end
  end

  def down
    Threadable::Transactions.in_migration = true
    threadable = Threadable.new(threadable_env)
    Group.where(primary: true).each do |group_record|
{'fraction_non_alphanumeric': 0.08985842242126553, 'fraction_numerical': 0.002889338341519792, 'mean_word_length': 3.106761565836299, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5707379', 'n_tokens_mistral': 990, 'n_tokens_neox': 972, 'n_words': 210}