-
Notifications
You must be signed in to change notification settings - Fork 41
/
migration_did_simple.go
54 lines (42 loc) · 1.8 KB
/
migration_did_simple.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
package migrations
import (
didtypes "github.com/cheqd/cheqd-node/x/did/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func MigrateDidSimple(sctx sdk.Context, mctx MigrationContext, apply func(didDocWithMetadata *didtypes.DidDocWithMetadata)) error {
sctx.Logger().Debug("MigrateDidSimple: Starting migration")
store := sctx.KVStore(mctx.didStoreKey)
sctx.Logger().Debug("MigrateDidSimple: Resetting counter")
// Reset counter
mctx.didKeeperNew.SetDidDocCount(&sctx, 0)
// Collect all DIDDoc versions
var allDidDocVersions []didtypes.DidDocWithMetadata
sctx.Logger().Debug("MigrateDidSimple: Iterating through all DIDDocs")
mctx.didKeeperNew.IterateAllDidDocVersions(&sctx, func(metadata didtypes.DidDocWithMetadata) bool {
allDidDocVersions = append(allDidDocVersions, metadata)
return true
})
// Iterate and migrate did docs. We can use single loop for removing old values, migration
// and writing new values because there is only one version of each diddoc in the store
for _, version := range allDidDocVersions {
// Needed for preventing using variables with the same address
version := version
sctx.Logger().Debug("MigrateDidSimple: Starting migration for DIDDoc: " + version.DidDoc.Id)
// Remove last version pointer
latestVersionKey := didtypes.GetLatestDidDocVersionKey(version.DidDoc.Id)
store.Delete(latestVersionKey)
// Remove version
versionKey := didtypes.GetDidDocVersionKey(version.DidDoc.Id, version.Metadata.VersionId)
store.Delete(versionKey)
// Migrate
apply(&version)
// Create as a new did doc
err := mctx.didKeeperNew.AddNewDidDocVersion(&sctx, &version)
if err != nil {
return err
}
sctx.Logger().Debug("MigrateDidSimple: Migration finished for DIDDoc: " + version.DidDoc.Id)
}
sctx.Logger().Debug("MigrateDidSimple: Migration finished")
return nil
}