diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2010-11-06 11:44:38 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2010-11-06 11:44:38 +0000 |
commit | c50b611f851303f97c83c8eb080f3c051c957395 (patch) | |
tree | 2713a976f8794ae9a5eb399a2540b4dd4b95ef30 /db | |
parent | 73167fb4f26b34c1da9c9bd9c12d175442a8e56d (diff) | |
download | redmine-c50b611f851303f97c83c8eb080f3c051c957395.tar.gz redmine-c50b611f851303f97c83c8eb080f3c051c957395.zip |
Adds a unique index on members (#6826).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4369 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20101104182107_add_unique_index_on_members.rb | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/db/migrate/20101104182107_add_unique_index_on_members.rb b/db/migrate/20101104182107_add_unique_index_on_members.rb new file mode 100644 index 000000000..6b07e4647 --- /dev/null +++ b/db/migrate/20101104182107_add_unique_index_on_members.rb @@ -0,0 +1,20 @@ +class AddUniqueIndexOnMembers < ActiveRecord::Migration + def self.up + # Reassign MemberRole rows if needed + 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 + + # Then add a unique index + add_index :members, [:user_id, :project_id], :unique => true + end + + def self.down + end +end |