def change_motions_comments(apps, schema_editor):
    """
    Index the comments fields in the config. Changing from an array to a dict with the
    ids as keys. CHange all motions from an array for comments to a dict with the comments
    field id as key to link motion comments and comments fields.
    """
    ConfigStore = apps.get_model('core', 'ConfigStore')
    Motion = apps.get_model('motions', 'Motion')
    try:
        config_comments_fields = ConfigStore.objects.get(key='motions_comments'
            ).value
    except ConfigStore.DoesNotExist:
        config_comments_fields = []
    comments_fields = {}
    for index, field in enumerate(config_comments_fields):
        comments_fields[index + 1] = field
    max_index = len(config_comments_fields) - 1
    try:
        db_value = ConfigStore.objects.get(key='motions_comments')
    except ConfigStore.DoesNotExist:
        db_value = ConfigStore(key='motions_comments')
    db_value.value = comments_fields
    db_value.save()
    for motion in Motion.objects.all():
        comments = {}
        for index, comment in enumerate(motion.comments or []):
            if index > max_index:
                break
            comments[index + 1] = comment
        motion.comments = comments
        motion.save(skip_autoupdate=True)
