def migrate_active_change_recommendations(apps, schema_editor):
    """
    Delete all change recommendation of motion versions, that are not active. For active
    change recommendations the motion id will be set.
    """
    MotionChangeRecommendation = apps.get_model('motions',
        'MotionChangeRecommendation')
    to_delete = []
    for cr in MotionChangeRecommendation.objects.all():
        if cr.motion_version.id == cr.motion_version.motion.active_version.id:
            cr.motion = cr.motion_version.motion
            cr.save(skip_autoupdate=True)
        else:
            to_delete.append(cr)
    for cr in to_delete:
        cr.delete(skip_autoupdate=True)
