Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OriginalEXE/338ee951b416d028fe75c289c58ba0ea to your computer and use it in GitHub Desktop.
Save OriginalEXE/338ee951b416d028fe75c289c58ba0ea to your computer and use it in GitHub Desktop.
Contentful Compose: Renaming existing page content type to make way for Compose page content type
// Example of a migration script for renaming an existing "page" content type to "legacyPage"
// to make room for Contentful Compose "page" content type
// The following example is written in TypeScript
import { MigrationFunction } from "contentful-migration";
import murmurhash from "murmurhash";
const migrate: MigrationFunction = (migration) => {
// Create a duplicate of the clashing page content type
const pageCopy = migration
.createContentType("legacyPage")
.name("Legacy Page")
.displayField("internalName");
// Here you would define all of the fields that your legacy page already has
pageCopy.createField("internalName").name("Internal name").type("Symbol");
pageCopy
.createField("title")
.name("Page title")
.type("Symbol")
.localized(true);
// Change content type of existing entries
migration.transformEntriesToType({
sourceContentType: "page",
targetContentType: "legacyPage",
shouldPublish: "preserve",
updateReferences: true,
removeOldEntries: true,
identityKey: function (fromFields) {
return murmurhash
.v3(`pageCopy-${fromFields.title["en-US"].toLowerCase()}`)
.toString();
},
transformEntryForLocale: function (fromFields, currentLocale) {
// We want all fields transfered as is
return Object.keys(fromFields).reduce((prev, current) => {
return {
...prev,
[current]: fromFields[current][currentLocale],
};
}, {});
},
});
// The migration is now done, you can delete the page content type in the next migration
};
module.exports = migrate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment