From 83e1315e42900b4052522bf14da83cf6985aae01 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 5 Sep 2018 15:57:42 +1000 Subject: [PATCH] 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. --- .../onceoff/correct_missing_dualstack_urls.rb | 26 ++++++++ .../correct_missing_dualstack_urls_spec.rb | 65 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 app/jobs/onceoff/correct_missing_dualstack_urls.rb create mode 100644 spec/jobs/correct_missing_dualstack_urls_spec.rb diff --git a/app/jobs/onceoff/correct_missing_dualstack_urls.rb b/app/jobs/onceoff/correct_missing_dualstack_urls.rb new file mode 100644 index 00000000000..f60e1c73ab1 --- /dev/null +++ b/app/jobs/onceoff/correct_missing_dualstack_urls.rb @@ -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 diff --git a/spec/jobs/correct_missing_dualstack_urls_spec.rb b/spec/jobs/correct_missing_dualstack_urls_spec.rb new file mode 100644 index 00000000000..493aaf6476e --- /dev/null +++ b/spec/jobs/correct_missing_dualstack_urls_spec.rb @@ -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