-
Notifications
You must be signed in to change notification settings - Fork 8
/
migrate.ts
49 lines (46 loc) · 1.58 KB
/
migrate.ts
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 { getClient, getNewClient } from "./client_loader";
async function main() {
const oldClient = await getClient();
const newClient = await getNewClient();
const oldDB = oldClient.kvstore;
const newDB = newClient.kvstore;
const oldKeys = await oldClient.kvstore.list();
let failedCount = 0;
let alreadyMigratedCount = 0;
const extrinsics = [];
for (const oldKey of oldKeys) {
try {
const oldValue = await oldDB.get({ key: oldKey });
const newValue = newDB.client.kvStore.encrypt(oldValue);
extrinsics.push(newDB.client.client.api.tx.tfkvStore.set(oldKey, newValue));
} catch {
try {
await newDB.get({ key: oldKey });
alreadyMigratedCount++;
console.log(`${oldKey} key is migrated`);
} catch (error) {
failedCount++;
}
}
}
if (extrinsics.length > 0) {
try {
await newClient.utility.batchAll({ extrinsics });
} catch (e) {
throw Error(`All or part of the keys are not migrated due to: ${e}`);
}
}
console.log(
`Migrated keys: ${extrinsics.length}, already migrated keys: ${alreadyMigratedCount}, failed keys: ${failedCount}`,
);
if (failedCount > 0 && extrinsics.length === 0) {
throw Error("storeSecret is wrong. Please enter the right storeSecret.");
} else if (failedCount > 0 && extrinsics.length !== 0) {
throw Error(
"Part of the keys are migrated successfully, but still some keys are not migrated. Maybe they are encrypted with different password or not encrypted",
);
}
await newClient.disconnect();
await oldClient.disconnect();
}
main();