From c83914e2e5aae589ff851ecd84876e708fec273e Mon Sep 17 00:00:00 2001 From: Leonardo Mosquera Date: Thu, 29 Jun 2023 13:22:47 -0300 Subject: [PATCH] FIX: fix normalize_raw method for nil inputs in migration scripts (#22304) Various migration scripts define a normalize_raw method to do custom processing of post contents before storing it in the Post.raw and other fields. They normally do not handle nil inputs, but it's a relatively common occurrence in data dumps. Since this method is used from various points in the migration script, as it stands, the experience of using a migration script is that it will fail multiple times at different points, forcing you to fix the data or apply logic hacks every time then restarting. This PR generalizes handling of nil input by returning a string. Pros: no more messy repeated crashes + restarts consistency Cons: it might hide data issues OTOH we can't print a warning on that method because it will flood the console since it's called from inside loops. * FIX: zendesk import script: support nil inputs in normalize_raw * FIX: return '' instead of empty string; do it for all methods --- script/import_scripts/bespoke_1.rb | 2 ++ script/import_scripts/jive.rb | 2 ++ script/import_scripts/yammer.rb | 2 ++ script/import_scripts/zendesk.rb | 2 ++ script/import_scripts/zendesk_api.rb | 2 ++ 5 files changed, 10 insertions(+) diff --git a/script/import_scripts/bespoke_1.rb b/script/import_scripts/bespoke_1.rb index bb120f98fe6..dd4290fbcbc 100644 --- a/script/import_scripts/bespoke_1.rb +++ b/script/import_scripts/bespoke_1.rb @@ -160,6 +160,8 @@ class ImportScripts::Bespoke < ImportScripts::Base end def normalize_raw!(raw) + return "" if raw.blank? + # purple and #1223f3 raw.gsub!(/\[color=[#a-z0-9]+\]/i, "") raw.gsub!(%r{\[/color\]}i, "") diff --git a/script/import_scripts/jive.rb b/script/import_scripts/jive.rb index 7e59a0f5a01..3d5d2a5d82e 100644 --- a/script/import_scripts/jive.rb +++ b/script/import_scripts/jive.rb @@ -196,6 +196,8 @@ class ImportScripts::Jive < ImportScripts::Base end def normalize_raw!(raw) + return "" if raw.blank? + raw = raw.dup raw = raw[5..-6] diff --git a/script/import_scripts/yammer.rb b/script/import_scripts/yammer.rb index 907ca4a6be8..f367a73cbfd 100644 --- a/script/import_scripts/yammer.rb +++ b/script/import_scripts/yammer.rb @@ -468,6 +468,8 @@ class ImportScripts::Yammer < ImportScripts::Base end def normalize_raw(raw) + return "" if raw.blank? + raw = raw.gsub('\n', "") raw.gsub!(/\[\[user:(\d+)\]\]/) do u = Regexp.last_match(1) diff --git a/script/import_scripts/zendesk.rb b/script/import_scripts/zendesk.rb index a8b5ef59ac5..4eb010aef62 100644 --- a/script/import_scripts/zendesk.rb +++ b/script/import_scripts/zendesk.rb @@ -211,6 +211,8 @@ class ImportScripts::Zendesk < ImportScripts::Base end def normalize_raw(raw) + return "" if raw.blank? + raw = raw.gsub('\n', "") raw = ReverseMarkdown.convert(raw) raw diff --git a/script/import_scripts/zendesk_api.rb b/script/import_scripts/zendesk_api.rb index d76ff1652dc..ae9d948413d 100644 --- a/script/import_scripts/zendesk_api.rb +++ b/script/import_scripts/zendesk_api.rb @@ -345,6 +345,8 @@ class ImportScripts::ZendeskApi < ImportScripts::Base end def normalize_raw(raw, user_id) + return "" if raw.blank? + raw = raw.gsub('\n', "") raw = ReverseMarkdown.convert(raw)