'Delete old entries out of the device_lists_outbound_pokes to ensure
that we don\'t fill up due to dead servers. We keep one entry per
(destination, user_id) tuple to ensure that the prev_ids remain correct
if the server does come back.'

def _prune_old_outbound_device_pokes(self):

    yesterday = (self._clock.time_msec() - (((24 * 60) * 60) * 1000))
    def _prune_txn(txn):
        select_sql = '\n                                                                SELECT    destination,    user_id,    max(stream_id)    as    stream_id\n                                                                FROM    device_lists_outbound_pokes\n                                                                GROUP    BY    destination,    user_id\n                                                                HAVING    min(ts)    <    ?    AND    count(*)    >    1\n                                                '
        txn.execute(select_sql, (yesterday,))
        rows = txn.fetchall()
        if (not rows):
            return
        delete_sql = '\n                                                                DELETE    FROM    device_lists_outbound_pokes\n                                                                WHERE    ts    <    ?    AND    destination    =    ?    AND    user_id    =    ?    AND    stream_id    <    ?\n                                                '
        txn.executemany(delete_sql, ((yesterday, row[0], row[1], row[2]) for row in rows))
        sql = '\n                                                                DELETE    FROM    device_lists_outbound_last_success\n                                                                WHERE    destination    =    ?    AND    user_id    =    ?\n                                                '
        txn.executemany(sql, ((row[0], row[1]) for row in rows))
        logger.info('Pruned    %d    device    list    outbound    pokes', txn.rowcount)
    return self.runInteraction('_prune_old_outbound_device_pokes', _prune_txn)
