Updating Cross-space references using the Contentful CLI
There are two ways users can review which spaces and environments are using cross-space links and which links should be updated:
- Exporting contents of every space using
contentful-export
andcontentful-import
. - Creating custom migration scripts.
1. Export contents of a space
When exporting the contents of a space, follow these steps:
- Export contents of a single space using the following command:
contentful space export
. The resulting.json
file contains all the information about the space. The important properties are thecontentTypes
andentries
.
{
"contentTypes": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "space-a-id"
}
},
"id": "restaurant",
"type": "ContentType"
},
"displayField": "name",
"name": "Restaurant",
"description": "",
"fields": [
{
"id": "name",
"name": "Name",
"type": "Symbol"
},
{
"id": "menu",
"name": "Menu",
"type": "Array",
"items": {
"type": "ResourceLink",
"validations": []
},
"allowedResources": [
{
"type": "Contentful:Entry",
"source": "crn:contentful:::content:spaces/space-b-id/environments/master",
"contentTypes": ["dish"]
}
]
},
{
"id": "about",
"name": "About",
"type": "RichText",
"validations": [
{
"enabledMarks": [],
"message": "Marks are not allowed"
},
{
"enabledNodeTypes": ["embedded-resource-block"],
"message": "Only block entry from a different space nodes are allowed"
},
{
"nodes": {
"embedded-resource-block": {
"validations": [],
"allowedResources": [
{
"type": "Contentful:Entry",
"source": "crn:contentful:::content:spaces/space-b-id/environments/master",
"contentTypes": ["address"]
}
]
}
}
}
]
}
]
}
],
"entries": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "space-a-id"
}
},
"id": "5gDrXf0Nf223qHTWNnxNwn",
"type": "Entry",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "restaurant"
}
}
},
"fields": {
"name": {
"en-US": "My favorite pizza place"
},
"menu": {
"en-US": [
{
"sys": {
"type": "ResourceLink",
"linkType": "Contentful:Entry",
"urn": "crn:contentful:::content:spaces/space-b-id/environments/master/entries/dish-1-id"
}
},
{
"sys": {
"type": "ResourceLink",
"linkType": "Contentful:Entry",
"urn": "crn:contentful:::content:spaces/space-b-id/environments/master/entries/dish-2-id"
}
}
]
},
"about": {
"en-US": {
"content": [
{
"content": [
{
"value": "This is my favorite pizza restaurant!",
"nodeType": "text"
}
],
"nodeType": "paragraph"
},
{
"data": {
"target": {
"sys": {
"urn": "crn:contentful:::content:spaces/space-b-id/environments/master/entries/4vxXfphlVtXmbyvMsnL4U9",
"type": "ResourceLink",
"linkType": "Contentful:Entry"
}
}
},
"nodeType": "embedded-resource-block"
}
],
"nodeType": "document"
}
}
}
}
]
}
Analyze the members of the
contentTypes
array and their.fields
property. Identify all fields that contain arrayallowedResources
. Be aware it can be both fields of typeResourceLink
, as well asRichText
.For each element of collected
allowedResources
, inspect its source property containing the definition of allowed cross-space links. Investigate which of these are relevant to your use case and update their evironments segment accordingly.
If the link does not contain the environment segment, it means it is implicitly pointing to a master environment. You can still add the explicit segment following this format: crn:contentful:::content:spaces/<space-id>/environments/<environment-id>
.
Analyze the members of entries array and their
.fields
property. Identify all fields that containurn
property and are of content type that you have adjusted in previous steps. Update theurn
accordingly, by adding theenvironments
segment.Once you are done with editing, you can import changed json file and apply changes to the space:
contentful space import –content-file name-of-json-file.json
.Repeat all these steps for all linked spaces.
IMPORTANT: This approach recreates the modified entries (possibly losing versioning / history?). For more information, see Importing to a space with existing content.
2. Create a custom migration script
The Merge CLI allows users to run migrations, which can be a combination of scripts generated by CLI and modifications implemented by the users. By running the command contentful merge export --te <source-environment> --se <target-environment>
, users can generate a base migration script editing the content model. This base migration script can be further enhanced to apply necessary changes, as well to add transformations for entries. For more information about this, see:
In addition, you can use the Content Management API to update content models and update entries.