Data/Field Migration

Migration/Conversion/Copying of one field to another

Since v5 there is some basic support for copying data from one field to another and optionally migrate that data to a new type.
Supported migration paths at the moment:

  • from image2 to imageX
  • from file2 to imageX
  • from text to textarea

Copy instead of Migrate
You can also copy data from one field to another field of the same type.

How to proceed?

We will take you step by step through the process to migrate the data of one field to another type.
In this example we want to migrate a field of type image2 to imageX

  1. Create an page with an OpenContent module and use the 'Migrator' template from the github repository
  2. Identify any of your existing templates that has a field in its schema.json that you want to change its type
{
  "type": "object",
  "properties": {
    "MyField": {
      "type": "string",
      "title": "My Field"
    }
  }
}
{
  "fields": {
    "MyField": {
      "type": "image2",
      "folder": "MyField"
    }
  }
}
  1. Create a new field of the desired type. That new field should be on the same level as the old field. Use the Builder to do this, or do it manually if no Builder is in use for this template.
{
  "type": "object",
  "properties": {
    "MyField": {
      "type": "string",
      "title": "My Field"
    },
    "MyField2": {
      "type": "string",
      "title": "My 2nd Field"
    }
  }
}
{
  "fields": {
    "MyField": {
      "type": "image2",
      "folder": "Images"
    },
    "MyField2": {
      "type": "imagex",2
      "uploadfolder": "",
      "fileExtensions": "gif|jpg|jpeg|tiff|png",
      "fileMaxSize": 2000000,
      "showOverwrite": true,
      "overwrite": false,
      "showCropper": false
    }
  }
}
  1. Manually open the options.json file and search the field of your initial type and add an additional key/value pair. Where key="migrateTo" and the value is the name of the field you want to migrate to.
    Careful, this key is case sensitive!!
{
  "fields": {
    "MyField": {
      "type": "image2",
      "folder": "Images",
      "migrateTo": "MyField2"   <<=============
    },
    "MyField2": {
      "type": "imagex",
      "uploadhidden": false,
      "uploadfolder": "",
      "fileExtensions": "gif|jpg|jpeg|tiff|png",
      "fileMaxSize": 2000000,
      "showOverwrite": true,
      "overwrite": false,
      "showCropper": false
    }
  }
}
  1. Next, open the migrator.cshtml that you downloaded in step 1. We will now be evaluating/modifying lines 17, 20 and 23. Read carefully the remarks that are written about these lines. Modify if needed and save the cshtml file.
@using Satrabel.OpenContent.Components.Migration;

@inherits Satrabel.OpenContent.Components.OpenContentWebPage

<div>
<h2>Migrator Example</h2>
<p>This template is an helper template to assist you with migration of one field to another field.<br/>
See <a href="https://opencontent.readme.io/v5.0/docs/datafield-migration" target="_blank">the Online Manual</a> for more info.
Edit the cshtml file of this template. </p>
</div>
  
@{
    if (IsPost && Request["sbutton"] == "Start Migration") 
    {
        // The name of the folder of the template that holds an options.json file with a field to migrate
        // Not the full path. Just the name of the folder. It will be searched in you portal\OpenContent\Templates folder.
        string templateFolder = "PicturesOnly"; 

        // Specify if your migration is allowed to overwrite any existing data of the target field or not. An additional failsave in some circumstances.
        bool overwriteTargetData = false;

        // Final fail safe: as long as dryRun==true, no modification what so ever will be made. 
        bool dryRun = false;

        // Now run the Migration.
        // But what is going to be migrated you wonder? See https://opencontent.readme.io/v5.0/docs/datafield-migration for more information.
        @FieldMigrator.Migrate(this, templateFolder, overwriteTargetData, dryRun);
    }
}

<input type="submit" value="Start Migration" class="submit" name="sbutton" />
<p>&nbsp;</p>
  1. Now browse, as administrator, to the page you created in step 1. Click on the "Start Migration" button. and wait for the Status Report to show up.