diff options
| -rw-r--r-- | lib/migrate.js | 157 |
1 files changed, 80 insertions, 77 deletions
diff --git a/lib/migrate.js b/lib/migrate.js index 38520ee9..f262903a 100644 --- a/lib/migrate.js +++ b/lib/migrate.js @@ -7,107 +7,110 @@ let queued = {}; let migrating = {}; async function migrate(id) { try { - if (id) id = id.replace(/\s/g, "").toLowerCase(); + if (id) id = id.replace(/\s/g, "").toLowerCase(); - let user = await g(`user:${id}`); - if (typeof user === "string") user = await g(`user:${user}`); + let user = await g(`user:${id}`); + if (typeof user === "string") user = await g(`user:${user}`); if (user) return user; + user = await ga(`user:${id}`); - user = await ga(`user:${id}`); + if (typeof user === "string") { + if (await g(`user:${user}`)) { + warn("user already migrated", id); + s(`user:${id}`, user); + } - if (typeof user === "string") { - if (await g(`user:${user}`)) { - warn("user already migrated", id); - s(`user:${id}`, user); + id = user; + user = await ga(`user:${id}`); } - id = user; - user = await ga(`user:${id}`); - } + if (user) { + if (migrating[id]) return; + migrating[id] = true; - if (user) { - if (migrating[id]) return; - migrating[id] = true; + let { username } = user; + username = username.replace(/\s/g, "").toLowerCase(); + l("migrating", username); - let { username } = user; - username = username.replace(/\s/g, "").toLowerCase(); - l("migrating", username); + try { + let multi = await db + .multi() + .set(`user:${username}`, id) + .set(`user:${user.pubkey}`, id) + .set(`user:${id}`, JSON.stringify(user)); - 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 multi.exec(); + await archive.del(`user:${username}`); + await archive.del(`user:${user.pubkey}`); + await archive.del(`user:${id}`); - 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(`${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: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( - `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}`); - 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}`); + let pos = await db.lPos(`${id}:payments`, pid) + if (!p || pos !== "nil") continue; + if (p.type === types.internal && p.ref) { + let recipient = await g(`user:${p.ref}`); + if (!recipient) queued[p.ref] = true; + } - 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) queued[p.ref] = true; - } + await multi + .lPush(`${id}:payments`, pid) + .set(`payment:${pid}`, JSON.stringify(p)) + .exec(); - await multi - .lPush(`${id}:payments`, pid) - .set(`payment:${pid}`, JSON.stringify(p)) - .exec(); + await archive.del(`payment:${pid}`); + } + await archive.del(`${id}:payments`); - await archive.del(`payment:${pid}`); - } - await archive.del(`${id}:payments`); + let iid; + while ((iid = await archive.rPop(`${id}:invoices`))) { + let inv = await ga(`invoice:${iid}`); + if (!inv) continue; + await multi + .lPush(`${id}:invoices`, iid) + .set(`invoice:${iid}`, JSON.stringify(inv)) + .exec(); - let iid; - while ((iid = await archive.rPop(`${id}:invoices`))) { - let inv = await ga(`invoice:${iid}`); - if (!inv) continue; - await multi - .lPush(`${id}:invoices`, iid) - .set(`invoice:${iid}`, JSON.stringify(inv)) - .exec(); + await archive.del(`invoice:${iid}`); + } + await archive.del(`${id}:invoices`); - await archive.del(`invoice:${iid}`); + l("done migrating", username); + } catch (e) { + warn("failed to migrate", username, e.message, line()); } - await archive.del(`${id}:invoices`); - l("done migrating", username); - } catch (e) { - warn("failed to migrate", username, e.message, line()); + delete migrating[id]; + return user; } - - delete migrating[id]; - return user; - } - } catch(e) { + } catch (e) { delete migrating[id]; throw e; } |
