-
Notifications
You must be signed in to change notification settings - Fork 6
/
migrate.js
60 lines (49 loc) · 1.48 KB
/
migrate.js
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
55
56
57
58
59
60
import bluebird from "bluebird";
import { fs } from "mz";
import path from "path";
import Redis from "redis";
import dbPromise from "./server/utils/db";
bluebird.promisifyAll(Redis.RedisClient.prototype);
bluebird.promisifyAll(Redis.Multi.prototype);
const redis = Redis.createClient();
await redis
.connect()(async () => {
let db = await dbPromise;
let userEmailLists = await redis.keysAsync("mosaico:*");
for (let userEmailList of userEmailLists) {
let [, user] = userEmailList.match(/mosaico:(\w+):emails/);
for (let i = await redis.llenAsync(userEmailList); i > 0; i--) {
let uuid = await redis.lindexAsync(userEmailList, i - 1);
let data, html;
try {
data = JSON.parse(
await fs.readFile(path.join("./emails", `${uuid}.json`)),
);
html = await fs.readFile(path.join("./emails", `${uuid}.html`));
} catch (e) {
continue;
}
try {
await db.run(
"INSERT INTO emails(uuid, user, metadata, content, html) VALUES(?, ?, ?, ?, ?)",
[
uuid,
user,
JSON.stringify(data.metadata),
JSON.stringify(data.content),
html,
],
);
} catch (e) {
if (e.code === "SQLITE_CONSTRAINT") {
continue;
}
throw e;
}
console.log(uuid);
}
}
console.log("OK");
return;
})()
.catch(console.log);