mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 08:33:39 +08:00
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:
parent
9d35240620
commit
83e1315e42
26
app/jobs/onceoff/correct_missing_dualstack_urls.rb
Normal file
26
app/jobs/onceoff/correct_missing_dualstack_urls.rb
Normal 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
|
65
spec/jobs/correct_missing_dualstack_urls_spec.rb
Normal file
65
spec/jobs/correct_missing_dualstack_urls_spec.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user