Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 71ea4ad

Browse files
authored
Support for unix socket for aiomysql and asyncmy (#551)
1 parent ab5eb71 commit 71ea4ad

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

databases/backends/asyncmy.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def _get_connection_kwargs(self) -> dict:
4040
max_size = url_options.get("max_size")
4141
pool_recycle = url_options.get("pool_recycle")
4242
ssl = url_options.get("ssl")
43+
unix_socket = url_options.get("unix_socket")
4344

4445
if min_size is not None:
4546
kwargs["minsize"] = int(min_size)
@@ -49,6 +50,8 @@ def _get_connection_kwargs(self) -> dict:
4950
kwargs["pool_recycle"] = int(pool_recycle)
5051
if ssl is not None:
5152
kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()]
53+
if unix_socket is not None:
54+
kwargs["unix_socket"] = unix_socket
5255

5356
for key, value in self._options.items():
5457
# Coerce 'min_size' and 'max_size' for consistency.

databases/backends/mysql.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def _get_connection_kwargs(self) -> dict:
4040
max_size = url_options.get("max_size")
4141
pool_recycle = url_options.get("pool_recycle")
4242
ssl = url_options.get("ssl")
43+
unix_socket = url_options.get("unix_socket")
4344

4445
if min_size is not None:
4546
kwargs["minsize"] = int(min_size)
@@ -49,6 +50,8 @@ def _get_connection_kwargs(self) -> dict:
4950
kwargs["pool_recycle"] = int(pool_recycle)
5051
if ssl is not None:
5152
kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()]
53+
if unix_socket is not None:
54+
kwargs["unix_socket"] = unix_socket
5255

5356
for key, value in self._options.items():
5457
# Coerce 'min_size' and 'max_size' for consistency.

tests/test_connection_options.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ def test_mysql_pool_size():
7777
assert kwargs == {"minsize": 1, "maxsize": 20}
7878

7979

80+
@pytest.mark.skipif(sys.version_info >= (3, 10), reason="requires python3.9 or lower")
81+
def test_mysql_unix_socket():
82+
backend = MySQLBackend(
83+
"mysql+aiomysql://username:password@/testsuite?unix_socket=/tmp/mysqld/mysqld.sock"
84+
)
85+
kwargs = backend._get_connection_kwargs()
86+
assert kwargs == {"unix_socket": "/tmp/mysqld/mysqld.sock"}
87+
88+
8089
@pytest.mark.skipif(sys.version_info >= (3, 10), reason="requires python3.9 or lower")
8190
def test_mysql_explicit_pool_size():
8291
backend = MySQLBackend("mysql://localhost/database", min_size=1, max_size=20)
@@ -114,6 +123,15 @@ def test_asyncmy_pool_size():
114123
assert kwargs == {"minsize": 1, "maxsize": 20}
115124

116125

126+
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
127+
def test_asyncmy_unix_socket():
128+
backend = AsyncMyBackend(
129+
"mysql+asyncmy://username:password@/testsuite?unix_socket=/tmp/mysqld/mysqld.sock"
130+
)
131+
kwargs = backend._get_connection_kwargs()
132+
assert kwargs == {"unix_socket": "/tmp/mysqld/mysqld.sock"}
133+
134+
117135
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
118136
def test_asyncmy_explicit_pool_size():
119137
backend = AsyncMyBackend("mysql://localhost/database", min_size=1, max_size=20)

tests/test_database_url.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def test_database_url_options():
6969
u = DatabaseURL("postgresql://localhost/mydatabase?pool_size=20&ssl=true")
7070
assert u.options == {"pool_size": "20", "ssl": "true"}
7171

72+
u = DatabaseURL(
73+
"mysql+asyncmy://username:password@/testsuite?unix_socket=/tmp/mysqld/mysqld.sock"
74+
)
75+
assert u.options == {"unix_socket": "/tmp/mysqld/mysqld.sock"}
76+
7277

7378
def test_replace_database_url_components():
7479
u = DatabaseURL("postgresql://localhost/mydatabase")

0 commit comments

Comments
 (0)