Skip to content

When migrating content entries, migration version returned is erroneous if one of the migration entry was created manually #52

@yvesgurcan

Description

@yvesgurcan

How to reproduce the bug

  • Init migrations on new environments called environment1 and environment2 using content storage as a method to track migrations. You can use a source environment to duplicate from for both of the environments if you want.
  • Create a migration and apply it to environment1.
  • Manually create a migration content entry in environment2 using the Contentful UI.
  • Run npx migrations content --verbose --diff --source-environment-id environment1 --dest-environment-id environment2
  • You should see an error like this one: Different migration states detected. environment1 (1682093148544) !== environment2 (2)

Explanation of the issue

While the ID of the migration in environment1 is accurate (1682093148544), the ID of the migration in environment2 isn't (2) and is the outcome of this line of code:

return (items || []).map((item) => parseInt(item.sys.id, 10));

parseInt(item.sys.id, 10) converts the ID of the migration to a number in order to compare them across environments. However, if the migration entry was created manually, the ID is not a number but a string generated automatically by Contentful, like this one: 2txYqBZB1wAHVjtHmvzbcA. It seems like parseInt in this particular case only keeps the 2 and this is what the CLI interprets as the ID of the manually-created migration when comparing migrations between the environments.

Suggestions on how to solve the issue

  1. Add logic to avoid using parseInt on IDs that are not numbers. Consequently, Math.max should not occur when comparing IDs that are not both numbers since it would result in NaN:
    return Math.max(...versions);

    There is probably more to this since a string ID is not a timestamp and won't really compare with number IDs. The upside of this approach is that the ID of the migration could still surface in the error displayed from the migration content command (for example: Different migration states detected. environment1 (1682093148544) !== environment2 (2txYqBZB1wAHVjtHmvzbcA)), which would hint at the fact that there are IDs that were manually created. The downside is that manually-created migrations might be considered as legitimate as a result of this change, which might not be a good idea.
  2. Warn the user of the CLI that a manually-created entry was found and stop the content-migration process.
  3. It might be possible to use a blend of both approaches, where the ID of the migration is displayed even if the entry was created manually but a warning is also shown to the user to tell them that they shouldn't do that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions