'The old cleanup script does not mark HistoryDatasetAssociations as purged when deleted Histories are purged.  This method can be used to rectify that situation.'

def update_hda_purged_flag(self):

    log.info('Marking    purged    all    HistoryDatasetAssociations    associated    with    purged    Datasets')
    event_id = self._create_event()
    sql = '\n                                                                WITH    purged_hda_ids\n                                                                        AS    (                    UPDATE    history_dataset_association\n                                                                                                                        SET    purged    =    true\n                                                                                                                    FROM    dataset\n                                                                                                                WHERE    history_dataset_association.dataset_id    =    dataset.id\n                                                                                                                                        AND    dataset.purged\n                                                                                                                                        AND    NOT    history_dataset_association.purged\n                                                                                                RETURNING    history_dataset_association.id),\n                                                                                    hda_events\n                                                                        AS    (INSERT    INTO    cleanup_event_hda_association\n                                                                                                                                        (create_time,    cleanup_event_id,    hda_id)\n                                                                                                            SELECT    NOW(),    %s,    id\n                                                                                                                    FROM    purged_hda_ids)\n                                                        SELECT    id\n                                                                FROM    purged_hda_ids\n                                                ORDER    BY    id;\n                                '
    args = (event_id,)
    cur = self._update(sql, args)
    self._flush()
    self._open_logfile()
    for tup in cur:
        self._log(('Marked    HistoryDatasetAssociation    purged:    %s' % tup[0]))
    self._close_logfile()
