FIX: Onceoff job to fix missing user profile backgrounds.

FIX: Onceoff job to fix missing user profile backgrounds.

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 0000000..067b190
--- /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 0000000..27a3851
--- /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

GitHub sha: cf60ae32

This method does not exist on the stable branch.

NoMethodError: undefined method `recover_user_profile_backgrounds' for #<UploadRecovery:0x00007fb0f8ae34d0 @dry_run=false>

cc @tgxworld @SamSaffron

This commit has been mentioned on Discourse Meta. There might be relevant details there: