Fix `MigratePollsData` failing when poll-votes have duplicated options

Fix MigratePollsData failing when poll-votes have duplicated options.

From 439b3fe10a4cdac949a3fec60a0174c726caa435 Mon Sep 17 00:00:00 2001
From: Guo Xiang Tan <tgx_world@hotmail.com>
Date: Wed, 21 Nov 2018 10:52:21 +0800
Subject: [PATCH] Fix `MigratePollsData` failing when poll-votes have
 duplicated options.


diff --git a/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb b/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb
index 2cab457..36769de 100644
--- a/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb
+++ b/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb
@@ -134,7 +134,10 @@ class MigratePollsData < ActiveRecord::Migration[5.2]
             options
               .select { |o| option_ids.has_key?(o) }
               .map { |o| "(#{poll_id}, #{option_ids[o]}, #{user_id.to_i}, '#{r.created_at}', '#{r.updated_at}')" }
-          end.flatten
+          end
+
+          poll_votes.flatten!
+          poll_votes.uniq!
 
           if poll_votes.present?
             execute <<~SQL
diff --git a/plugins/poll/spec/db/post_migrate/migrate_polls_data_spec.rb b/plugins/poll/spec/db/post_migrate/migrate_polls_data_spec.rb
index f7e3096..829b048 100644
--- a/plugins/poll/spec/db/post_migrate/migrate_polls_data_spec.rb
+++ b/plugins/poll/spec/db/post_migrate/migrate_polls_data_spec.rb
@@ -90,7 +90,8 @@ RSpec.describe MigratePollsData do
           "1" => {
             "testing" => [
               "b2c3e3668a886d09e97e38b8adde7d45",
-              "28df49fa9e9c09d3a1eb8cfbcdcda7790",
+              "b2c3e3668a886d09e97e38b8adde7d45",
+              "28df49fa9e9c09d3a1eb8cfbcdcda7790"
             ]
           },
           "2" => {

GitHub

2 Likes