From 2faf3fe10da4285fe9d8a20801d71770e79232f7 Mon Sep 17 00:00:00 2001 From: Raphael Sofaer <raphael@joindiaspora.com> Date: Fri, 11 Feb 2011 13:16:56 -0800 Subject: [PATCH] delete dup pvs in migration --- ...11204804_unique_index_post_visibilities.rb | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/db/migrate/20110211204804_unique_index_post_visibilities.rb b/db/migrate/20110211204804_unique_index_post_visibilities.rb index 4e11ea684e..624a407d2d 100644 --- a/db/migrate/20110211204804_unique_index_post_visibilities.rb +++ b/db/migrate/20110211204804_unique_index_post_visibilities.rb @@ -1,5 +1,28 @@ class UniqueIndexPostVisibilities < ActiveRecord::Migration def self.up + sql = <<-SQL + SELECT `post_visibilities`.post_id, `post_visibilities`.aspect_id FROM `post_visibilities` + GROUP BY post_id, aspect_id + HAVING COUNT(*)>1; + SQL + + result = execute(sql) + dup_pvs = result.to_a + undesired_ids = [] + + dup_pvs.each do |arr| + post_id, aspect_id = arr + pv_ids = execute(" + SELECT `post_visibilities`.id FROM `post_visibilities` + WHERE `post_visibilities`.post_id = #{post_id} + AND `post_visibilities`.aspect_id = #{aspect_id};" + ) + pv_ids.pop + undesired_ids.concat(pv_ids) + end + execute("DELETE FROM `post_visibilities` WHERE `post_visibiilties`.id IN (#{undesired_ids.join(",")});") unless undesired_ids.empty? + + remove_index :post_visibilities, [:aspect_id, :post_id] add_index :post_visibilities, [:aspect_id, :post_id], :unique => true end -- GitLab