FIX: `uploads:fix_missing_s3` rake task used wrong SHA1 (PR #12495)

GitHub

not following here, upload did not change between the 2 lines, is sha being updated somewhere as a side effect?

We assign a random hex as sha1:

Rolling back the transaction doesn’t affect the state of the upload object. The next update writes the random hex number into the DB:

@SamSaffron I took another look and my initial suspicion was right after all, but I refactored my original PR into a one-liner.

Here’s a simple demo of the problem:

ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)

id = Upload.last.id
upload = Upload.find(id)
original_sha1 = upload.sha1

Upload.transaction do
  upload.update!(sha1: SecureRandom.hex)
  raise ActiveRecord::Rollback
end

puts Upload.find(id).sha1
upload.update!(verification_status: 0)
puts Upload.find(id).sha1

upload.update!(sha1: original_sha1)

And here’s the relevant log output:

   (0.0ms)  BEGIN
  Upload Update (0.4ms)  UPDATE "uploads" SET "sha1" = 'b3f67e355ddb366071ee8e088277194d', "updated_at" = '2021-03-24 14:31:05.380205' WHERE "uploads"."id" = 2
   (0.1ms)  ROLLBACK
  Upload Load (0.2ms)  SELECT * WHERE "uploads"."id" = 2 LIMIT 1
33dcda0dcceab8e1f8a4c6a9617a6c79ae0ddd1a
   (0.0ms)  BEGIN
  Upload Update (0.2ms)  UPDATE "uploads" SET "sha1" = 'b3f67e355ddb366071ee8e088277194d', "updated_at" = '2021-03-24 14:31:05.380205' WHERE "uploads"."id" = 2
   (8.1ms)  COMMIT
  Upload Load (0.2ms)  SELECT * WHERE "uploads"."id" = 2 LIMIT 1
b3f67e355ddb366071ee8e088277194d

As you can see, the upload.update!() outside the transaction is still updating the sha1 column even though it’s not part of the method call.

1 Like