Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a connection URL from an SQLAlchemy Engine instance?

I begin a program by generating a URL object and passing it to create_engine. In a section of code far, far away I would like to find out what this engine is connected to, i.e. the connection URL.

Is there an easy way to do this? Using inspect I can only see how to get the driver type. I can understand if the password component of a connection string was no longer available, but I'm hoping everything else is still available.

Any ideas?

like image 395
Samuel Harmer Avatar asked Jul 28 '16 08:07

Samuel Harmer


People also ask

What is SQLAlchemy URL?

class sqlalchemy.engine. URL(*arg, **kw) Represent the components of a URL used to connect to a database. This object is suitable to be passed directly to a create_engine() call. The fields of the URL are parsed from a string by the make_url() function.


2 Answers

This works quite well for me:

    log.info("* Using DB: %s" % (engine.url))
like image 149
Evvo Avatar answered Sep 27 '22 16:09

Evvo


The Engine class has an attribute url. Although not documented, it is not 'underscore hidden' so I would assume safe to read.

like image 41
Samuel Harmer Avatar answered Sep 27 '22 17:09

Samuel Harmer