FIX: properly migrate polls/votes stored as array

FIX: properly migrate polls/votes stored as array

From ed400a90fe1f5e1622e7f28aaddfa5a1aa1afdf4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Sat, 24 Nov 2018 17:14:03 +0100
Subject: [PATCH] FIX: properly migrate polls/votes stored as array


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 36769de..51311ae 100644
--- a/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb
+++ b/plugins/poll/db/post_migrate/20180820080623_migrate_polls_data.rb
@@ -58,6 +58,10 @@ class MigratePollsData < ActiveRecord::Migration[5.2]
     SQL
 
     DB.query(sql).each do |r|
+      # for some reasons, polls or votes might be an array
+      r.polls = r.polls[0] if Array === r.polls && r.polls.size > 0
+      r.votes = r.votes[0] if Array === r.votes && r.votes.size > 0
+
       existing_user_ids = User.where(id: r.votes.keys).pluck(:id).to_set
 
       # Poll votes are stored in a JSON object with the following hierarchy

GitHub

2 Likes

Yeah this is the problem with custom fields, only way to provide a guarantee here would be to add an extra index. But then… it makes more sense just to roll out a new data model…

1 Like