github.com/Finschia/finschia-sdk@v0.48.1/docs/building-modules/upgrade.md (about)

     1  <!--
     2  order: 13
     3  -->
     4  
     5  # Upgrading Modules
     6  
     7  [In-Place Store Migrations](../core/upgrade.html) allow your modules to upgrade to new versions that include breaking changes. This document outlines how to build modules to take advantage of this functionality. {synopsis}
     8  
     9  ## Prerequisite Readings
    10  
    11  - [In-Place Store Migration](../core/upgrade.md) {prereq}
    12  
    13  ## Consensus Version
    14  
    15  Successful upgrades of existing modules require each `AppModule` to implement the function `ConsensusVersion() uint64`.
    16  
    17  - The versions must be hard-coded by the module developer.
    18  - The initial version **must** be set to 1.
    19  
    20  Consensus versions serve as state-breaking versions of app modules and must be incremented when the module introduces breaking changes.
    21  
    22  ## Registering Migrations
    23  
    24  To register the functionality that takes place during a module upgrade, you must register which migrations you want to take place.
    25  
    26  Migration registration takes place in the `Configurator` using the `RegisterMigration` method. The `AppModule` reference to the configurator is in the `RegisterServices` method.
    27  
    28  You can register one or more migrations. If you register more than one migration script, list the migrations in increasing order and ensure there are enough migrations that lead to the desired consensus version. For example, to migrate to version 3 of a module, register separate migrations for version 1 and version 2 as shown in the following example:
    29  
    30  ```golang
    31  func (am AppModule) RegisterServices(cfg module.Configurator) {
    32      // --snip--
    33      cfg.RegisterMigration(types.ModuleName, 1, func(ctx sdk.Context) error {
    34          // Perform in-place store migrations from ConsensusVersion 1 to 2.
    35      })
    36       cfg.RegisterMigration(types.ModuleName, 2, func(ctx sdk.Context) error {
    37          // Perform in-place store migrations from ConsensusVersion 2 to 3.
    38      })
    39  }
    40  ```
    41  
    42  Since these migrations are functions that need access to a Keeper's store, use a wrapper around the keepers called `Migrator` as shown in this example:
    43  
    44  +++ https://github.com/cosmos/cosmos-sdk/blob/6ac8898fec9bd7ea2c1e5c79e0ed0c3f827beb55/x/bank/keeper/migrations.go#L8-L21
    45  
    46  ## Writing Migration Scripts
    47  
    48  To define the functionality that takes place during an upgrade, write a migration script. Since migration scripts manipulate legacy code, place these functions in a `legacy/` directory. For example, to write migration scripts for the bank module, place the functions in `x/bank/legacy/`. Use the recommended naming convention for these functions. For example, `v043bank` is the script that migrates this legacy package `x/bank/legacy/v043`:
    49  
    50  ```golang
    51  // Migrating bank module from version 1 to 2
    52  func (m Migrator) Migrate1to2(ctx sdk.Context) error {
    53  	return v043bank.MigrateStore(ctx, m.keeper.storeKey) // v043bank is package `x/bank/legacy/v043`.
    54  }
    55  ```
    56  
    57  To see example code of changes that were implemented in a migration of balance keys, check out [migrateBalanceKeys](https://github.com/cosmos/cosmos-sdk/blob/36f68eb9e041e20a5bb47e216ac5eb8b91f95471/x/bank/legacy/v043/store.go#L41-L62). For context, this code introduced migrations of the bank store that updated addresses to be prefixed by their length in bytes as outlined in [ADR-028](../architecture/adr-028-public-key-addresses.md).