databases icon indicating copy to clipboard operation
databases copied to clipboard

0.4.0: Failing to use HA postgresql connection string

Open sukratkashyap opened this issue 5 years ago • 1 comments

Before the release of 0.4.0: We were able to use high available postgres connection string. postgresql://user:password@host1,host2:port/dbname

After the release of 0.4.0:

We are not able to use the high available postgres connection string given above. After looking at the code and changes. We found that this particular PR: https://github.com/encode/databases/pull/210 is breaking the change.

Any help would be appreciated regarding this issue!

sukratkashyap avatar Nov 04 '20 14:11 sukratkashyap

Hey @sukratkashyap sorry for this regression. I just did not know it was possible, another example of https://xkcd.com/1172/ :laughing: Seriously though, I clearly see that the surrounding code assumes a single endpoint. The specified high available DSN used to parse to something without errors by pure luck.

The workaround that I can recommend is registering a new backend with overridden connect().

from databases import Database
from databases.backends.postgres import PostgresBackend

class HighAvailablePostgresBackend(PostgresBackend):
    async def connect(self) -> None:
        assert self._pool is None, "DatabaseBackend is already running"
        kwargs = self._get_connection_kwargs()
        self._pool = await asyncpg.create_pool(str(self._database_url), **kwargs)

Database.SUPPORTED_BACKENDS["hapostgresql"] = "my.package.module.HighAvailablePostgresBackend"

ha_db = Database("hapostgresql://user:password@host1,host2:port/dbname")

vmarkovtsev avatar Nov 04 '20 15:11 vmarkovtsev