code.vegaprotocol.io/vega@v0.79.0/cmd/blockexplorer/commands/init_db.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/blockexplorer/config"
    23  	"code.vegaprotocol.io/vega/blockexplorer/store"
    24  	"code.vegaprotocol.io/vega/logging"
    25  
    26  	"github.com/jessevdk/go-flags"
    27  )
    28  
    29  type InitDBCmd struct {
    30  	config.VegaHomeFlag
    31  }
    32  
    33  func (opts *InitDBCmd) Execute(_ []string) error {
    34  	logger := logging.NewLoggerFromConfig(logging.NewDefaultConfig())
    35  	defer logger.AtExit()
    36  
    37  	config, err := loadConfig(logger, opts.VegaHome)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	err = store.MigrateToLatestSchema(logger, config.Store)
    43  	if err != nil {
    44  		return fmt.Errorf("creating db schema: %w", err)
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  var initDBCmd InitDBCmd
    51  
    52  func InitDB(ctx context.Context, parser *flags.Parser) error {
    53  	initDBCmd = InitDBCmd{}
    54  
    55  	short := "Initialize / update database schema"
    56  	long := "Creates, (or updates) database tables and views according to the schema required for the tendermint psql indexer"
    57  
    58  	_, err := parser.AddCommand("init-db", short, long, &initDBCmd)
    59  	return err
    60  }