retry blob entry migration once on unique violation

This commit is contained in:
Dylan Knutson
2025-03-02 19:29:51 +00:00
parent e49fe33dc6
commit 1a84b885f2

View File

@@ -97,16 +97,23 @@ class BlobFile < ReduxApplicationRecord
sig { params(sha256: String).returns(T.nilable(BlobFile)) }
def self.migrate_sha256!(sha256)
retried = T.let(false, T::Boolean)
# convert to binary if hex formatted
sha256 = HexUtil.hex2bin(sha256) if sha256.length == 64
blob_file = BlobFile.find_by(sha256: sha256)
return blob_file if blob_file
blob_entry = BlobEntry.find_by(sha256: sha256)
return nil unless blob_entry
blob_file = BlobFile.find_or_initialize_from_blob_entry(blob_entry)
return nil unless blob_file.save
logger.info("migrated sha256 #{HexUtil.bin2hex(sha256)} to blob_file")
blob_file
begin
blob_file = BlobFile.find_by(sha256: sha256)
return blob_file if blob_file
blob_entry = BlobEntry.find_by(sha256: sha256)
return nil unless blob_entry
blob_file = BlobFile.find_or_initialize_from_blob_entry(blob_entry)
return nil unless blob_file.save
logger.info("migrated sha256 #{HexUtil.bin2hex(sha256)} to blob_file")
blob_file
rescue ActiveRecord::RecordNotUnique => e
raise e if retried
retried = true
retry
end
end
sig { params(content_bytes: T.nilable(String)).void }