aboutsummaryrefslogtreecommitdiffstats
path: root/lib/migrate.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/migrate.js')
-rw-r--r--lib/migrate.js98
1 files changed, 64 insertions, 34 deletions
diff --git a/lib/migrate.js b/lib/migrate.js
index 7ff13649..ef15c738 100644
--- a/lib/migrate.js
+++ b/lib/migrate.js
@@ -1,11 +1,15 @@
-import { err, warn, l } from "$lib/logging";
+import { err, warn, l, line } from "$lib/logging";
import { types } from "$lib/payments";
-import { archive, db, g, ga } from "$lib/db";
+import { archive, db, g, ga, s } from "$lib/db";
let q = {};
async function migrate(id) {
- id = id.replace(/\s/g, "").toLowerCase();
- let user = await ga(`user:${id}`);
+ if (id) id = id.replace(/\s/g, "").toLowerCase();
+ let user = await g(`user:${id}`);
+ if (typeof user === "string") user = await g(`user:${user}`);
+ if (user) return user;
+
+ user = await ga(`user:${id}`);
if (typeof user === "string") {
if (await g(`user:${user}`)) {
@@ -14,7 +18,6 @@ async function migrate(id) {
}
id = user;
-
user = await ga(`user:${id}`);
}
@@ -22,48 +25,75 @@ async function migrate(id) {
let { username } = user;
username = username.replace(/\s/g, "").toLowerCase();
l("migrating", username);
- let multi = await db
- .multi()
- .set(`user:${username}`, id)
- .set(`user:${user.pubkey}`, id)
- .set(`user:${id}`, JSON.stringify(user))
- .set(`${id}:contacts`, (await ga(`${id}:contacts`)) || "[]")
- .set(`credit:bitcoin:${id}`, (await ga(`credit:bitcoin:${id}`)) || 0)
- .set(`credit:lightning:${id}`, (await ga(`credit:lightning:${id}`)) || 0)
- .set(`${id}:lastlen`, (await ga(`${id}:lastlen`)) || 0)
- .set(`${id}:cindex`, (await ga(`${id}:cindex`)) || 0)
- .set(`balance:${id}`, (await ga(`balance:${id}`)) || 0)
- .set(`pending:${id}`, (await ga(`pending:${id}`)) || 0);
- let payments = await archive.lRange(`${id}:payments`, 0, -1);
- await Promise.all(
- payments.map(async (pid) => {
+ try {
+ let multi = await db
+ .multi()
+ .set(`user:${username}`, id)
+ .set(`user:${user.pubkey}`, id)
+ .set(`user:${id}`, JSON.stringify(user));
+
+ await multi.exec();
+
+ await archive.del(`user:${username}`);
+ await archive.del(`user:${user.pubkey}`);
+ await archive.del(`user:${id}`);
+
+ await s(`${id}:contacts`, (await ga(`${id}:contacts`)) || []);
+ await archive.del(`${id}:contacts`);
+
+ await s(`credit:bitcoin:${id}`, (await ga(`credit:bitcoin:${id}`)) || 0);
+ await archive.del(`credit:bitcoin:${id}`);
+
+ await s(
+ `credit:lightning:${id}`,
+ (await ga(`credit:lightning:${id}`)) || 0,
+ );
+ await archive.del(`credit:lightning:${id}`);
+
+ await s(`${id}:lastlen`, (await ga(`${id}:lastlen`)) || 0);
+ await archive.del(`${id}:lastlen`);
+ await s(`${id}:cindex`, (await ga(`${id}:cindex`)) || 0);
+ await archive.del(`${id}:cindex`);
+ await s(`balance:${id}`, (await ga(`balance:${id}`)) || 0);
+ await archive.del(`balance:${id}`);
+ await s(`pending:${id}`, (await ga(`pending:${id}`)) || 0);
+ await archive.del(`pending:${id}`);
+
+ let pid;
+ while ((pid = await archive.rPop(`${id}:payments`))) {
let p = await ga(`payment:${pid}`);
+ if (!p) continue;
if (p.type === types.internal && p.ref) {
let recipient = await g(`user:${p.ref}`);
if (!recipient) q[p.ref] = true;
}
- multi = await multi
+ await multi
.lPush(`${id}:payments`, pid)
- .set(`payment:${pid}`, JSON.stringify(p));
- }),
- );
+ .set(`payment:${pid}`, JSON.stringify(p))
+ .exec();
+
+ await archive.del(`payment:${pid}`);
+ }
+ await archive.del(`${id}:payments`);
- let invoices = await archive.lRange(`${id}:invoices`, 0, -1);
- await Promise.all(
- invoices.map(async (iid) => {
+ let iid;
+ while ((iid = await archive.rPop(`${id}:invoices`))) {
let inv = await ga(`invoice:${iid}`);
- multi = await multi
+ if (!inv) continue;
+ await multi
.lPush(`${id}:invoices`, iid)
- .set(`invoice:${iid}`, JSON.stringify(inv));
- }),
- );
+ .set(`invoice:${iid}`, JSON.stringify(inv))
+ .exec();
- try {
- await multi.exec();
+ await archive.del(`invoice:${iid}`);
+ }
+ await archive.del(`${id}:invoices`);
+
+ l("done migrating", username);
} catch (e) {
- warn("failed to migrate", username);
+ warn("failed to migrate", username, e.message, line());
}
delete q[id];