@devaltitude/contentful-migrate

中文文档
1.0.0发布于 2025/08/29MIT近 30 天下载 2927

[AS] Migration tooling for Contentful, with state management

原始使用文档
文档目录9 节
  1. Installation
  2. Usage
  3. init
  4. bootstrap
  5. create
  6. list
  7. up
  8. down
  9. Writing Migrations

Contentful Migrate Tool

npm contentful-migration version Build Status Downloads

NOTICE: This is a fork from the original contentful-migrate package

Our goal is to update the package based on our needs

Manage your Contentful schema by creating incremental scripted changes. This project is based on the ideas exposed in Contentful's CMS as Code article

Scripts are written using Contentful's migration tool syntax. Ex:

module.exports.description = "Create Post model";

module.exports.up = migration => {
  const post = migration
    .createContentType("post")
    .name("Post")
    .displayField("title")
    .description("Post model");

  post
    .createField("title")
    .name("Title")
    .type("Symbol")
    .required(true)
    .localized(false);
};

module.exports.down = migration => {
  migration.deleteContentType("post");
};

This command line tool is designed to keep track of changes of content types individually. It keeps the scripts in a migrations folder in your project. This folder must contain one subfolder for each content type. Ex:

your-project
├── README.md
├── migrations
│   ├── banner
│   │   └── 1513743198536-create-banner.js
│   └── post
│       ├── 1513695986378-create-post.js
│       └── 1513716408272-add-date-field.js
├── package.json
.
.
.

If you don't wish to have your migrations directory in the root of your project, you can configure the name and location by setting the CONTENTFUL_MIGRATIONS_DIR environment variable, e.g. /path/to/project/foo/contentful-migrations

ATTENTION: The migrations directory should always be a folder under the project's root (or under some subdir inside the root). Placing the directory outside of the project's version control could lead to the migration code and Contentful schema becoming out-of-sync.

For more information on schema migrations technique and practice, see:

Installation

npm install -g contentful-migrate

Usage

Most of the available commands need a personal access token for accessing the CMA (Contentful Management API). You can pass the token using the --access-token option or setting an environment variable called CONTENTFUL_MANAGEMENT_ACCESS_TOKEN

Similarly, a default contentful space and environment id can be specified by setting the CONTENTFUL_SPACE_ID and CONTENTFUL_ENV_ID environment variables, which will be used as defaults for any command that accepts the --space-id and --environment-id options.

init

Creates the content type 'Migration' into the designated contentful space. This will be used to keep track of the current state of each managed content type.

  Usage: ctf-migrate init [options]

  Options:

    -t, --access-token [access-token]  CMA token, defaults to your environment variable CONTENTFUL_MANAGEMENT_ACCESS_TOKEN if empty
    -s, --space-id [space-id]          space id to use (defaults to environment variable CONTENTFUL_SPACE_ID)
    -e, --environment-id [env-id]      id of the environment within the space (defaults to environment variable CONTENTFUL_ENV_ID if set, otherwise defaults to 'master')

If the target space already has been init'd before, it will throw an error:

Content type with id "migration" already exists.

bootstrap

Create your migration files for content models already in your space. It gives you the option to squash any previous migration state. Note: It will delete any existing migration scripts and create a consolidated one for each specified content type.

  Usage: ctf-migrate bootstrap [options]

  Options:

    -t, --access-token [access-token]  CMA token, defaults to your environment variable CONTENTFUL_MANAGEMENT_ACCESS_TOKEN if empty
    -s, --space-id [space-id]          space id to use (defaults to environment variable CONTENTFUL_SPACE_ID)
    -e, --environment-id [env-id]      id of the environment within the space (defaults to environment variable CONTENTFUL_ENV_ID if set, otherwise defaults to 'master')
    -c, --content-type [content-type]  one or more content type to bootstrap with choice to overwrite migration state
    -a, --all                          apply bootstrap to all with choice to overwrite migration state

Example: executing the command ctf-migrate bootstrap -c post -s <space-id> will create a file where the up command will generate the exact snapshot of the Post content model

create

Creates an empty time stamped file in the content-type's migrations folder.

  Usage: ctf-migrate create <name> [options]

  Options:

    -c, --content-type <content-type>  content type name

Example: executing the command ctf-migrate create create-post-model -c post will create a file named ./migrations/post/1513695986378-create-post.js (the timestamp will vary)

