Skip to content

Commit

Permalink
only call delete once
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Sep 4, 2023
1 parent cc43d17 commit ef642a2
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions rollup/internal/orm/l1_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ func (o *L1Block) InsertL1Blocks(ctx context.Context, blocks []L1Block) error {
}

return o.db.Transaction(func(tx *gorm.DB) error {
var blockNumbers []uint64
for _, block := range blocks {
blockNumbers = append(blockNumbers, block.Number)
}

db := tx.WithContext(ctx)
db = db.Model(&L1Block{})
for _, block := range blocks {
if err := db.Where("number = ?", block.Number).Delete(&L1Block{}).Error; err != nil {
return fmt.Errorf("L1Block.InsertL1Blocks error: soft deleting existing block with number: %d. Error: %w", block.Number, err)
}
if err := db.Where("number IN (?)", blockNumbers).Delete(&L1Block{}).Error; err != nil {
return fmt.Errorf("L1Block.InsertL1Blocks error: soft deleting blocks failed, block numbers: %v, error: %w", blockNumbers, err)
}

if err := db.Create(&blocks).Error; err != nil {
Expand Down

0 comments on commit ef642a2

Please sign in to comment.