2011-05-30 00:11:52 +04:00
|
|
|
#-- copyright
|
|
|
|
# ChiliProject is a project management system.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# Copyright (C) 2010-2011 the ChiliProject Team
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-05-30 22:52:25 +04:00
|
|
|
#
|
2011-05-30 00:11:52 +04:00
|
|
|
# See doc/COPYRIGHT.rdoc for more details.
|
|
|
|
#++
|
|
|
|
|
2010-11-06 14:44:38 +03:00
|
|
|
class AddUniqueIndexOnMembers < ActiveRecord::Migration
|
|
|
|
def self.up
|
2010-11-06 14:49:45 +03:00
|
|
|
# Clean and reassign MemberRole rows if needed
|
|
|
|
MemberRole.delete_all("member_id NOT IN (SELECT id FROM #{Member.table_name})")
|
2010-11-06 14:44:38 +03:00
|
|
|
MemberRole.update_all("member_id =" +
|
|
|
|
" (SELECT min(m2.id) FROM #{Member.table_name} m1, #{Member.table_name} m2" +
|
|
|
|
" WHERE m1.user_id = m2.user_id AND m1.project_id = m2.project_id" +
|
|
|
|
" AND m1.id = #{MemberRole.table_name}.member_id)")
|
|
|
|
# Remove duplicates
|
|
|
|
Member.connection.select_values("SELECT m.id FROM #{Member.table_name} m" +
|
|
|
|
" WHERE m.id > (SELECT min(m1.id) FROM #{Member.table_name} m1 WHERE m1.user_id = m.user_id AND m1.project_id = m.project_id)").each do |i|
|
|
|
|
Member.delete_all(["id = ?", i])
|
|
|
|
end
|
2011-05-30 22:52:25 +04:00
|
|
|
|
2010-11-06 14:44:38 +03:00
|
|
|
# Then add a unique index
|
|
|
|
add_index :members, [:user_id, :project_id], :unique => true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.down
|
2011-01-02 02:08:49 +03:00
|
|
|
remove_index :members, [:user_id, :project_id]
|
2010-11-06 14:44:38 +03:00
|
|
|
end
|
|
|
|
end
|