def merge_enrollments(db, uuid, organization):
    with db.connect() as session:
        uidentity = find_unique_identity(session, uuid)
        if not uidentity:
            raise NotFoundError(entity=uuid)
        org = find_organization(session, organization)
        if not org:
            raise NotFoundError(entity=organization)
        disjoint = session.query(Enrollment).\
            filter(Enrollment.uidentity == uidentity,
                   Enrollment.organization == org).all()
        if not disjoint:
            entity = '-'.join((uuid, organization))
            raise NotFoundError(entity=entity)
        dates = [(enr.start, enr.end) for enr in disjoint]
        for st, en in utils.merge_date_ranges(dates):
            is_dup = lambda x, st, en: x.start == st and x.end == en
            filtered = [x for x in disjoint if not is_dup(x, st, en)]
            if len(filtered) != len(disjoint):
                disjoint = filtered
                continue
            try:
                enroll_db(session, uidentity, org,
                          from_date=st, to_date=en)
            except ValueError as e:
                raise InvalidValueError(e)
        for enr in disjoint:
            delete_enrollment_db(session, enr)