github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/operator/migrate.go (about)

     1  package operator
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
     9  	"github.com/machinefi/w3bstream/pkg/depends/kit/logr"
    10  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
    11  	"github.com/machinefi/w3bstream/pkg/models"
    12  	"github.com/machinefi/w3bstream/pkg/types"
    13  )
    14  
    15  // will delete at next version
    16  func Migrate(ctx context.Context) {
    17  	ctx, l := logr.Start(ctx, "operator.Migrate")
    18  	defer l.End()
    19  
    20  	d := types.MustMgrDBExecutorFromContext(ctx)
    21  
    22  	a := &models.Account{}
    23  	as, err := a.List(d, nil)
    24  	if err != nil {
    25  		l.Error(errors.Wrap(err, "list account failed"))
    26  		return
    27  	}
    28  	for _, a := range as {
    29  		if a.OperatorPrivateKey == "" {
    30  			continue
    31  		}
    32  
    33  		id := confid.MustSFIDGeneratorFromContext(ctx).MustGenSFID()
    34  
    35  		op := &models.Operator{
    36  			RelAccount:  models.RelAccount{AccountID: a.AccountID},
    37  			RelOperator: models.RelOperator{OperatorID: id},
    38  			OperatorInfo: models.OperatorInfo{
    39  				Name:       DefaultOperatorName,
    40  				PrivateKey: a.OperatorPrivateKey,
    41  			},
    42  		}
    43  
    44  		if err := op.Create(d); err != nil {
    45  			if sqlx.DBErr(err).IsConflict() {
    46  				continue
    47  			}
    48  			l.Error(errors.Wrap(err, "create operator failed"))
    49  		}
    50  	}
    51  }