| 
7 | 7 | 	"time"  | 
8 | 8 | 
 
  | 
9 | 9 | 	"gorm.io/gorm"  | 
10 |  | -	"gorm.io/gorm/clause"  | 
11 | 10 | 
 
  | 
12 | 11 | 	"scroll-tech/common/types"  | 
13 | 12 | )  | 
@@ -95,18 +94,30 @@ func (o *BlobUpload) InsertOrUpdateBlobUpload(ctx context.Context, batchIndex ui  | 
95 | 94 | 		db = dbTX[0]  | 
96 | 95 | 	}  | 
97 | 96 | 	db = db.WithContext(ctx)  | 
98 |  | -	blobUpload := &BlobUpload{  | 
99 |  | -		BatchIndex: batchIndex,  | 
100 |  | -		BatchHash:  batchHash,  | 
101 |  | -		Platform:   int16(platform),  | 
102 |  | -		Status:     int16(status),  | 
 | 97 | + | 
 | 98 | +	var existing BlobUpload  | 
 | 99 | +	err := db.Where("batch_index = ? AND batch_hash = ? AND platform = ? AND deleted_at IS NULL",  | 
 | 100 | +		batchIndex, batchHash, int16(platform),  | 
 | 101 | +	).First(&existing).Error  | 
 | 102 | + | 
 | 103 | +	if errors.Is(err, gorm.ErrRecordNotFound) {  | 
 | 104 | +		newRecord := BlobUpload{  | 
 | 105 | +			BatchIndex: batchIndex,  | 
 | 106 | +			BatchHash:  batchHash,  | 
 | 107 | +			Platform:   int16(platform),  | 
 | 108 | +			Status:     int16(status),  | 
 | 109 | +		}  | 
 | 110 | +		if err := db.Create(&newRecord).Error; err != nil {  | 
 | 111 | +			return fmt.Errorf("BlobUpload.InsertOrUpdateBlobUpload insert error: %w, batch index: %v, batch_hash: %v, platform: %v", err, batchIndex, batchHash, platform)  | 
 | 112 | +		}  | 
 | 113 | +		return nil  | 
 | 114 | +	} else if err != nil {  | 
 | 115 | +		return fmt.Errorf("BlobUpload.InsertOrUpdateBlobUpload query error: %w, batch index: %v, batch_hash: %v, platform: %v", err, batchIndex, batchHash, platform)  | 
103 | 116 | 	}  | 
104 |  | -	if err := db.Clauses(clause.OnConflict{  | 
105 |  | -		Columns:   []clause.Column{{Name: "batch_index"}, {Name: "batch_hash"}, {Name: "platform"}},  | 
106 |  | -		Where:     clause.Where{Exprs: []clause.Expression{clause.Eq{Column: "blob_upload.deleted_at", Value: nil}}},  | 
107 |  | -		DoUpdates: clause.AssignmentColumns([]string{"status"}),  | 
108 |  | -	}).Create(blobUpload).Error; err != nil {  | 
109 |  | -		return fmt.Errorf("BlobUpload.InsertOrUpdateBlobUpload error: %w, batch index: %v, platform: %v", err, batchIndex, platform)  | 
 | 117 | + | 
 | 118 | +	if err := db.Model(&existing).Update("status", int16(status)).Error; err != nil {  | 
 | 119 | +		return fmt.Errorf("BlobUpload.InsertOrUpdateBlobUpload update error: %w, batch index: %v, batch_hash: %v, platform: %v", err, batchIndex, batchHash, platform)  | 
110 | 120 | 	}  | 
 | 121 | + | 
111 | 122 | 	return nil  | 
112 | 123 | }  | 
0 commit comments