def add_recursive_folder_sizes(c):
    """Aggregate folder size from file sizes and set folder value."""
    sql = """
        WITH RECURSIVE descendants AS
            ( 
                SELECT parent_id, child_id, 1 as level
                FROM folder_x_folder
            UNION ALL
                SELECT d.parent_id, s.child_id, d.level + 1
                FROM descendants AS d
                JOIN folder_x_folder s
                ON d.child_id = s.parent_id
            )
        SELECT SUM(folder_size) as size, parent_id
        FROM descendants
        INNER JOIN folders ON descendants.child_id = folders.folder_id
        GROUP BY parent_id
    """
    c.execute(sql)
    sizes = c.fetchall()
    sql = """
        UPDATE folders
        SET folder_size_r = ?
        WHERE folder_id = ?
    """
    c.executemany(sql, sizes)
    """Add the children size to the non-recusrive size to include files in
    the parent folder."""
    sql = """
        UPDATE folders
        SET folder_size_r = folder_size_r + folder_size
    """
    c.execute(sql)
