catherinedevlin / ipython-sql

%%sql magic for IPython, hopefully evolving into full SQL client
MIT License
1.79k stars 368 forks source link

Using DSN connections not working #208

Closed mx-tc closed 2 years ago

mx-tc commented 2 years ago

It seems using a DSN connection is broken. It keeps showing the following message, although database-connection is configured by DSN-file properly

%load_ext sql
%config SqlMagic.dsn_filename='/run/secrets/databases.ini'
%sql --section DB-PROD

Environment variable $DATABASE_URL not set, and no connect string given.
Connection info needed in SQLAlchemy format, example:
               postgresql://username:password@hostname/dbname
               or an existing connection: dict_keys([])

I was tracking it down to magic.py:205 where a connection-string, that was received from the DSN-file and therefor stored in the variable connect_str won't get used.

So the easy fix is to just apply following patch

--- magic-orig.py   2022-02-04 16:09:27.134124577 +0100
+++ magic.py    2022-02-04 16:10:27.122126305 +0100
@@ -202,7 +202,7 @@

         try:
             conn = sql.connection.Connection.set(
-                parsed["connection"],
+                connect_str,
                 displaycon=self.displaycon,
                 connect_args=args.connection_arguments,
                 creator=args.creator,
hozn commented 2 years ago

This appears that it [would be] fixed by #193 .

astrojuanlu commented 2 years ago

I think gh-193 was released as part of ipython-sql 0.4.1, @mx-tc @hozn could you try it? And if it works this issue can be closed

mx-tc commented 2 years ago

I can confirm that in ipython-sql==0.4.1 this bug is fixed and using DSN-connection is working now. Tested it with same instruction as provided with this ticket to reproduce the bug.