def rename_variable(formula, old_name, new_name):
    """
    Function that traverses the formula and changes the appearance of one
    variable. The renaming is done to the values of the id and field
     attributes.
    :param formula: Root node of the formula object
    :param old_name: Old variable name
    :param new_name: New variable name
    :return: The new modified formula.
    """

    # Trivial case of an empty formula
    if not formula:
        return formula

    if 'condition' in formula:
        # Recursive call
        formula['rules'] = [rename_variable(x, old_name, new_name)
                            for x in formula['rules']]
        return formula

    # Loop over the changes and apply them to this node
    if formula['id'] != old_name:
        # No need to rename this formula
        return formula

    formula['id'] = new_name
    formula['field'] = new_name

    return formula