/
migration_resource_version_links.go
67 lines (58 loc) · 2.22 KB
/
migration_resource_version_links.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
package migrations
import (
"fmt"
"sort"
"github.com/cheqd/cheqd-node/app/migrations/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func MigrateResourceVersionLinks(sctx sdk.Context, mctx MigrationContext) error {
sctx.Logger().Debug("MigrateResourceVersionLinks: Starting migration")
store := sctx.KVStore(mctx.resourceStoreKey)
sctx.Logger().Debug("MigrateResourceVersionLinks: Reading all resources")
// Read all resources. Yes, this is memory intensive, but it's the simplest way to do it.
// Resource size is limited to 200KB, so this should be fine.
resources, err := mctx.resourceKeeperNew.GetAllResources(&sctx)
if err != nil {
return err
}
sctx.Logger().Debug("MigrateResourceVersionLinks: Reading all keys and Clean store")
// Clean store
keys := helpers.ReadAllKeys(store, nil)
for _, key := range keys {
store.Delete(key)
}
// Reset version links
for _, resource := range resources {
sctx.Logger().Debug(fmt.Sprintf(
"MigrateResourceVersionLinks: Id: %s CollectionId: %s OldPreviousVersionId: %s OldNextVersionId: %s",
resource.Metadata.Id,
resource.Metadata.CollectionId,
resource.Metadata.PreviousVersionId,
resource.Metadata.NextVersionId))
resource.Metadata.PreviousVersionId = ""
resource.Metadata.NextVersionId = ""
}
sctx.Logger().Debug("MigrateResourceVersionLinks: Sorting resources by date created")
// Sort resources by date created
sort.Slice(resources, func(i, j int) bool {
iCreated := resources[i].Metadata.Created
jCreated := resources[j].Metadata.Created
return iCreated.Before(jCreated)
})
sctx.Logger().Debug("MigrateResourceVersionLinks: Setting version links")
// Add resources to store in the same order as they were created. This will create proper links.
for _, resource := range resources {
err = mctx.resourceKeeperNew.AddNewResourceVersion(&sctx, resource)
sctx.Logger().Debug(fmt.Sprintf(
"MigrateResourceVersionLinks: Id: %s CollectionId: %s NewPreviousVersionId: %s NewNextVersionId: %s",
resource.Metadata.Id,
resource.Metadata.CollectionId,
resource.Metadata.PreviousVersionId,
resource.Metadata.NextVersionId))
if err != nil {
return err
}
}
sctx.Logger().Debug("MigrateResourceVersionLinks: Migration finished")
return nil
}