summaryrefslogtreecommitdiffstats
path: root/db/migrate/20111201201315_add_unique_index_to_issue_relations.rb
blob: 63e6af87be1578697bf1fc7683d5e02f743e5aa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class AddUniqueIndexToIssueRelations < ActiveRecord::Migration[4.2]
  def self.up

    # Remove duplicates
    IssueRelation.connection.select_values("SELECT r.id FROM #{IssueRelation.table_name} r" +
      " WHERE r.id > (SELECT min(r1.id) FROM #{IssueRelation.table_name} r1 WHERE r1.issue_from_id = r.issue_from_id AND r1.issue_to_id = r.issue_to_id)").each do |i|
        IssueRelation.where(["id = ?", i]).delete_all
    end

    add_index :issue_relations, [:issue_from_id, :issue_to_id], :unique => true
  end

  def self.down
    remove_index :issue_relations, :column => [:issue_from_id, :issue_to_id]
  end
end