From 241e34e6cf0cd74ef5ca592d346e2ac578ed8c2d Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 7 Mar 2022 09:45:20 +0000 Subject: [PATCH] FIX: Update and rebake uses of the old centralized avatar service (#16086) This URL was originally updated in 89cb537fae8a563b22d873ea8efd3009fd32b042. However, some sites are not using the proxy, and have configured their forum to hotlink images directly to avatars.discourse.org. We intend to shut down this domain in favor of `avatars.discourse-cdn.com`, so this migration will re-write any matching site setting values and queue affected posts for rebaking. --- ...0302163246_update_avatar_service_domain.rb | 27 +++++++++++++++++ ...02171443_rebake_old_avatar_service_urls.rb | 30 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 db/migrate/20220302163246_update_avatar_service_domain.rb create mode 100644 db/post_migrate/20220302171443_rebake_old_avatar_service_urls.rb diff --git a/db/migrate/20220302163246_update_avatar_service_domain.rb b/db/migrate/20220302163246_update_avatar_service_domain.rb new file mode 100644 index 00000000000..b602c501a55 --- /dev/null +++ b/db/migrate/20220302163246_update_avatar_service_domain.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class UpdateAvatarServiceDomain < ActiveRecord::Migration[6.1] + def up + existing_value = DB.query_single("SELECT value FROM site_settings WHERE name = 'external_system_avatars_url'")&.[](0) + + if existing_value&.include?("avatars.discourse.org") + new_value = DB.query_single(<<~SQL)&.[](0) + UPDATE site_settings + SET value = REPLACE(value, 'avatars.discourse.org', 'avatars.discourse-cdn.com') + WHERE name = 'external_system_avatars_url' + AND value LIKE '%avatars.discourse.org%' + RETURNING value + SQL + + DB.exec <<~SQL, previous: existing_value, new: new_value + INSERT INTO user_histories + (action, subject, previous_value, new_value, admin_only, updated_at, created_at, acting_user_id) + VALUES (3, 'external_system_avatars_url', :previous, :new, true, NOW(), NOW(), -1) + SQL + end + end + + def down + # Nothing to do + end +end diff --git a/db/post_migrate/20220302171443_rebake_old_avatar_service_urls.rb b/db/post_migrate/20220302171443_rebake_old_avatar_service_urls.rb new file mode 100644 index 00000000000..84e70066b71 --- /dev/null +++ b/db/post_migrate/20220302171443_rebake_old_avatar_service_urls.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class RebakeOldAvatarServiceUrls < ActiveRecord::Migration[6.1] + def up + # Only need to run this migration if 20220302163246 + # changed the site setting. We can determine that + # by checking for a user_histories entry in the last + # month + + recently_changed = DB.query_single(<<~SQL).[](0) + SELECT 1 + FROM user_histories + WHERE action = 3 + AND subject = 'external_system_avatars_url' + AND previous_value LIKE '%avatars.discourse.org%' + AND created_at > NOW() - INTERVAL '1 month' + SQL + + if recently_changed + execute <<~SQL + UPDATE posts SET baked_version = 0 + WHERE cooked LIKE '%avatars.discourse.org%' + SQL + end + end + + def down + # Nothing to do + end +end