question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for connection params in `Database` in addition to the database url

See original GitHub issue

There’s a small proposal/question about the details for contenting the DB. There might be a situation where the host might have a complex address, particularly GCP has something like: /<cloudsql>/<project>:<region>:<db_instance> Generally, it was solved being passed as a query param unix_socket=<host>/.s.PGSQL.5432 to the sqlalchemy engine. It doesn’t work with asyncpg as it tries to parse the db url and the : char violates the parsing logic (when such host is part of the url or passed as environment variable) or the host is empty str when it’s passed as unix_socket param.

But asyncpg can take connections params separately https://magicstack.github.io/asyncpg/current/api/index.html#connection. Params have higher precedence over the url parsing, so they can take place.

I haven’t come up with a great idea yet, but it might be done as

class PostgresBackend | DatabaseBackend:
    def __init__(self, database_url: typing.Union[DatabaseURL, str], params: typing.Dict) -> None:
        self._database_url = DatabaseURL(database_url)
        ...
        self._params = params

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

Would it be possible to consider?

Happy to create a PR with a reasonable solution.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
gvbgduhcommented, Mar 19, 2019

Yeah, I think I’ve come up with the solution that takes minimum amount of blood. DatabaseURL already does parsing and provides DatabaseURL.options kwargs that are passed further. The only problem now that the set of those options is just {'min_size', 'max_size', 'ssl'}, so other options are ignored.

So, for example, the url 'postgresql://USER:PWD@/DB?host=HOST:project:region:db&port=5432' should do it, where in postgres it can be just extended to recognise other options mysql might have additional backup here, like

self._pool = await aiomysql.create_pool(
    host= kwargs.pop("host", None) or self._database_url.hostname,
    ...
    autocommit=kwargs.pop("autocommit", None) or True,
    **kwargs,
)

and the mentioned url is still valid.

This gives more flexibility and control over it and extends the amount of supported urls and we still have simplicity of just the url instantiation/settings.

While I think it might be not a good idea to allow arbitrary keys in kwargs, it’s worth considering to add all possible options per back end.

UPDATE: and it’s still valid url for sqlalchemy engine (and hence for alembic).

Any thoughts/objections?

0reactions
keurciencommented, Dec 10, 2020

Indeed the local code mismatches with the one I’m seeing in the repo. This is what I get when I run the ll command in Pdb, and presumably the one that’s running on my machine and in my Docker containers, even if databases.__version__ ==0.4.1:

63         async def connect(self) -> None:
64             assert self._pool is None, "DatabaseBackend is already running"
65             kwargs = self._get_connection_kwargs()
66  ->         self._pool = await asyncpg.create_pool(
67                 host=self._database_url.hostname,
68                 port=self._database_url.port,
69                 user=self._database_url.username,
70                 password=self._database_url.password,
71                 database=self._database_url.database,
72                 **kwargs,
73             )

Should I open an issue with a minimum working app packaged in a Dockerfile so it excludes local machine problems?

N.B: I installed a version of the repo and it works fine! 👌

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining Connection Parameters - InterBase
The database URL and connection properties arguments to connect() or getConnection() must be defined before trying to create the connection.
Read more >
JDBC Driver Connection Parameter Reference
Parameters for the Default Database, Role, Schema, and Warehouse ... The JDBC Driver does not support underscores in URLs, which include the account...
Read more >
34.1. Database Connection Control Functions - PostgreSQL
This function opens a new database connection using the parameters taken from two NULL -terminated arrays. The first, keywords , is defined as...
Read more >
Setting the connection properties - JDBC Driver for SQL Server
(Version 6.0+) Use this property to connect to a database using an access token. accessToken can't be set using the connection URL.
Read more >
Connection string for MySQL with multiple ... - Stack Overflow
In reference to this question, I'm trying to frame the connection string to connect to MySQL database with the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found