Make post guids unique

This commit is contained in:
Raphael Sofaer 2011-03-17 16:54:25 -07:00
parent 379f242a1f
commit 0686c28692
2 changed files with 38 additions and 2 deletions

View file

@ -0,0 +1,36 @@
class GuidIsUnique < ActiveRecord::Migration
def self.consolidate_post(guid)
post_ids = execute("select posts.id from posts where posts.guid = #{guid}").to_a.flatten!
keep_id = post_ids.pop
execute("UPDATE comments
SET comments.post_id = #{keep_id}
WHERE comments.post_id IN (#{post_ids.join(',')})")
execute("UPDATE posts
SET posts.status_message_id = #{keep_id}
WHERE posts.status_message_id IN (#{post_ids.join(',')})")
execute("DELETE post_visibilities WHERE post_visibilities.post_id IN (#{post_ids.join(',')})")
execute("DELETE mentions WHERE mentions.post_id IN (#{post_ids.join(',')})")
execute("DELETE posts WHERE posts.id IN (#{post_ids.join(',')})")
end
def self.up
sql = <<-SQL
SELECT posts.guid FROM posts
GROUP BY posts.guid
HAVING COUNT(*)>1;
SQL
duplicated_guids = execute(sql).to_a.flatten!
duplicated_guids.each do |guid|
consolidate_post(guid)
end if duplicated_guids
remove_index :posts, :guid
add_index :posts, :guid, :unique => true
end
def self.down
remove_index :posts, :column => :guid
add_index :posts, :guid
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110314043119) do
ActiveRecord::Schema.define(:version => 20110317222802) do
create_table "aspect_memberships", :force => true do |t|
t.integer "aspect_id", :null => false
@ -213,7 +213,7 @@ ActiveRecord::Schema.define(:version => 20110314043119) do
end
add_index "posts", ["author_id"], :name => "index_posts_on_person_id"
add_index "posts", ["guid"], :name => "index_posts_on_guid"
add_index "posts", ["guid"], :name => "index_posts_on_guid", :unique => true
add_index "posts", ["mongo_id"], :name => "index_posts_on_mongo_id"
add_index "posts", ["status_message_id", "pending"], :name => "index_posts_on_status_message_id_and_pending"
add_index "posts", ["status_message_id"], :name => "index_posts_on_status_message_id"