asyncpg icon indicating copy to clipboard operation
asyncpg copied to clipboard

using notify listener with all-caps channel names

Open ajaxerror opened this issue 3 years ago • 1 comments

Hello,

I have a project where we set all our notify listeners up using all-caps channel names. In applying your package I found this didn't work, since the add_listener function sends the channel to postgres as a quoted identifier. I monkey patched as such to get it to work for my project:

async def add_listener(self, channel, callback, quote=True):
    """Add a listener for Postgres notifications.

    :param str channel: Channel to listen on.

    :param callable callback:
        A callable receiving the following arguments:
        **connection**: a Connection the callback is registered with;
        **pid**: PID of the Postgres server that sent the notification;
        **channel**: name of the channel the notification was sent to;
        **payload**: the payload.

    :param quote:
        specifies the channel name should should be sent as quoted identifier
    """
    self._check_open()
    if not quote:
        channel = channel.lower()
    if channel not in self._listeners:
        if quote:
            await self.fetch('LISTEN {}'.format(utils._quote_ident(channel)))
        else:
            await self.fetch('LISTEN {}'.format(channel))
        self._listeners[channel] = set()
    self._listeners[channel].add(callback)

Would you consider a pull request with this change? I would think I am not the only one who uses the listeners this way.

Thanks!

ajaxerror avatar Jul 27 '21 21:07 ajaxerror

Would you consider a pull request with this change

This is a good fix! Please send a pull request.

elprans avatar Jul 27 '21 22:07 elprans