diff --git a/app/jobs/onceoff/recover_user_profile_backgrounds.rb b/app/jobs/onceoff/recover_user_profile_backgrounds.rb new file mode 100644 index 00000000000..067b1900c76 --- /dev/null +++ b/app/jobs/onceoff/recover_user_profile_backgrounds.rb @@ -0,0 +1,25 @@ +module Jobs + class RecoverUserProfileBackgrounds < Onceoff + def execute_onceoff(_) + base_url = Discourse.store.absolute_base_url + return if !base_url.match?(/s3\.dualstack/) + + old = base_url.sub('s3.dualstack.', 's3-') + old_like = %"#{old}%" + + DB.exec(<<~SQL, from: old, to: base_url, old_like: old_like) + UPDATE user_profiles + SET profile_background = replace(profile_background, :from, :to) + WHERE profile_background ilike :old_like + SQL + + DB.exec(<<~SQL, from: old, to: base_url, old_like: old_like) + UPDATE user_profiles + SET card_background = replace(card_background, :from, :to) + WHERE card_background ilike :old_like + SQL + + UploadRecovery.new.recover_user_profile_backgrounds + end + end +end diff --git a/spec/jobs/recover_user_profile_backgrounds_spec.rb b/spec/jobs/recover_user_profile_backgrounds_spec.rb new file mode 100644 index 00000000000..27a3851eab9 --- /dev/null +++ b/spec/jobs/recover_user_profile_backgrounds_spec.rb @@ -0,0 +1,39 @@ +require 'rails_helper' + +require_dependency 'jobs/onceoff/recover_user_profile_backgrounds' + +RSpec.describe Jobs::RecoverUserProfileBackgrounds do + let(:user_profile) { Fabricate(:user).user_profile } + + before do + SiteSetting.s3_upload_bucket = "s3-upload-bucket" + SiteSetting.s3_access_key_id = "s3-access-key-id" + SiteSetting.s3_secret_access_key = "s3-secret-access-key" + SiteSetting.enable_s3_uploads = true + end + + it "corrects the URL and recovers the uploads" do + current_upload = Upload.create!( + url: '//s3-upload-bucket.s3-us-east-1.amazonaws.com/somewhere/a.png', + original_filename: 'a.png', + filesize: 100, + user_id: -1, + ) + + user_profile.update!( + profile_background: current_upload.url, + card_background: current_upload.url + ) + + Jobs::RecoverUserProfileBackgrounds.new.execute_onceoff({}) + + user_profile.reload + + %i{card_background profile_background}.each do |column| + expect(user_profile.public_send(column)).to eq( + '//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com/somewhere/a.png' + ) + end + + end +end