Repository: ploomber/jupysql (python)

Issue:
showing feedback when switching connections
we should print a message using the display.py module when switching connections:

```python
In [1]: %load_ext sql

In [2]: %sql duckdb:// --alias one

In [3]: %sql duckdb:// --alias two

In [4]: %sql --connections
Out[4]:
Active connections:
+---------+-----------+-------+
| current |    url    | alias |
+---------+-----------+-------+
|         | duckdb:// |  one  |
|    *    | duckdb:// |  two  |
+---------+-----------+-------+

In [5]: %sql one # NO MESSAGE DISPLAYED!

In [6]: %sql --connections
Out[6]:
Active connections:
+---------+-----------+-------+
| current |    url    | alias |
+---------+-----------+-------+
|    *    | duckdb:// |  one  |
|         | duckdb:// |  two  |
+---------+-----------+-------+
```

Pull request:
Showing feedback when switching connections
## Describe your changes
Show feedback when switching connections

## Issue number

Closes #727 

## Checklist before requesting a review

- [x] Performed a self-review of my code
- [x] Formatted my code with [`pkgmt format`](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#linting-formatting)
- [x] Added [docstring](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#documenting-changes-and-new-features) documentation and update the [changelog](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#changelog) (when needed)



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ef3bb2c..a860dcc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@
 * [Fix] Modified histogram query to ensure histogram binning is done correctly (#751)
 * [Fix] Fix bug that caused the `COMMIT` not to work when the SQLAlchemy driver did not support `set_isolation_level`
 * [Fix] Fixed vertical color breaks in histograms (#702)
+* [Fix] Showing feedback when switching connections (#727)
 * [Fix] Fix error that caused some connections not to be closed when calling `--close/-x`
 
 ## 0.8.0 (2023-07-18)
diff --git a/doc/tutorials/duckdb-native-sqlalchemy.md b/doc/tutorials/duckdb-native-sqlalchemy.md
index eaf4c21..dcafd9f 100644
--- a/doc/tutorials/duckdb-native-sqlalchemy.md
+++ b/doc/tutorials/duckdb-native-sqlalchemy.md
@@ -5,7 +5,7 @@ jupytext:
     extension: .md
     format_name: myst
     format_version: 0.13
-    jupytext_version: 1.14.7
+    jupytext_version: 1.14.6
 kernelspec:
   display_name: Python 3 (ipykernel)
   language: python
@@ -128,6 +128,7 @@ To suppress it, add this at the top of your notebook/script:
 ```{code-cell} ipython3
 from sql.warnings import JupySQLDataFramePerformanceWarning
 import warnings
+
 warnings.filterwarnings("ignore", category=JupySQLDataFramePerformanceWarning)
 ```
 
diff --git a/src/sql/connection/connection.py b/src/sql/connection/connection.py
index 25d4942..261f0a0 100644
--- a/src/sql/connection/connection.py
+++ b/src/sql/connection/connection.py
@@ -152,12 +152,12 @@ class ConnectionManager:
                 cls.current = DBAPIConnection(descriptor, alias=alias)
             else:
                 existing = rough_dict_get(cls.connections, descriptor)
-
                 if existing and existing.alias == alias:
                     cls.current = existing
                 # passing an existing descriptor and not alias: use existing connection
                 elif existing and alias is None:
                     cls.current = existing
+                    display.message(f"Switching to connection {descriptor}")
                 # passing the same URL but different alias: create a new connection
                 elif existing is None or existing.alias != alias:
                     cls.current = cls.from_connect_str(


Test patch:
diff --git a/src/tests/test_connection.py b/src/tests/test_connection.py
index 9d0dc63..e0b6197 100644
--- a/src/tests/test_connection.py
+++ b/src/tests/test_connection.py
@@ -555,3 +555,24 @@ def test_set_no_descriptor_database_url(monkeypatch):
 
     assert connections == {"sqlite://": conn}
     assert ConnectionManager.current == conn
+
+
+def test_feedback_when_switching_connection_with_alias(ip_empty, capsys):
+    ip_empty.run_cell("%load_ext sql")
+    ip_empty.run_cell("%sql duckdb:// --alias one")
+    ip_empty.run_cell("%sql duckdb:// --alias two")
+    ip_empty.run_cell("%sql one")
+
+    captured = capsys.readouterr()
+    assert "Switching to connection one" == captured.out.replace("\n", "")
+
+
+def test_feedback_when_switching_connection_without_alias(ip_empty, capsys):
+    ip_empty.run_cell("%load_ext sql")
+    ip_empty.run_cell("%sql duckdb://")
+    ip_empty.run_cell("%sql duckdb:// --alias one")
+    ip_empty.run_cell("%sql duckdb:// --alias two")
+    ip_empty.run_cell("%sql duckdb://")
+
+    captured = capsys.readouterr()
+    assert "Switching to connection duckdb://" == captured.out.replace("\n", "")