-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.js
54 lines (51 loc) · 1.62 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
const MurmurHash3 = require("imurmurhash");
module.exports = async function (migration, context) {
const subjects = await context.makeRequest({
method: "GET",
url: `/entries?content_type=subject`,
});
migration.transformEntries({
contentType: "course",
from: ["primarySubject"],
to: ["learnPage"],
transformEntryForLocale: (
{ primarySubject: primarySubjectWithLocale },
currentLocale
) => {
const primarySubject = primarySubjectWithLocale?.[currentLocale];
if (!primarySubject) {
return;
}
const learnPage = subjectsMap.get(primarySubject.sys.id).fields.learnPage[
currentLocale
];
return {
learnPage,
};
},
});
// It doesn't update references in rich text fields.
migration.transformEntriesToType({
sourceContentType: "iframeSection",
targetContentType: "githubGistSection",
from: ["internalName", "embedCode"],
shouldPublish: true,
updateReferences: true,
removeOldEntries: true,
identityKey: function (fields) {
const value = fields?.internalName?.["en-US"] + new Date().toISOString();
console.log(`MurmurHash3(${value}) = ${MurmurHash3(value).result()}`);
return MurmurHash3(value).result().toString();
},
transformEntryForLocale: function (fromFields, currentLocale, { id }) {
if (!fromFields.embedCode[currentLocale]?.trim()?.startsWith("<script")) {
return undefined;
}
return {
originalId: id,
internalName: fromFields.internalName[currentLocale],
embedCode: fromFields.embedCode[currentLocale],
};
},
});
};