'Any operation that purges a HistoryDatasetAssociation may require
updating a user\'s disk usage.  Rather than attempt to resolve dataset
copies at purge-time, simply maintain a list of users that have had
HDAs purged, and update their usages once all updates are complete.
This could probably be done more efficiently.'

def _update_user_disk_usage(self):

    log.info('Recalculating    disk    usage    for    users    whose    HistoryDatasetAssociations    were    purged')
    for user_id in self.disk_accounting_user_ids:
        sql = '\n                                                                            UPDATE    galaxy_user\n                                                                                        SET    disk_usage    =    (SELECT    COALESCE(SUM(total_size),    0)\n                                                                                                                                                                        FROM    (        SELECT    d.total_size\n                                                                                                                                                                                                                FROM    history_dataset_association    hda\n                                                                                                                                                                                                                                    JOIN    history    h    ON    h.id    =    hda.history_id\n                                                                                                                                                                                                                                    JOIN    dataset    d    ON    hda.dataset_id    =    d.id\n                                                                                                                                                                                                            WHERE    h.user_id    =    %s\n                                                                                                                                                                                                                                    AND    h.purged    =    false\n                                                                                                                                                                                                                                    AND    hda.purged    =    false\n                                                                                                                                                                                                                                    AND    d.purged    =    false\n                                                                                                                                                                                                                                    AND    d.id    NOT    IN    (SELECT    dataset_id\n                                                                                                                                                                                                                                                                                                                FROM    library_dataset_dataset_association)\n                                                                                                                                                                                                GROUP    BY    d.id)    sizes)\n                                                                                WHERE    id    =    %s\n                                                                RETURNING    disk_usage;\n                                                '
        args = (user_id, user_id)
        cur = self._update(sql, args)
        self._flush()
        for tup in cur:
            log.debug(('Updated    disk    usage    for    user    id    %i    to    %s    bytes' % (user_id, tup[0])))
