From bca855d80e7a537b1bdfd2040d2c47b8b00d53fd Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 15 Aug 2014 14:56:48 -0400 Subject: [PATCH] FIX: from Sam: post_actions.targets_topic should not be nullable. Delete duplicate likes and prevent them from happening again. --- ...0140815183851_fix_index_on_post_actions.rb | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 db/migrate/20140815183851_fix_index_on_post_actions.rb diff --git a/db/migrate/20140815183851_fix_index_on_post_actions.rb b/db/migrate/20140815183851_fix_index_on_post_actions.rb new file mode 100644 index 000000000..ed403da39 --- /dev/null +++ b/db/migrate/20140815183851_fix_index_on_post_actions.rb @@ -0,0 +1,26 @@ +class FixIndexOnPostActions < ActiveRecord::Migration + def change + execute 'UPDATE post_actions SET targets_topic = false WHERE targets_topic IS NULL' + change_column :post_actions, :targets_topic, :boolean, default: false, null: false + + execute ' + DELETE FROM post_actions pa + USING post_actions x + WHERE pa.user_id = x.user_id AND + pa.post_action_type_id = x.post_action_type_id AND + pa.post_id = x.post_id AND + pa.targets_topic = x.targets_topic AND + pa.id < x.id AND + pa.deleted_at IS NULL AND + x.deleted_at IS NULL + ' + + remove_index "post_actions", name: "idx_unique_actions" + add_index "post_actions", + ["user_id", "post_action_type_id", + "post_id", "targets_topic"], + name: "idx_unique_actions", + unique: true, + where: 'deleted_at IS NULL' + end +end