github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/migration/step_05.go (about)

     1  // Copyright 2024 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package migration
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"os"
    11  
    12  	"github.com/ethersphere/bee/v2/pkg/log"
    13  	"github.com/ethersphere/bee/v2/pkg/storage"
    14  	"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
    15  	"github.com/ethersphere/bee/v2/pkg/storer/internal/upload"
    16  )
    17  
    18  // step_05 is a migration step that removes all upload items from the store.
    19  func step_05(st transaction.Storage) func() error {
    20  	return func() error {
    21  		logger := log.NewLogger("migration-step-05", log.WithSink(os.Stdout))
    22  		logger.Info("start removing upload items")
    23  
    24  		itemC := make(chan storage.Item)
    25  		errC := make(chan error)
    26  		go func() {
    27  			for item := range itemC {
    28  				err := st.Run(context.Background(), func(s transaction.Store) error {
    29  					return s.IndexStore().Delete(item)
    30  				})
    31  				if err != nil {
    32  					errC <- fmt.Errorf("delete upload item: %w", err)
    33  					return
    34  				}
    35  			}
    36  			close(errC)
    37  		}()
    38  
    39  		err := upload.IterateAll(st.IndexStore(), func(u storage.Item) (bool, error) {
    40  			select {
    41  			case itemC <- u:
    42  			case err := <-errC:
    43  				return true, err
    44  			}
    45  			return false, nil
    46  		})
    47  		close(itemC)
    48  		if err != nil {
    49  			return err
    50  		}
    51  
    52  		logger.Info("finished removing upload items")
    53  		return <-errC
    54  	}
    55  
    56  }