github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/cmd/tendermint/commands/key_migrate.go (about)

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/ari-anchor/sei-tendermint/config"
    10  	"github.com/ari-anchor/sei-tendermint/libs/log"
    11  	"github.com/ari-anchor/sei-tendermint/scripts/keymigrate"
    12  	"github.com/ari-anchor/sei-tendermint/scripts/scmigrate"
    13  )
    14  
    15  func MakeKeyMigrateCommand(conf *config.Config, logger log.Logger) *cobra.Command {
    16  	cmd := &cobra.Command{
    17  		Use:   "key-migrate",
    18  		Short: "Run Database key migration",
    19  		RunE: func(cmd *cobra.Command, args []string) error {
    20  			return RunDatabaseMigration(cmd.Context(), logger, conf)
    21  		},
    22  	}
    23  
    24  	// allow database info to be overridden via cli
    25  	addDBFlags(cmd, conf)
    26  
    27  	return cmd
    28  }
    29  
    30  func RunDatabaseMigration(ctx context.Context, logger log.Logger, conf *config.Config) error {
    31  	contexts := []string{
    32  		// this is ordered to put
    33  		// the more ephemeral tables first to
    34  		// reduce the possibility of the
    35  		// ephemeral data overwriting later data
    36  		"tx_index",
    37  		"peerstore",
    38  		"light",
    39  		"blockstore",
    40  		"state",
    41  		"evidence",
    42  	}
    43  
    44  	for idx, dbctx := range contexts {
    45  		logger.Info("beginning a key migration",
    46  			"dbctx", dbctx,
    47  			"num", idx+1,
    48  			"total", len(contexts),
    49  		)
    50  
    51  		db, err := config.DefaultDBProvider(&config.DBContext{
    52  			ID:     dbctx,
    53  			Config: conf,
    54  		})
    55  
    56  		if err != nil {
    57  			return fmt.Errorf("constructing database handle: %w", err)
    58  		}
    59  
    60  		if err = keymigrate.Migrate(ctx, db); err != nil {
    61  			return fmt.Errorf("running migration for context %q: %w",
    62  				dbctx, err)
    63  		}
    64  
    65  		if dbctx == "blockstore" {
    66  			if err := scmigrate.Migrate(ctx, db); err != nil {
    67  				return fmt.Errorf("running seen commit migration: %w", err)
    68  
    69  			}
    70  		}
    71  	}
    72  
    73  	logger.Info("completed database migration successfully")
    74  
    75  	return nil
    76  }