retry blob entry migration once on unique violation
This commit is contained in:
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user