From db9ae32e41098ece977c0725addc9fc91ef89429 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 4 May 2022 15:05:18 +0100 Subject: [PATCH] FIX: Skip invalid custom_field json in hotlinked_media migration (#16630) Truly testing for JSON validity would require defining a new postgres function. Checking just the first character should take care of all the cases of invalid historic data that we've seen. --- db/migrate/20220428094026_create_post_hotlinked_media.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db/migrate/20220428094026_create_post_hotlinked_media.rb b/db/migrate/20220428094026_create_post_hotlinked_media.rb index e152a488394..ff31138ccd6 100644 --- a/db/migrate/20220428094026_create_post_hotlinked_media.rb +++ b/db/migrate/20220428094026_create_post_hotlinked_media.rb @@ -46,6 +46,7 @@ class CreatePostHotlinkedMedia < ActiveRecord::Migration[6.1] JOIN json_each_text(pcf.value::json) obj ON true JOIN uploads ON obj.value::bigint = uploads.id WHERE name='downloaded_images' + AND pcf.value LIKE '{%' -- JSON Object ON CONFLICT (post_id, md5(url::text)) DO NOTHING SQL @@ -61,6 +62,7 @@ class CreatePostHotlinkedMedia < ActiveRecord::Migration[6.1] FROM post_custom_fields pcf JOIN json_array_elements_text(pcf.value::json) url ON true WHERE name='broken_images' + AND pcf.value LIKE '[%' -- JSON Array ON CONFLICT (post_id, md5(url::text)) DO NOTHING SQL @@ -76,6 +78,7 @@ class CreatePostHotlinkedMedia < ActiveRecord::Migration[6.1] FROM post_custom_fields pcf JOIN json_array_elements_text(pcf.value::json) url ON true WHERE name='large_images' + AND pcf.value LIKE '[%' -- JSON Array ON CONFLICT (post_id, md5(url::text)) DO NOTHING SQL end