-
Notifications
You must be signed in to change notification settings - Fork 5
Description
How to reproduce the bug
- Init migrations on new environments called
environment1andenvironment2using 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
environment2using 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:
contentful-migrations/lib/backend.js
Line 297 in bfdca95
| 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
- Add logic to avoid using
parseInton IDs that are not numbers. Consequently,Math.maxshould not occur when comparing IDs that are not both numbers since it would result inNaN:
contentful-migrations/lib/backend.js
Line 347 in bfdca95
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. - Warn the user of the CLI that a manually-created entry was found and stop the content-migration process.
- 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.