Contentful Migration Tooling

repository·main·Indexed 43 days ago

https://github.com/contentful/contentful-migration

A library and tool for programmatically describing and executing changes to Contentful content models and transforming entry content. It allows developers to manage content types, fields, tags, and entries, and provides capabilities for structural data migration via functions like transformEntriesToType.

Tokens
16.3K
Snippets
48
Records
66
Agent score
55%

What's inside Contentful Migration

  1. Understand Contentful Migration validation error scoping

    main

    Contentful Migration validates your script to ensure actions on content types and fields are logically sound. Error keys are scoped using a specific pattern based on the entity and the action being performed:

    {entity}.{action}.{validation_name}

    For example, an error occurring during a field creation might look like field.create.FIELD_ALREADY_CREATED.

    Some validation errors are scoped only to the entity itself (e.g., {entity}.{validation_name}) when they relate to the final payload generated after the migration script is evaluated.

  2. Understand the relationship between contentful-migration and contentful-cli

    main

    The contentful-migration package serves as the core migration engine and Domain Specific Language (DSL) library. While it contains a bin/contentful-migration entry point for backward compatibility and testing, it is no longer the recommended user-facing CLI tool.

    Instead, contentful-cli acts as the primary interface for users, consuming contentful-migration as a dependency to provide the space migration command. Developers building custom migration scripts should use contentful-migration as a library rather than relying on the standalone CLI binary.

  3. Understand migration domain concepts

    main

    To write effective migration scripts, understand these core abstractions:

    • Migration Script: A JS/TS file that exports a function receiving the migration object.
    • Intent: An internal representation of a single discrete operation (e.g., "create content type X").
    • Request Batch: A group of HTTP requests belonging to a single intent, executed sequentially.
    • Offline API: An in-memory simulation of the CMA state used to validate your migration plan locally before any real API calls are made.
    • Context: An object passed to migration scripts containing makeRequest, spaceId, and accessToken, allowing for "escape-hatch" direct CMA access if the DSL is insufficient.
  4. Versioning rules for Conventional Commits

    main

    The release process relies on specific commit message patterns to determine the next version bump:

    • Major bump: Triggered by breaking changes. Use the feat!: syntax or include a BREAKING CHANGE: footer in the commit message.
    • Patch bump: Triggered by dependency updates, such as using the build(deps) prefix.
    • Changelog generation: Automated via @semantic-release/changelog based on commit history.
  5. Automated release process via Semantic Release

    main

    The project uses semantic-release to automate the release lifecycle. Version numbers are derived from commit messages, changelogs are generated automatically, and packages are published to npm without manual intervention.

    To ensure correct versioning, developers must follow the Conventional Commits specification. The version field in package.json is kept at 0.0.0-development, as semantic-release manages the actual published version numbers.

  6. How contentful-migration works

    main

    The contentful-migration library is a TypeScript-based Domain Specific Language (DSL) used to script migrations for Contentful content models and entries.

    The Execution Lifecycle:

    1. Script Execution: Your migration script (a JS/TS file) is executed in a sandboxed context. It calls DSL methods (like createContentType or editField) on a migration object.
    2. Intent Building: The library intercepts these calls to build an ordered list of Intents (discrete operations like "add field Y").
    3. Validation & Simulation: Before touching the real API, the library uses an Offline API to simulate the migration against an in-memory model of your Contentful space. This validates the entire plan against the existing state fetched from the Contentful Management API (CMA).
    4. Execution: Once validated and confirmed by the user, the library converts intents into Actions (CMA API requests) and executes them in throttled, batched requests.
  7. Use Chaining vs Object notation for migrations

    main

    The migration API supports two styles of method calls:

    1. Chained approach (Recommended): Allows for better error reporting. Validation errors will display the specific line number where the error occurred.
    2. Object approach: Uses a single configuration object. Validation errors will only point to the line where the object is defined, making debugging harder.

    Example of Chaining:

    const author = migration
      .createContentType('author')
      .name('Author')
      .description('Author of blog posts or pages')

    Example of Object notation:

    const author = migration.createContentType('author', {
      name: 'Author',
      description: 'Author of blog posts or pages'
    })
  8. Delete fields correctly using omit or shorthand

    main

    To delete a field, you must first mark it as omitted. Using .deleted(true) on a field that hasn't been omitted first will trigger a field.NO_DELETE_WITHOUT_OMIT error.

    Recommendation: Use the deleteField shorthand method to perform both operations (omitting and deleting) in a single step.

    // Error: field.NO_DELETE_WITHOUT_OMIT
    const author = migration.editContentType('author')
    author.editField('fullName').deleted(true)
    
    // Correct way (manual):
    author.editField('fullName').omitted(true).deleted(true)
    
    // Correct way (shorthand):
    // migration.deleteField('author', 'fullName')
  9. Upgrade requirements for contentful-migration v5.0.0

    main

    When upgrading to contentful-migration v5.0.0, you must ensure that your environment meets the following dependency requirements:

    • contentful-management SDK: You must use version v12 or higher.
    • Library Consumers: If you are using contentful-migration as a library in your own code, you must manually update your contentful-management peer dependency to v12+ to avoid compatibility issues.
    • CLI Users: If you use contentful-cli, upgrading to contentful-cli v4.0.0 will automatically provide contentful-migration v5.0.0.