Project

General

Profile

Defect #9685 ยป add_unique_index_in_issue_relations.patch

Etienne Massip, 2011-12-01 20:54

View differences:

db/migrate/20111201201315_add_unique_index_to_issue_relations.rb (revision 0)
1
class AddUniqueIndexToIssueRelations < ActiveRecord::Migration
2
  def self.up
3

  
4
    # Remove duplicates
5
    IssueRelation.connection.select_values("SELECT r.id FROM #{IssueRelation.table_name} r" +
6
      " 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|
7
        IssueRelation.delete_all(["id = ?", i])
8
    end
9

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

  
13
  def self.down
14
    remove_index :issue_relations, :column => [:issue_from_id, :issue_to_id]
15
  end
16
end
    (1-1/1)