Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for unix socket for aiomysql and asyncmy #551

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions databases/backends/asyncmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def _get_connection_kwargs(self) -> dict:
max_size = url_options.get("max_size")
pool_recycle = url_options.get("pool_recycle")
ssl = url_options.get("ssl")
unix_socket = url_options.get("unix_socket")

if min_size is not None:
kwargs["minsize"] = int(min_size)
Expand All @@ -49,6 +50,8 @@ def _get_connection_kwargs(self) -> dict:
kwargs["pool_recycle"] = int(pool_recycle)
if ssl is not None:
kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()]
if unix_socket is not None:
kwargs["unix_socket"] = unix_socket

for key, value in self._options.items():
# Coerce 'min_size' and 'max_size' for consistency.
Expand Down
3 changes: 3 additions & 0 deletions databases/backends/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def _get_connection_kwargs(self) -> dict:
max_size = url_options.get("max_size")
pool_recycle = url_options.get("pool_recycle")
ssl = url_options.get("ssl")
unix_socket = url_options.get("unix_socket")

if min_size is not None:
kwargs["minsize"] = int(min_size)
Expand All @@ -49,6 +50,8 @@ def _get_connection_kwargs(self) -> dict:
kwargs["pool_recycle"] = int(pool_recycle)
if ssl is not None:
kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()]
if unix_socket is not None:
kwargs["unix_socket"] = unix_socket

for key, value in self._options.items():
# Coerce 'min_size' and 'max_size' for consistency.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_connection_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ def test_mysql_pool_size():
assert kwargs == {"minsize": 1, "maxsize": 20}


@pytest.mark.skipif(sys.version_info >= (3, 10), reason="requires python3.9 or lower")
def test_mysql_unix_socket():
backend = MySQLBackend(
"mysql+aiomysql://username:password@/testsuite?unix_socket=/tmp/mysqld/mysqld.sock"
)
kwargs = backend._get_connection_kwargs()
assert kwargs == {"unix_socket": "/tmp/mysqld/mysqld.sock"}


@pytest.mark.skipif(sys.version_info >= (3, 10), reason="requires python3.9 or lower")
def test_mysql_explicit_pool_size():
backend = MySQLBackend("mysql://localhost/database", min_size=1, max_size=20)
Expand Down Expand Up @@ -114,6 +123,15 @@ def test_asyncmy_pool_size():
assert kwargs == {"minsize": 1, "maxsize": 20}


@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
def test_asyncmy_unix_socket():
backend = AsyncMyBackend(
"mysql+asyncmy://username:password@/testsuite?unix_socket=/tmp/mysqld/mysqld.sock"
)
kwargs = backend._get_connection_kwargs()
assert kwargs == {"unix_socket": "/tmp/mysqld/mysqld.sock"}


@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
def test_asyncmy_explicit_pool_size():
backend = AsyncMyBackend("mysql://localhost/database", min_size=1, max_size=20)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_database_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def test_database_url_options():
u = DatabaseURL("postgresql://localhost/mydatabase?pool_size=20&ssl=true")
assert u.options == {"pool_size": "20", "ssl": "true"}

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


def test_replace_database_url_components():
u = DatabaseURL("postgresql://localhost/mydatabase")
Expand Down