/
store.go
56 lines (46 loc) · 1.56 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package v5
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
// MigrateStore migrates the store from version 5 to version 6.
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
return moveDefaultUserGroupToRootSection(ctx, storeKey, cdc)
}
func moveDefaultUserGroupToRootSection(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
groupsStore := prefix.NewStore(store, types.GroupsPrefix)
iterator := groupsStore.Iterator(nil, nil)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var oldGroup types.UserGroup
cdc.MustUnmarshal(iterator.Value(), &oldGroup)
if oldGroup.ID != types.DefaultGroupID {
// Skip because the group is not default
continue
}
if oldGroup.SectionID == types.RootSectionID {
// Skip because the default group has not been moved
continue
}
// Delete moved old default group key
store.Delete(types.GroupStoreKey(oldGroup.SubspaceID, oldGroup.SectionID, oldGroup.ID))
// Save new default group key
newGroup := types.NewUserGroup(
oldGroup.SubspaceID,
types.RootSectionID,
types.DefaultGroupID,
oldGroup.Name,
oldGroup.Description,
oldGroup.Permissions,
)
store.Set(
types.GroupStoreKey(newGroup.SubspaceID, newGroup.SectionID, newGroup.ID),
cdc.MustMarshal(&newGroup),
)
}
return nil
}