Contentful logo

Contentful Community

Migrating references with the Contentful Migration DSL?

#1

I have a module that was created with fields as a single reference and would like to migrate to a new field as multiple references. I tried the following, the migration “runs”, but the fields are not really updated.

module.exports = function(migration) {
  migration.transformEntries({
    contentType: 'patchworkStory',
    from: ['responsiveImage'],
    to: ['responsiveImages'],
    transformEntryForLocale: function (fromFields, currentLocale) {
      if (currentLocale === 'en' || !fromFields.responsiveImage.en) {
        return;
      }

      return { responsiveImages: [fromFields.responsiveImage.en] };
    }
  });
};

Is there any way to do this with transformEntries or can I only do it with the sdk?

0 Likes

#2

The code you included only transforms entries of a content type. Have you added the responsiveImages field prior to transforming the entries?

If not, you’d want to add something like the following before you run the code you included:

const patchworkStory = migration.editContentType('patchworkStory')
const responsiveImages = patchworkStory.createField('responsiveImages', {
  name: 'Responsive Images',
  type: 'Array',
  items: {
    type: 'Link',
    linkType: 'Asset'
  }
})

This code skips the transformation for all en localizations. Is that what you want to do? Just making sure :smiley:

Also are you meaning to copy the en localized field values to all other localizations? That’s fine if you are, just from your description it sounds like you want to copy from the responsiveImage field to the responsiveImages field of all the same locale. If that is the case then I think your return statement should use currentLocale rather than en like so:

  return { responsiveImages: [fromFields.responsiveImage[currentLocale]]}

Hope this helps!

0 Likes