-
Notifications
You must be signed in to change notification settings - Fork 48
/
store.go
38 lines (31 loc) · 1.09 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
package v7
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/posts/types"
)
// MigrateStore migrates the x/posts module state from the consensus version 6 to version 7.
// This migration set new owner field to author address for all the posts.
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
prefixStore := prefix.NewStore(store, types.PostPrefix)
iterator := prefixStore.Iterator(nil, nil)
defer iterator.Close()
// Get all the posts
var posts []types.Post
for ; iterator.Valid(); iterator.Next() {
var post types.Post
cdc.MustUnmarshal(iterator.Value(), &post)
posts = append(posts, post)
}
// Set the owner to its author for posts
for _, post := range posts {
newPost := post
newPost.Owner = newPost.Author
// Save the post
store.Set(types.PostStoreKey(post.SubspaceID, post.ID), cdc.MustMarshal(&newPost))
}
return nil
}