2012-02-16 07:47:08 +08:00
|
|
|
class FixUserMergeConversations < ActiveRecord::Migration
|
2012-04-25 02:23:44 +08:00
|
|
|
tag :postdeploy
|
|
|
|
|
2012-02-16 07:47:08 +08:00
|
|
|
def self.up
|
2013-10-26 06:12:54 +08:00
|
|
|
disable_ddl_transaction!
|
2012-02-16 07:47:08 +08:00
|
|
|
|
|
|
|
# remove any duplicate CP's, possibly fixing private conversation hashes
|
|
|
|
# (which may merge it with another conversation)
|
|
|
|
ConversationParticipant.find_by_sql(<<-SQL).
|
|
|
|
SELECT conversation_participants.*
|
2015-07-16 05:42:27 +08:00
|
|
|
FROM #{ConversationParticipant.quoted_table_name}, (
|
2012-02-16 07:47:08 +08:00
|
|
|
SELECT MIN(id) AS id, user_id, conversation_id
|
2015-07-16 05:42:27 +08:00
|
|
|
FROM #{ConversationParticipant.quoted_table_name}
|
2012-02-16 07:47:08 +08:00
|
|
|
GROUP BY user_id, conversation_id
|
|
|
|
HAVING COUNT(*) > 1
|
|
|
|
ORDER BY conversation_id
|
|
|
|
) cps2keep
|
|
|
|
WHERE conversation_participants.user_id = cps2keep.user_id
|
|
|
|
AND conversation_participants.conversation_id = cps2keep.conversation_id
|
|
|
|
AND conversation_participants.id <> cps2keep.id
|
|
|
|
SQL
|
|
|
|
each do |cp|
|
|
|
|
cp.destroy
|
2012-02-24 04:12:31 +08:00
|
|
|
cp.conversation.regenerate_private_hash! if cp.private?
|
2012-02-16 07:47:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# there may be a bunch more private conversations with the wrong private
|
|
|
|
# hash, and there's not a reliable way to figure out which ones those are
|
|
|
|
# in sql alone (unless you have a sha1 method for postgres and sqlite), so
|
|
|
|
# we just walk them all out of band and make sure they're right (this may
|
|
|
|
# also merge some private conversations in the process)
|
2015-09-16 11:46:47 +08:00
|
|
|
Conversation.where("private_hash IS NOT NULL").pluck(:id).each_slice(1000) do |ids|
|
2012-02-16 07:47:08 +08:00
|
|
|
Conversation.send_later_if_production_enqueue_args(:batch_regenerate_private_hashes!, {
|
|
|
|
:priority => Delayed::LOWER_PRIORITY,
|
|
|
|
:max_attempts => 1,
|
|
|
|
:strand => "regenerate_conversation_private_hashes"
|
|
|
|
}, ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.down
|
|
|
|
raise ActiveRecord::IrreversibleMigration
|
|
|
|
end
|
|
|
|
end
|