<commit_before>class LinkShareVisibilitiesWithUser < ActiveRecord::Migration
  class ShareVisibility < ActiveRecord::Base
  end

  def up
    cleanup_deleted_share_visibilities

    remove_columns :share_visibilities, :created_at, :updated_at
    add_column :share_visibilities, :user_id, :integer

    # update_all from AR doesn't work with postgres, see: https://github.com/rails/rails/issues/13496
    if AppConfig.postgres?
      execute "UPDATE share_visibilities SET user_id = contacts.user_id " \
              "FROM contacts WHERE contacts.id = share_visibilities.contact_id"
    else
      ShareVisibility.joins("INNER JOIN contacts ON share_visibilities.contact_id = contacts.id")
        .update_all("share_visibilities.user_id = contacts.user_id")
    end

    remove_foreign_key :share_visibilities, name: :post_visibilities_contact_id_fk

    remove_index :share_visibilities, name: :index_post_visibilities_on_contact_id
    remove_index :share_visibilities, name: :shareable_and_contact_id
    remove_index :share_visibilities, name: :shareable_and_hidden_and_contact_id

    remove_column :share_visibilities, :contact_id
    change_column :share_visibilities, :user_id, :integer, null: false

    ShareVisibility.joins("LEFT OUTER JOIN users ON users.id = share_visibilities.user_id")
      .delete_all("users.id is NULL")

    add_index :share_visibilities, :user_id
    add_index :share_visibilities, %i(shareable_id shareable_type user_id), name: :shareable_and_user_id
    add_index :share_visibilities, %i(shareable_id shareable_type hidden user_id),
              name: :shareable_and_hidden_and_user_id

    add_foreign_key :share_visibilities, :users, name: :share_visibilities_user_id_fk, on_delete: :cascade
  end

  def down
    add_column :share_visibilities, :contact_id, :integer

    if AppConfig.postgres?
      execute "UPDATE share_visibilities SET contact_id = contacts.id " \
              "FROM contacts WHERE contacts.user_id = share_visibilities.user_id"
    else
      ShareVisibility.joins("INNER JOIN contacts ON share_visibilities.user_id = contacts.user_id")
        .update_all("share_visibilities.contact_id = contacts.id")
    end

    remove_foreign_key :share_visibilities, name: :share_visibilities_user_id_fk
<commit_msg>Delete all null entries before removing the column<commit_after>class LinkShareVisibilitiesWithUser < ActiveRecord::Migration
  class ShareVisibility < ActiveRecord::Base
  end

  def up
    cleanup_deleted_share_visibilities

    remove_columns :share_visibilities, :created_at, :updated_at
    add_column :share_visibilities, :user_id, :integer

    # update_all from AR doesn't work with postgres, see: https://github.com/rails/rails/issues/13496
    if AppConfig.postgres?
      execute "UPDATE share_visibilities SET user_id = contacts.user_id " \
              "FROM contacts WHERE contacts.id = share_visibilities.contact_id"
    else
      ShareVisibility.joins("INNER JOIN contacts ON share_visibilities.contact_id = contacts.id")
        .update_all("share_visibilities.user_id = contacts.user_id")
    end

    remove_foreign_key :share_visibilities, name: :post_visibilities_contact_id_fk

    remove_index :share_visibilities, name: :index_post_visibilities_on_contact_id
    remove_index :share_visibilities, name: :shareable_and_contact_id
    remove_index :share_visibilities, name: :shareable_and_hidden_and_contact_id

    remove_column :share_visibilities, :contact_id

    ShareVisibility.joins("LEFT OUTER JOIN users ON users.id = share_visibilities.user_id")
      .delete_all("users.id is NULL")

    change_column :share_visibilities, :user_id, :integer, null: false

    add_index :share_visibilities, :user_id
    add_index :share_visibilities, %i(shareable_id shareable_type user_id), name: :shareable_and_user_id
    add_index :share_visibilities, %i(shareable_id shareable_type hidden user_id),
              name: :shareable_and_hidden_and_user_id

    add_foreign_key :share_visibilities, :users, name: :share_visibilities_user_id_fk, on_delete: :cascade
  end

  def down
    add_column :share_visibilities, :contact_id, :integer

    if AppConfig.postgres?
      execute "UPDATE share_visibilities SET contact_id = contacts.id " \
              "FROM contacts WHERE contacts.user_id = share_visibilities.user_id"
    else
      ShareVisibility.joins("INNER JOIN contacts ON share_visibilities.user_id = contacts.user_id")
        .update_all("share_visibilities.contact_id = contacts.id")
    end

    remove_foreign_key :share_visibilities, name: :share_visibilities_user_id_fk

{'fraction_non_alphanumeric': 0.07152202354993459, 'fraction_numerical': 0.002180549498473615, 'mean_word_length': 4.726591760299626, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '256299', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1385, 'n_words': 295}