'Specialised version of _simple_upsert_txn that picks a push_rule_id
using the _push_rule_id_gen if it needs to insert the rule. It assumes
that the "push_rules" table is locked'

def _upsert_push_rule_txn(self, txn, stream_id, event_stream_ordering, user_id, rule_id, priority_class, priority, conditions_json, actions_json, update_stream=True):

    sql = 'UPDATE    push_rules    SET    priority_class    =    ?,    priority    =    ?,    conditions    =    ?,    actions    =    ?    WHERE    user_name    =    ?    AND    rule_id    =    ?'
    txn.execute(sql, (priority_class, priority, conditions_json, actions_json, user_id, rule_id))
    if (txn.rowcount == 0):
        push_rule_id = self._push_rule_id_gen.get_next()
        self._simple_insert_txn(txn, table='push_rules', values={'id': push_rule_id, 'user_name': user_id, 'rule_id': rule_id, 'priority_class': priority_class, 'priority': priority, 'conditions': conditions_json, 'actions': actions_json})
    if update_stream:
        self._insert_push_rules_update_txn(txn, stream_id, event_stream_ordering, user_id, rule_id, op='ADD', data={'priority_class': priority_class, 'priority': priority, 'conditions': conditions_json, 'actions': actions_json})
