The right way to migrate your content using the Contentful Migration CLI

Published on September 18, 2017

Contentful Migration CLI

What is the best way to automate your release process? That’s a question that many developers face. Our answer is the Contentful Migration Command Line Interface (CLI).

With the Contentful Migration CLI, we offer a framework that makes migrations easy and that also makes it possible for developers to script changes to their content model in a fine-grained way. All in the name of making your development process with Contentful as easy as possible.

You can, for example, use this tool together with a continuous integration service, like , to run migration scripts on a predefined test space as part of a test suite. This is so that you can validate the deployment before it gets pushed into production.

Not using Contentful yet? Sign up for your free account and start building in minutes.

How the Contentful Migration CLI works

You can use the new Contentful migrations framework for every step of the process: like for creating a new content type, adding a field, or changing validations on a field.

Using JavaScript, you can utilize the Contentful Migration CLI to create migration scripts describing changes to a content model, and then run these scripts on a given Contentful space by using the command line.

Upon execution, the migration CLI does the following:

  1. Parses your script

  2. Validates your script for syntax or consistency issues

  3. Shows you an execution plan, that you then accept if everything looks ok

  4. Applies the migration to the space that you specified, once the input has been validated and approved

How to install the Contentful Migration CLI

To install the contentful migration CLI, you need to have Node.js installed, and run the following command:

npm install -g contentful-migration-cli

After the installation is complete, you’ll need to set up and export the following environment variables:

  • CONTENTFUL_MANAGEMENT_ACCESS_TOKEN –– The personal access token for accessing the CMA.

  • HTTP_PROXY or http_proxy (optional) –– The settings for the HTTP proxy in the shape of http://[user:password@]<host>[:port].

  • HTTPS_PROXY or https_proxy (optional) –– The settings for the HTTPS proxy in the shape of https://[user:password@]<host>[:port].

If you have previously installed the Contentful CLI then these will be automatically picked up from your ~/.contentfulrc.json configuration file.

Please note that the environment variables will take precedence over the ~/.contentfulrc.json configuration.

An example of content model migration

Consider the following content model:

You can download and import this as an example space by using contentful-import:

Imagine an application which uses this data model, and you decide you want to add an Author field — which is defined in a separate content type. The data model, once migrated, will look like so:

So let’s say we create a migration script called 01-add-author.js with the following content:

For more information about the available functions, see the contentful-migration reference documentation.

We then kick off the migration:

contentful-migration --space-id $YOUR_SPACE_ID 01-add-author.js

What we will then see is the following migration plan:

Contentful migrations – Blog post proposal - Plan

Since everything looks the way we want it to, we’ll go ahead with the migration:

Contentful migrations – Blog post proposal - Result

How to validate migrations

In order to prevent as many errors as possible, the migration executor performs some sanity checks on migration files. The sanity checks include:

  • Syntax: typos, invalid properties, wrong types

  • Consistency: create a content type or field that already exists, delete a field or edit a content type that doesn’t exist, etc.

All types of validation errors are listed in the reference documentation.

Whenever a validation issue is detected, the contentful-migration command displays details of the error, including the line number within the script where the problem originates from:

Validation failed

What’s next

The Contentful Migration CLI is still in its early stages. And we will expand the framework in the near future to also cover content transformation, and integrate it in our main CLI.

We’re always keen to get your feedback, so feel free to reach out if you have ideas, remarks or if you are experiencing issues. You can connect with us via our Slack community channel at contentful.com/slack or by writing to devrel@contentful.com.

And if you enjoy using Contentful, tell your friends so they can benefit too. Anyone can sign up for a free Contentful account and start building in minutes.

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

Related articles

An illustration of a shopping basket filled with abstract shapes representing how microservices architecture unites content and ecommerce
Guides

Create a composable commerce site with Contentful and Medusa

July 7, 2022

Contentful launches two more hands-on courses for developers.
Guides

Contentful launches two more hands-on courses for developers

August 2, 2022

TypeScript makes Next.js development more streamlined, less error prone and more enjoyable. We show how to integrate TypeScript into your Next.js projects.
Guides

How to use TypeScript in your Next.js project

June 24, 2024

Contentful Logo 2.5 Dark

Ready to start building?

Put everything you learned into action. Create and publish your content with Contentful — no credit card required.

Get started