From 64bc93926f87aa9a0a29b4f014af2374e527fc8a Mon Sep 17 00:00:00 2001 From: Leonidas Spyropoulos Date: Tue, 18 May 2021 13:15:47 +0100 Subject: [PATCH] Add support for configuring database with port instead of socket Signed-off-by: Leonidas Spyropoulos --- .gitignore | 1 + aurweb/config.py | 4 ++++ aurweb/db.py | 11 ++++++++--- conf/config.defaults | 1 + conf/config.dev | 5 ++++- test/test_db.py | 24 +++++++++++++++++------- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 372fa105..35b571d7 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ fastapi_aw/ .vim/ .pylintrc .coverage +.idea diff --git a/aurweb/config.py b/aurweb/config.py index 49a2765a..2a6cfc3e 100644 --- a/aurweb/config.py +++ b/aurweb/config.py @@ -32,6 +32,10 @@ def rehash(): _get_parser() +def get_with_fallback(section, option, fallback): + return _get_parser().get(section, option, fallback=fallback) + + def get(section, option): return _get_parser().get(section, option) diff --git a/aurweb/db.py b/aurweb/db.py index f5530bcf..491ce9e2 100644 --- a/aurweb/db.py +++ b/aurweb/db.py @@ -27,15 +27,20 @@ def get_sqlalchemy_url(): aur_db_backend = aurweb.config.get('database', 'backend') if aur_db_backend == 'mysql': + if aurweb.config.get_with_fallback('database', 'port', fallback=None): + port = aurweb.config.get('database', 'port') + param_query = None + else: + port = None + param_query = {'unix_socket': aurweb.config.get('database', 'socket')} return constructor( 'mysql+mysqlconnector', username=aurweb.config.get('database', 'user'), password=aurweb.config.get('database', 'password'), host=aurweb.config.get('database', 'host'), database=aurweb.config.get('database', 'name'), - query={ - 'unix_socket': aurweb.config.get('database', 'socket'), - }, + port=port, + query=param_query ) elif aur_db_backend == 'sqlite': return constructor( diff --git a/conf/config.defaults b/conf/config.defaults index 98e033b7..c05648d5 100644 --- a/conf/config.defaults +++ b/conf/config.defaults @@ -2,6 +2,7 @@ backend = mysql host = localhost socket = /var/run/mysqld/mysqld.sock +;port = 3306 name = AUR user = aur password = aur diff --git a/conf/config.dev b/conf/config.dev index ccb01f4f..194a3bf8 100644 --- a/conf/config.dev +++ b/conf/config.dev @@ -9,11 +9,14 @@ backend = sqlite name = YOUR_AUR_ROOT/aurweb.sqlite3 -; Alternative MySQL configuration +; Alternative MySQL configuration (Use either port of socket, if both defined port takes priority) ;backend = mysql ;name = aurweb ;user = aur ;password = aur +;host = localhost +;port = 3306 +;socket = /var/run/mysqld/mysqld.sock [options] aurwebdir = YOUR_AUR_ROOT diff --git a/test/test_db.py b/test/test_db.py index f5902e4c..41936321 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -56,18 +56,28 @@ def test_sqlalchemy_mysql_url(): aurweb.config.rehash() -def make_temp_config(backend): +def test_sqlalchemy_mysql_port_url(): + tmpctx, tmp = make_temp_config("conf/config.defaults", ";port = 3306", "port = 3306") + + with tmpctx: + with mock.patch.dict(os.environ, {"AUR_CONFIG": tmp}): + aurweb.config.rehash() + assert db.get_sqlalchemy_url() + aurweb.config.rehash() + + +def make_temp_config(config_file, src_str, replace_with): tmpdir = tempfile.TemporaryDirectory() tmp = os.path.join(tmpdir.name, "config.tmp") - with open("conf/config") as f: - config = re.sub(r'backend = sqlite', f'backend = {backend}', f.read()) + with open(config_file) as f: + config = re.sub(src_str, f'{replace_with}', f.read()) with open(tmp, "w") as o: o.write(config) - return (tmpdir, tmp) + return tmpdir, tmp def test_sqlalchemy_unknown_backend(): - tmpctx, tmp = make_temp_config("blah") + tmpctx, tmp = make_temp_config("conf/config", "backend = sqlite", "backend = blah") with tmpctx: with mock.patch.dict(os.environ, {"AUR_CONFIG": tmp}): @@ -93,7 +103,7 @@ def test_connection_class_without_fail(): def test_connection_class_unsupported_backend(): - tmpctx, tmp = make_temp_config("blah") + tmpctx, tmp = make_temp_config("conf/config", "backend = sqlite", "backend = blah") with tmpctx: with mock.patch.dict(os.environ, {"AUR_CONFIG": tmp}): @@ -106,7 +116,7 @@ def test_connection_class_unsupported_backend(): @mock.patch("mysql.connector.connect", mock.MagicMock(return_value=True)) @mock.patch.object(mysql.connector, "paramstyle", "qmark") def test_connection_mysql(): - tmpctx, tmp = make_temp_config("mysql") + tmpctx, tmp = make_temp_config("conf/config", "backend = sqlite", "backend = mysql") with tmpctx: with mock.patch.dict(os.environ, { "AUR_CONFIG": tmp,