FIX: Add onceoff job to remove double quotes from s3 etags

FIX: Add onceoff job to remove double quotes from s3 etags

diff --git a/app/jobs/onceoff/fix_s3_etags.rb b/app/jobs/onceoff/fix_s3_etags.rb
new file mode 100644
index 0000000..b2ae816
--- /dev/null
+++ b/app/jobs/onceoff/fix_s3_etags.rb
@@ -0,0 +1,11 @@
+module Jobs
+  class FixS3Etags < Jobs::Onceoff
+
+    def execute_onceoff(args)
+      [Upload, OptimizedImage].each do |model|
+        sql = "UPDATE #{model.table_name} SET etag = REGEXP_REPLACE(etag, '\"', '', 'g') WHERE etag LIKE '\"%\"'"
+        DB.exec(sql)
+      end
+    end
+  end
+end
diff --git a/spec/jobs/fix_s3_etags_spec.rb b/spec/jobs/fix_s3_etags_spec.rb
new file mode 100644
index 0000000..85ad2aa
--- /dev/null
+++ b/spec/jobs/fix_s3_etags_spec.rb
@@ -0,0 +1,22 @@
+require 'rails_helper'
+
+RSpec.describe Jobs::FixS3Etags do
+  let(:etag_with_quotes) { '"ETag"' }
+  let(:etag_without_quotes) { 'ETag' }
+
+  it 'should remove double quotes from etags' do
+    upload1 = Fabricate(:upload, etag: etag_with_quotes)
+    upload2 = Fabricate(:upload, etag: etag_without_quotes)
+    optimized = Fabricate(:optimized_image, etag: etag_with_quotes)
+
+    described_class.new.execute_onceoff({})
+
+    upload1.reload
+    upload2.reload
+    optimized.reload
+
+    expect(upload1.etag).to eq(etag_without_quotes)
+    expect(upload2.etag).to eq(etag_without_quotes)
+    expect(optimized.etag).to eq(etag_without_quotes)
+  end
+end

GitHub sha: 484bd822

1 Like