-
Notifications
You must be signed in to change notification settings - Fork 41
/
migration_resource_protobuf.go
80 lines (68 loc) · 2.78 KB
/
migration_resource_protobuf.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package migrations
import (
"encoding/hex"
"strings"
"github.com/cheqd/cheqd-node/app/migrations/helpers"
didutils "github.com/cheqd/cheqd-node/x/did/utils"
resourcetypes "github.com/cheqd/cheqd-node/x/resource/types"
resourcetypesv1 "github.com/cheqd/cheqd-node/x/resource/types/v1"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func MigrateResourceProtobuf(sctx sdk.Context, mctx MigrationContext) error {
sctx.Logger().Debug("MigrateResourceProtobuf: Starting migration")
// Storage for old headers and data
store := sctx.KVStore(mctx.resourceStoreKey)
sctx.Logger().Debug("MigrateResourceProtobuf: Resetting counter")
// Reset counter
mctx.resourceKeeperNew.SetResourceCount(&sctx, 0)
sctx.Logger().Debug("MigrateResourceProtobuf: Reading all keys")
headerKeys := helpers.ReadAllKeys(store, didutils.StrBytes(resourcetypesv1.ResourceHeaderKey))
for _, headerKey := range headerKeys {
sctx.Logger().Debug("MigrateResourceProtobuf: Starting migration for resource with header key: " + string(headerKey))
dataKey := ResourceV1HeaderkeyToDataKey(headerKey)
var oldHeader resourcetypesv1.ResourceHeader
mctx.codec.MustUnmarshal(store.Get(headerKey), &oldHeader)
oldData := store.Get(dataKey)
sctx.Logger().Debug("MigrateResourceProtobuf: Collecting new resource metadata")
newMetadata := resourcetypes.Metadata{
CollectionId: oldHeader.CollectionId,
Id: oldHeader.Id,
Name: oldHeader.Name,
Version: "",
ResourceType: oldHeader.ResourceType,
AlsoKnownAs: []*resourcetypes.AlternativeUri{},
MediaType: oldHeader.MediaType,
Created: helpers.MustParseFromStringTimeToGoTime(oldHeader.Created),
Checksum: hex.EncodeToString(oldHeader.Checksum),
PreviousVersionId: oldHeader.PreviousVersionId,
NextVersionId: oldHeader.NextVersionId,
}
resourceWithMetadata := resourcetypes.ResourceWithMetadata{
Metadata: &newMetadata,
Resource: &resourcetypes.Resource{
Data: oldData,
},
}
sctx.Logger().Debug("MigrateResourceProtobuf: Remove old values")
// Remove old resource data and header
store.Delete(headerKey)
store.Delete(dataKey)
sctx.Logger().Debug("MigrateResourceProtobuf: Write new resource with metadata")
// Write new resource
err := mctx.resourceKeeperNew.SetResource(&sctx, &resourceWithMetadata)
if err != nil {
return err
}
sctx.Logger().Debug("MigrateResourceProtobuf: Migration finished for resource with header key: " + string(headerKey))
}
sctx.Logger().Debug("MigrateResourceProtobuf: Migration finished")
return nil
}
func ResourceV1HeaderkeyToDataKey(headerKey []byte) []byte {
return []byte(
strings.Replace(
string(headerKey),
string(resourcetypesv1.ResourceHeaderKey),
string(resourcetypesv1.ResourceDataKey),
1))
}