FIX: Onceoff job to fix missing user profile backgrounds.

This commit is contained in:
Guo Xiang Tan 2018-10-01 16:06:07 +08:00
parent b6e7992a3d
commit cf60ae32ea
2 changed files with 64 additions and 0 deletions

View File

@ -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

View File

@ -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