Skip to content

Commit fc2be98

Browse files
committed
fix logic
1 parent a2d59cc commit fc2be98

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

rollup/internal/orm/blob_upload.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"gorm.io/gorm"
10-
"gorm.io/gorm/clause"
1110

1211
"scroll-tech/common/types"
1312
)
@@ -95,18 +94,30 @@ func (o *BlobUpload) InsertOrUpdateBlobUpload(ctx context.Context, batchIndex ui
9594
db = dbTX[0]
9695
}
9796
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)
103116
}
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)
110120
}
121+
111122
return nil
112123
}

0 commit comments

Comments
 (0)