github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/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/osdi23p228/fabric/common/ledger/blkstorage"
    11  	"github.com/osdi23p228/fabric/common/ledger/util/leveldbhelper"
    12  	"github.com/osdi23p228/fabric/core/ledger"
    13  	"github.com/osdi23p228/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // UpgradeDBs upgrades existing ledger databases to the latest formats.
    18  // It checks the format of idStore and does not drop any databases
    19  // if the format is already the latest version. Otherwise, it drops
    20  // ledger databases and upgrades the idStore format.
    21  func UpgradeDBs(config *ledger.Config) error {
    22  	rootFSPath := config.RootFSPath
    23  	fileLockPath := fileLockPath(rootFSPath)
    24  	fileLock := leveldbhelper.NewFileLock(fileLockPath)
    25  	if err := fileLock.Lock(); err != nil {
    26  		return errors.Wrap(err, "as another peer node command is executing,"+
    27  			" wait for that command to complete its execution or terminate it before retrying")
    28  	}
    29  	defer fileLock.Unlock()
    30  
    31  	logger.Infof("Ledger data folder from config = [%s]", rootFSPath)
    32  
    33  	if config.StateDBConfig.StateDatabase == "CouchDB" {
    34  		if err := statecouchdb.DropApplicationDBs(config.StateDBConfig.CouchDB); err != nil {
    35  			return err
    36  		}
    37  	}
    38  	if err := dropDBs(rootFSPath); err != nil {
    39  		return err
    40  	}
    41  	if err := blkstorage.DeleteBlockStoreIndex(BlockStorePath(rootFSPath)); err != nil {
    42  		return err
    43  	}
    44  
    45  	dbPath := LedgerProviderPath(rootFSPath)
    46  	db := leveldbhelper.CreateDB(&leveldbhelper.Conf{DBPath: dbPath})
    47  	db.Open()
    48  	defer db.Close()
    49  	idStore := &idStore{db, dbPath}
    50  	return idStore.upgradeFormat()
    51  }