This tool migrates cross-space references in Contentful from one space/environment to another. It's designed to handle scenarios where content references need to be updated to point to different spaces or environments.
The migration process involves three sequential steps:
- Add new cross-space reference locations to content types
- Update existing entries to use the new reference locations
- Remove old cross-space reference locations from content types
- This script is not thoroughly tested - Only use for non-production purposes or in environments where you can safely recover from potential issues
- Test in a development environment first
- Have a rollback plan in case of issues
- Monitor your GraphQL API for reference resolution errors after migration
- Node.js (version 14 or higher)
- npm or yarn package manager
- Contentful Management API access token
- Access to the Contentful spaces you want to migrate
npm install
Copy the example environment file and configure it:
cp .env.example .env
Edit the .env
file and add your Contentful access token:
CONTENTFUL_ACCESS_TOKEN=your_contentful_management_api_token_here
- Go to your Contentful account settings
- Navigate to Account settings > CMA tokens
- Create a new personal access token or use an existing one
- Copy the token and add it to your
.env
file
Edit the configuration in src/xspace-refs-migration.ts
to match your migration requirements:
const config: Config = {
// The Contentful space where you want to run the migration
spaceId: "your_target_space_id",
// The environment within that space to migrate
environmentId: "dev", // or "master", "staging", etc.
// Your personal access token (loaded from environment variable)
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN!,
// Define which cross-space references should be migrated
crossSpaceReferenceTargetChanges: [
{
current: { spaceId: "old_space_id", environmentId: "master" },
new: { spaceId: "new_space_id", environmentId: "dev" },
},
],
// Whether to publish the entries after migration
shouldPublish: "preserve", // or true to auto-publish
// Whether to skip the confirmation prompt during migration
yes: false,
};
- spaceId: The Contentful space ID where you want to run the migration
- environmentId: The environment within that space (e.g., "master", "dev", "staging")
- crossSpaceReferenceTargetChanges: Array of reference changes to apply
- current: Where references currently point
- new: Where you want references to point after migration
- shouldPublish:
"preserve"
: Keep current publish status (default)true
: Automatically publish all migrated entries
- yes: Set to
true
to skip confirmation prompts
Scenario 1: Moving references from master to dev environment (same space)
{
current: { spaceId: "abc123", environmentId: "master" },
new: { spaceId: "abc123", environmentId: "dev" }
}
Scenario 2: Moving references from one space to another
{
current: { spaceId: "oldspace123", environmentId: "master" },
new: { spaceId: "newspace456", environmentId: "master" }
}
Scenario 3: Multiple migrations in one run
[
{
current: { spaceId: "space1", environmentId: "master" },
new: { spaceId: "space1", environmentId: "dev" },
},
{
current: { spaceId: "space2", environmentId: "master" },
new: { spaceId: "space2", environmentId: "dev" },
},
];
npm run migrate
This will:
- Load your configuration from the TypeScript file
- Connect to Contentful using your access token
- Execute the three-step migration process
- Provide progress updates and completion status
- Step 1: Scans content types for cross-space references and adds new allowed resource targets
- Step 2: Updates all entries to use the new reference locations while preserving existing data
- Step 3: Removes the old reference targets from content types
By default (shouldPublish: "preserve"
):
- Draft/changed entries will have references updated in the Management API
- Changes won't be visible in the Content Delivery API until manually published
- Published entries remain published after migration
If you set shouldPublish: true
:
- All processed entries will be automatically published
- Changes become immediately visible in the Content Delivery API
GraphQL Errors After Migration
- Error:
Cannot find the schema for space xyz and environment abc
- Cause: Entries with migrated references that weren't published may cause GraphQL resolution errors
- Solution: Publish the affected entries or set
shouldPublish: true
in your config
Access Token Issues
- Ensure your token has Management API access
- Verify the token has permissions for the target space
- Check that the token hasn't expired
TypeScript Compilation Errors
- Ensure you have compatible TypeScript and Node.js versions
- Run
npm install
to ensure all dependencies are installed
If you encounter issues:
- Check the console output for detailed error messages
- Verify your configuration matches your Contentful setup
- Test with a small subset of content first
- Ensure you have proper backups before running on production data
To modify the migration script:
- Edit
src/xspace-refs-migration.ts
- Update the configuration section with your requirements
- Test in a development environment first
- Run with
npm run migrate
ISC