From ef642a27ef23b5104f728a6563b28c2365b3d67c Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Mon, 4 Sep 2023 17:17:53 +0800 Subject: [PATCH] only call delete once --- rollup/internal/orm/l1_block.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/rollup/internal/orm/l1_block.go b/rollup/internal/orm/l1_block.go index 9d2a21e858..7a1be88d6a 100644 --- a/rollup/internal/orm/l1_block.go +++ b/rollup/internal/orm/l1_block.go @@ -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 {