20170813141631_cleanup_account_deletions_and_add_unique_index.rb 765 Bytes
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class CleanupAccountDeletionsAndAddUniqueIndex < ActiveRecord::Migration[5.1]
  def up
    remove_column :account_deletions, :diaspora_handle

    duplicate_query = "WHERE a1.person_id = a2.person_id AND a1.id > a2.id"
    if AppConfig.postgres?
      execute("DELETE FROM account_deletions AS a1 USING account_deletions AS a2 #{duplicate_query}")
    else
      execute("DELETE a1 FROM account_deletions a1, account_deletions a2 #{duplicate_query}")
    end

    add_index :account_deletions, :person_id, name: :index_account_deletions_on_person_id, unique: true
  end

  def down
    remove_index :account_deletions, name: :index_account_deletions_on_person_id
    add_column :account_deletions, :diaspora_handle, :string
  end
end