FIX: Skip migrating null/blank custom field values in migrations

FIX: Skip migrating null/blank custom field values in migrations

diff --git a/db/migrate/20200728222920_create_discourse_voting_votes.rb b/db/migrate/20200728222920_create_discourse_voting_votes.rb
index 9035349..0e3633e 100644
--- a/db/migrate/20200728222920_create_discourse_voting_votes.rb
+++ b/db/migrate/20200728222920_create_discourse_voting_votes.rb
@@ -15,6 +15,7 @@ class CreateDiscourseVotingVotes < ActiveRecord::Migration[6.0]
       SELECT value::integer, user_id, 'false', created_at, updated_at
       FROM user_custom_fields
       WHERE name = 'votes'
+      AND value <> ''
       ON CONFLICT(user_id, topic_id) DO NOTHING
     SQL
 
@@ -23,6 +24,7 @@ class CreateDiscourseVotingVotes < ActiveRecord::Migration[6.0]
       SELECT value::integer, user_id, 'true', created_at, updated_at
       FROM user_custom_fields
       WHERE name = 'votes_archive'
+      AND value <> ''
       ON CONFLICT(user_id, topic_id) DO NOTHING
     SQL
   end
diff --git a/db/migrate/20200729042607_create_discourse_voting_topic_vote_count.rb b/db/migrate/20200729042607_create_discourse_voting_topic_vote_count.rb
index 4a24625..ff59571 100644
--- a/db/migrate/20200729042607_create_discourse_voting_topic_vote_count.rb
+++ b/db/migrate/20200729042607_create_discourse_voting_topic_vote_count.rb
@@ -14,6 +14,7 @@ class CreateDiscourseVotingTopicVoteCount < ActiveRecord::Migration[6.0]
       SELECT topic_id::integer, value::integer, created_at, updated_at
       FROM topic_custom_fields
       WHERE name = 'vote_count'
+      AND value <> ''
     SQL
   end
 

GitHub sha: 835b3580

1 Like

What about sites that have already ran this migration before this change? Do we need another migration to clean up these blank values or is it not really an issue?

2 Likes

This change is to deal with sites which had corrupt custom fields. For example:

For sites with bad data, the migration was failing with

ERROR:  invalid input syntax for type integer: ""

So if the migration already succeeded, the data is already valid, and I don’t think we need to do anything :smiley:

2 Likes