-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.js
325 lines (277 loc) · 11 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const path = require("path");
const execa = require("execa");
const fs = require("fs-extra");
const { chunk, pick, get } = require("lodash");
const mime = require("mime-types");
const { GraphQLClient } = require("graphql-request");
const loadJson = require("load-json-file");
const writeJson = require("write-json-file");
const {
getFilesFromElement,
getFilesFromElementPreview,
getFilesFromPageSettings,
getFilesFromPageBuilderSettings,
injectFilesIntoElement,
injectFilesIntoElementPreview,
injectFilesIntoPageSettings,
injectFilesIntoPageBuilderSettings
} = require("./utils");
const { CREATE_FILES, UPLOAD_FILES } = require("./graphql");
const uploadToS3 = require("./uploadToS3");
const { FILES_LOCATION, FILES_PER_CHUNK, AUTH_TOKEN, GRAPHQL_API_URL } = process.env;
module.exports = async (dbInstance, { host, dbName }) => {
const out = path.join(__dirname, "out");
await fs.ensureDir(out);
const env = { LC_ALL: "C" };
const opts = { stdio: "inherit", env };
const mongoDump = collection => [
"mongoexport",
[
"--uri",
`${host}/${dbName}`,
"--out",
path.join(out, collection + ".json"),
"--jsonArray",
"--collection",
collection
].filter(Boolean),
opts
];
const cmsCollections = ["CmsCategory", "CmsElement", "CmsMenu", "CmsPage"];
const renameMap = {
CmsCategory: "PbCategory",
CmsElement: "PbPageElement",
CmsMenu: "PbMenu",
CmsPage: "PbPage"
};
// Step 1. Convert all Cms collections to PageBuilder structure
// This process operates on the collection files directly, then restores the files into the DB.
async function convertData() {
try {
for (let i = 0; i < cmsCollections.length; i++) {
const cmsCollection = cmsCollections[i];
await execa(...mongoDump(cmsCollection), { stdio: "inherit" });
}
// Replace data
for (let i = 0; i < cmsCollections.length; i++) {
const cmsCollection = cmsCollections[i];
// Replace `cms-` prefix
await execa(
"sed",
["-i.bak", '-es/"cms-/"pb-/g', `./out/${cmsCollection}.json`],
opts
);
// Replace `pb-element-` prefix
await execa(
"sed",
["-i.bak", '-es/"pb-element-/"/g', `./out/${cmsCollection}.json`],
opts
);
await execa(
"sed",
["-i.bak", '-es/"pages-list-component-/"/g', `./out/${cmsCollection}.json`],
opts
);
// Replace `--webiny-cms` prefix for CSS vars
await execa(
"sed",
["-i.bak", "-es/--webiny-cms/--webiny-pb/g", `./out/${cmsCollection}.json`],
opts
);
if (cmsCollection === "CmsElement") {
// replace `pb-block-category-` prefix with an empty string
await execa(
"sed",
["-i.bak", '-es/"pb-block-category-/"/g', `./out/${cmsCollection}.json`],
opts
);
}
if (cmsCollection === "CmsMenu") {
// replace `pb-menu-item-` prefix with an empty string
await execa(
"sed",
["-i.bak", '-es/"pb-menu-item-/"/g', `./out/${cmsCollection}.json`],
opts
);
}
// Restore
if (!["CmsPage", "CmsElement"].includes(cmsCollection)) {
await execa(
"mongoimport",
[
"--uri",
`${host}/${dbName}`,
"--drop",
"--collection",
renameMap[cmsCollection],
"--jsonArray",
"--file",
path.join(out, `${cmsCollection}.json`)
].filter(Boolean),
opts
);
}
}
} catch (err) {
console.log(err);
}
}
await convertData();
// Step 2. Migrate files
const files = {};
// Get images from cms settings and assign them to page-builder settings
console.log("👀 Collecting files from Settings...");
const cmsSettings = await dbInstance.collection("Settings").findOne({ key: "cms" });
const pbSettings = await dbInstance.collection("Settings").findOne({ key: "page-builder" });
Object.assign(pbSettings.data, pick(cmsSettings.data, ["favicon", "logo", "social"]));
getFilesFromPageBuilderSettings(pbSettings.data, files);
// Get all images from Elements
console.log("👀 Collecting files from Elements...");
const elementsJson = await loadJson(path.join(out, "CmsElement.json"));
collectFromElements(elementsJson, files);
// Get all images from Page content and settings
console.log("👀 Collecting files from Pages...");
const pagesJson = await loadJson(path.join(out, "CmsPage.json"));
collectFromPages(pagesJson, files);
console.log(`Found ${Object.keys(files).length} files!`);
Object.keys(files).forEach(key => {
// const filePath = path.resolve(FILES_LOCATION, "test-image.jpg");
const filePath = path.resolve(FILES_LOCATION, files[key].key);
files[key] = {
...files[key],
path: filePath,
name: files[key].key,
size: fs.statSync(filePath).size,
type: mime.lookup(key)
};
});
// Upload files
const client = new GraphQLClient(GRAPHQL_API_URL, {
headers: { Authorization: AUTH_TOKEN }
});
// Gives an array of chunks (each consists of FILES_COUNT_IN_EACH_BATCH items).
const filesChunks = chunk(Object.keys(files), FILES_PER_CHUNK);
await console.log(
`Upload files: there are total of ${
filesChunks.length
} chunks of ${FILES_PER_CHUNK} files to save.`
);
for (let i = 0; i < filesChunks.length; i++) {
await console.log(`Upload files: started with chunk index ${i}`);
let filesChunk = filesChunks[i];
// 1. Get pre-signed POST payloads for current files chunk.
const response = await client.request(UPLOAD_FILES, {
data: filesChunk.map(key => pick(files[key], ["name", "size", "type"]))
});
const preSignedPostPayloads = get(response, "files.uploadFiles.data") || [];
await console.log(
`Upload files: received pre-signed POST payloads for ${
preSignedPostPayloads.length
} files.`
);
// 2. Use received pre-signed POST payloads to upload files directly to S3.
const s3UploadProcess = [];
for (let j = 0; j < filesChunk.length; j++) {
const currentFile = filesChunk[j];
// const buffer = fs.readFileSync(path.resolve(FILES_LOCATION, "test-image.jpg"));
const buffer = fs.readFileSync(files[currentFile].path);
s3UploadProcess.push(uploadToS3(buffer, preSignedPostPayloads[j].data));
}
await Promise.all(s3UploadProcess);
// 3. Now that all of the files were successfully uploaded, we create files entries in the database.
await console.log("Upload files: saving File entries into the database...");
const filesToCreate = filesChunk.map((item, i) => preSignedPostPayloads[i].file);
const res = await client.request(CREATE_FILES, { data: filesToCreate });
const createdFiles = get(res, "files.createFiles.data");
for (let j = 0; j < filesChunk.length; j++) {
const key = filesChunk[j];
files[key].id = createdFiles[j].id;
files[key].src = createdFiles[j].src;
}
}
// Inject files into page-builder settings
injectFilesIntoPageBuilderSettings(pbSettings.data, files);
await dbInstance
.collection("Settings")
.updateOne({ key: "page-builder" }, { $set: { data: pbSettings.data } });
// Inject file IDs into elements
injectFilesIntoElements(elementsJson, files);
await writeJson(__dirname + "/out/CmsElement.json", elementsJson);
// Inject file IDs into pages
injectFilesIntoPages(pagesJson, files);
await writeJson(__dirname + "/out/CmsPage.json", pagesJson);
// Import the modified collections back to DB
const restore = ["CmsElement", "CmsPage"];
for (let i = 0; i < restore.length; i++) {
await execa(
"mongoimport",
[
"--uri",
`${host}/${dbName}`,
"--drop",
"--collection",
renameMap[restore[i]],
"--jsonArray",
"--file",
path.join(out, `${restore[i]}.json`)
].filter(Boolean),
opts
);
}
console.log(`\n✅ Migration process finished!`);
};
const isValidElement = element => {
return element && element.type;
};
// Recursively traverse element and its children
const traverse = (element, cb) => {
if (!isValidElement(element)) {
return;
}
cb(element);
if (Array.isArray(element.elements)) {
for (let i = 0; i < element.elements.length; i++) {
traverse(element.elements[i], cb);
}
}
};
// Traverse element content and preview, and collect references to files.
function collectFromElements(json, files) {
for (let i = 0; i < json.length; i++) {
let element = json[i];
traverse(element.content, contentElement => {
getFilesFromElement(contentElement, files);
});
getFilesFromElementPreview(element, files);
}
}
// Traverse page content and settings, and collect references to files.
function collectFromPages(json, files) {
for (let i = 0; i < json.length; i++) {
let page = json[i];
traverse(page.content, element => {
getFilesFromElement(element, files);
});
getFilesFromPageSettings(page, files);
}
}
// Traverse Elements and inject file IDs
function injectFilesIntoElements(json, files) {
for (let i = 0; i < json.length; i++) {
let element = json[i];
traverse(element.content, contentElement => {
injectFilesIntoElement(contentElement, files);
});
injectFilesIntoElementPreview(element, files);
}
}
// Traverse Pages and inject file IDs
function injectFilesIntoPages(json, files) {
for (let i = 0; i < json.length; i++) {
let page = json[i];
traverse(page.content, element => {
injectFilesIntoElement(element, files);
});
injectFilesIntoPageSettings(page, files);
}
}