github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/upgrade_dbs.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package kvledger
     8  
     9  import (
    10  	"github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage"
    11  	"github.com/hyperledger/fabric/common/ledger/dataformat"
    12  	"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  // UpgradeDBs upgrades existing ledger databases to the latest formats
    17  func UpgradeDBs(rootFSPath string) error {
    18  	fileLockPath := fileLockPath(rootFSPath)
    19  	fileLock := leveldbhelper.NewFileLock(fileLockPath)
    20  	if err := fileLock.Lock(); err != nil {
    21  		return errors.Wrap(err, "as another peer node command is executing,"+
    22  			" wait for that command to complete its execution or terminate it before retrying")
    23  	}
    24  	defer fileLock.Unlock()
    25  
    26  	logger.Infof("Ledger data folder from config = [%s]", rootFSPath)
    27  
    28  	// upgrades idStore (ledgerProvider) and drop databases
    29  	if err := UpgradeIDStoreFormat(rootFSPath); err != nil {
    30  		return err
    31  	}
    32  
    33  	if err := dropDBs(rootFSPath); err != nil {
    34  		return err
    35  	}
    36  
    37  	blockstorePath := BlockStorePath(rootFSPath)
    38  	return fsblkstorage.DeleteBlockStoreIndex(blockstorePath)
    39  }
    40  
    41  // UpgradeIDStoreFormat upgrades the format for idStore
    42  func UpgradeIDStoreFormat(rootFSPath string) error {
    43  	logger.Debugf("Attempting to upgrade idStore data format to current format %s", dataformat.Version20)
    44  
    45  	dbPath := LedgerProviderPath(rootFSPath)
    46  	db := leveldbhelper.CreateDB(&leveldbhelper.Conf{DBPath: dbPath})
    47  	db.Open()
    48  	defer db.Close()
    49  
    50  	idStore := &idStore{db, dbPath}
    51  	return idStore.upgradeFormat()
    52  }