-
Notifications
You must be signed in to change notification settings - Fork 5
/
lib.mo
49 lines (41 loc) · 1.42 KB
/
lib.mo
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
import MigrationTypes "./types";
import v0_1_0 "./v000_001_000";
import D "mo:base/Debug";
module {
let upgrades = [
v0_1_0.upgrade,
// do not forget to add your new migration upgrade method here
];
let downgrades = [
v0_1_0.downgrade,
// do not forget to add your new migration downgrade method here
];
func getMigrationId(state: MigrationTypes.State): Nat {
return switch (state) {
case (#v0_0_0(_)) 0;
case (#v0_1_0(_)) 1;
// do not forget to add your new migration id here
// should be increased by 1 as it will be later used as an index to get upgrade/downgrade methods
};
};
public func migrate(
prevState: MigrationTypes.State,
nextState: MigrationTypes.State,
args: MigrationTypes.Args
): MigrationTypes.State {
D.print("in migrate" # debug_show(prevState));
var state = prevState;
var migrationId = getMigrationId(prevState);
D.print("getting migration id");
let nextMigrationId = getMigrationId(nextState);
D.print(debug_show(nextMigrationId));
while (migrationId != nextMigrationId) {
D.print("in nft while");
let migrate = if (nextMigrationId > migrationId) upgrades[migrationId] else downgrades[migrationId - 1];
D.print("upgrade should have run");
migrationId := if (nextMigrationId > migrationId) migrationId + 1 else migrationId - 1;
state := migrate(state, args);
};
return state;
};
};