list

Lists all migrations for the given content-types, also indicating whether they were already applied and when.

  Usage: ctf-migrate list [options]

  Options:

    -t, --access-token [access-token]  CMA token, defaults to your environment variable CONTENTFUL_MANAGEMENT_ACCESS_TOKEN if empty
    -s, --space-id [space-id]          space id to use (defaults to environment variable CONTENTFUL_SPACE_ID)
    -e, --environment-id [env-id]      id of the environment within the space (defaults to environment variable CONTENTFUL_ENV_ID if set, otherwise defaults to 'master')
    -c, --content-type [content-type]  one or more content type names to list
    -a, --all                          lists migrations for all content types

Example:

$ ctf-migrate list -s i2ztmmsocxul -c post banner
Listing post
  [2017-12-19 22:12:58] 1513695986378-create-post.js : Create Post model
  [pending] 1513716408272-add-title-field.js : Adds title field
Listing banner
  [2018-01-08 15:01:45] 20180103165614-create-banner.js : Create Banner model
  [2018-01-22 11:01:33] 20180111172942-add-subtitle-field.js: Add Subtitle field

For the post model in this example, the first script (create-post.js) has already been applied but the second one (add-title-field.js) has not. For the banner model, all scripts have been applied.

up

Migrates up to a specific version or all pending scripts if a filename is not informed. This will apply pending scripts for the specified content-type into the specified space.

  Usage: ctf-migrate up [filename] [options]

  Options:

    -t, --access-token [access-token]  CMA token, defaults to your environment variable CONTENTFUL_MANAGEMENT_ACCESS_TOKEN if empty
    -s, --space-id [space-id]          space id to use (defaults to environment variable CONTENTFUL_SPACE_ID)
    -e, --environment-id [env-id]      id of the environment within the space (defaults to environment variable CONTENTFUL_ENV_ID if set, otherwise defaults to 'master')
    -c, --content-type [content-type]  one or more content type names to process
    -a, --all                          processes migrations for all content types
    -d, --dry-run                      only shows the plan, don't write anything to contentful. defaults to false

down

ATTENTION: As noted in the CMS as Code article, "in real-world situations there is often no real way to down migrate content without resorting to backups". Even though we agree with that assertion, we still think there is value in having a down function to make it easier to develop and debug the up migration scripts (when you're working on a dev/test space), as it makes it easy to revert your changes and try again, without resorting to any manual intervention.*

Migrates down to a specific version or just the last one if filename is not informed. This will roll back applied scripts for the specified content-type from the specified space.

  Usage: ctf-migrate down [filename] [options]

  Options:

    -t, --access-token [access-token]  CMA token, defaults to your environment variable CONTENTFUL_MANAGEMENT_ACCESS_TOKEN if empty
    -s, --space-id [space-id]          space id to use (defaults to environment variable CONTENTFUL_SPACE_ID)
    -e, --environment-id [env-id]      id of the environment within the space (defaults to environment variable CONTENTFUL_ENV_ID if set, otherwise defaults to 'master')
    -c, --content-type [content-type]  content type name
    -d, --dry-run                      only shows the plan, don't write anything to contentful. defaults to false

Writing Migrations

For more information on how to write migrations, see Contentful migrations documentation

This tool is based on node-migrate. For more information on how to run migrations, see Running migrations

版本与发布记录

@devaltitude/contentful-migrate 共发布 1 个版本,首次发布于 2025/08/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2025/08/291.2 年前

关于 @devaltitude/contentful-migrate 的常见问题

@devaltitude/contentful-migrate 是什么?

[AS] Migration tooling for Contentful, with state management

如何安装 @devaltitude/contentful-migrate?

在项目目录运行 npm install @devaltitude/contentful-migrate,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 ctf-migrate,可以用 npx ctf-migrate 直接运行。

@devaltitude/contentful-migrate 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/devaltitude__contentful-migrate 是否存在。

@devaltitude/contentful-migrate 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@devaltitude/contentful-migrate 可以商用吗?

@devaltitude/contentful-migrate 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@devaltitude/contentful-migrate 的下载量有多少?

根据 npm 官方下载统计,@devaltitude/contentful-migrate 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,927 次,日均约 98 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@devaltitude/contentful-migrate 还在维护吗?

最新版本 1.0.0 发布于 2025/08/29(1.2 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/08/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。