Skip to content

Commit

Permalink
feat(ecocredit): add state migrations (#957)
Browse files Browse the repository at this point in the history
* feat: wip add ecocredit migrations

* wip: add credit batch migrations

* cleanup

* add tests

* chore: sort imports

* feat: register ecocredit migraions

* chore: delete records from old state

* Update x/ecocredit/migrations/v3/core.go

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>

* review changes

* Update types/module/server/manager.go

* review changes

* chore: cleanup

* review changes

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
  • Loading branch information
aleem1314 and ryanchristo authored Apr 13, 2022
1 parent 52061e8 commit 0a35fad
Show file tree
Hide file tree
Showing 14 changed files with 3,226 additions and 159 deletions.
13 changes: 13 additions & 0 deletions api/regen/ecocredit/v1/state.cosmos_orm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

195 changes: 98 additions & 97 deletions api/regen/ecocredit/v1/state.pulsar.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions proto/regen/ecocredit/v1/state.proto
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ message ProjectInfo {
primary_key : {fields : "id", auto_increment : true}
index : {id : 1, fields : "name", unique : true}
index : {id : 2, fields : "class_id,name", unique : true}
index : {id : 3, fields : "admin"}
};

// id is the unique ID of the project.
Expand Down
36 changes: 36 additions & 0 deletions types/module/server/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"sort"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -32,6 +33,7 @@ type Manager struct {
weightedOperationsHandlers []WeightedOperationsHandler
beginBlockers []BeginBlockerModule
endBlockers []EndBlockerModule
migrationHandlers map[string]MigrationHandler
}

// RegisterInvariants registers all module routes and module querier routes
Expand Down Expand Up @@ -59,6 +61,7 @@ func NewManager(baseApp *baseapp.BaseApp, cdc *codec.ProtoCodec) *Manager {
},
requiredServices: map[reflect.Type]bool{},
weightedOperationsHandlers: []WeightedOperationsHandler{},
migrationHandlers: map[string]MigrationHandler{},
}
}

Expand Down Expand Up @@ -136,6 +139,10 @@ func (mm *Manager) RegisterModules(modules []module.Module) error {
mm.weightedOperationsHandlers = append(mm.weightedOperationsHandlers, cfg.weightedOperationHandler)
}

if cfg.migrationHandler != nil {
mm.migrationHandlers[name] = cfg.migrationHandler
}

for typ := range cfg.requiredServices {
mm.requiredServices[typ] = true
}
Expand Down Expand Up @@ -199,6 +206,29 @@ func (mm *Manager) InitGenesis(ctx sdk.Context, genesisData map[string]json.RawM
return res
}

// RunMigrations performs state migrations for registered modules.
func (mm *Manager) RunMigrations(ctx sdk.Context, cdc codec.Codec) error {
// sorting migration handlers map to prevent non-determinism
keys := make([]string, 0, len(mm.migrationHandlers))
for k := range mm.migrationHandlers {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
h := mm.migrationHandlers[k]
if h == nil {
continue
}

if err := h(ctx, cdc); err != nil {
return err
}
}

return nil
}

func initGenesis(ctx sdk.Context, cdc codec.Codec,
genesisData map[string]json.RawMessage, validatorUpdates []abci.ValidatorUpdate,
initGenesisHandlers map[string]module.InitGenesisHandler) (abci.ResponseInitChain, error) {
Expand Down Expand Up @@ -279,6 +309,7 @@ func exportGenesis(ctx sdk.Context, cdc codec.Codec, exportGenesisHandlers map[s
}

type RegisterInvariantsHandler func(ir sdk.InvariantRegistry)
type MigrationHandler func(ctx sdk.Context, cdc codec.Codec) error

type configurator struct {
sdkmodule.Configurator
Expand All @@ -291,10 +322,15 @@ type configurator struct {
exportGenesisHandler module.ExportGenesisHandler
weightedOperationHandler WeightedOperationsHandler
registerInvariantsHandler RegisterInvariantsHandler
migrationHandler MigrationHandler
}

var _ Configurator = &configurator{}

func (c *configurator) RegisterMigrationHandler(mHandler MigrationHandler) {
c.migrationHandler = mHandler
}

func (c *configurator) RegisterWeightedOperationsHandler(operationsHandler WeightedOperationsHandler) {
c.weightedOperationHandler = operationsHandler
}
Expand Down
1 change: 1 addition & 0 deletions types/module/server/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ type Configurator interface {
RegisterInvariantsHandler(registry RegisterInvariantsHandler)
RegisterGenesisHandlers(module.InitGenesisHandler, module.ExportGenesisHandler)
RegisterWeightedOperationsHandler(WeightedOperationsHandler)
RegisterMigrationHandler(MigrationHandler)
}
122 changes: 61 additions & 61 deletions x/ecocredit/core/state.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0a35fad

Please sign in to comment.