FIX: correct urls in uploads table to point at dualstack

Last week we added support for dual stack urls but did not remap the
the old records in the uploads and optimized images table

This caused a few minor edge cases worst was that if you rebaked old
images S3 CDN was not repopulated.
This commit is contained in:
Sam 2018-09-05 15:57:42 +10:00
parent 9d35240620
commit 83e1315e42
2 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,26 @@
module Jobs
class CorrectMissingDualstackUrls < Jobs::Onceoff
def execute_onceoff(args)
# s3 now uses dualstack urls, keep them around correctly
# in both uploads and optimized_image tables
base_url = Discourse.store.absolute_base_url
return if !base_url.match?(/s3\.dualstack/)
old = base_url.sub('.dualstack', '')
old_like = %"#{old}%"
DB.exec(<<~SQL, from: old, to: base_url, old_like: old_like)
UPDATE uploads
SET url = replace(url, :from, :to)
WHERE url ilike :old_like
SQL
DB.exec(<<~SQL, from: old, to: base_url, old_like: old_like)
UPDATE optimized_images
SET url = replace(url, :from, :to)
WHERE url ilike :old_like
SQL
end
end
end

View File

@ -0,0 +1,65 @@
require 'rails_helper'
require_dependency 'jobs/onceoff/correct_missing_dualstack_urls'
describe Jobs::CorrectMissingDualstackUrls do
it 'corrects the urls' 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
# we will only correct for our base_url, random urls will be left alone
expect(Discourse.store.absolute_base_url).to eq('//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com')
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,
)
bad_upload = Upload.create!(
url: '//s3-upload-bucket.s3.us-west-1.amazonaws.com/somewhere/a.png',
original_filename: 'a.png',
filesize: 100,
user_id: -1,
)
current_optimized = OptimizedImage.create!(
url: '//s3-upload-bucket.s3.us-east-1.amazonaws.com/somewhere/a.png',
filesize: 100,
upload_id: current_upload.id,
width: 100,
height: 100,
sha1: 'xxx',
extension: '.png'
)
bad_optimized = OptimizedImage.create!(
url: '//s3-upload-bucket.s3.us-west-1.amazonaws.com/somewhere/a.png',
filesize: 100,
upload_id: current_upload.id,
width: 110,
height: 100,
sha1: 'xxx',
extension: '.png'
)
Jobs::CorrectMissingDualstackUrls.new.execute_onceoff(nil)
bad_upload.reload
expect(bad_upload.url).to eq('//s3-upload-bucket.s3.us-west-1.amazonaws.com/somewhere/a.png')
current_upload.reload
expect(current_upload.url).to eq('//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com/somewhere/a.png')
bad_optimized.reload
expect(bad_optimized.url).to eq('//s3-upload-bucket.s3.us-west-1.amazonaws.com/somewhere/a.png')
current_optimized.reload
expect(current_optimized.url).to eq('//s3-upload-bucket.s3.dualstack.us-east-1.amazonaws.com/somewhere/a.png')
end
end