Commit 64bc9392 authored by Leonidas Spyropoulos's avatar Leonidas Spyropoulos Committed by Kevin Morris
Browse files

Add support for configuring database with port instead of socket



Signed-off-by: Leonidas Spyropoulos's avatarLeonidas Spyropoulos <artafinde@gmail.com>
parent ac31f520
...@@ -17,3 +17,4 @@ fastapi_aw/ ...@@ -17,3 +17,4 @@ fastapi_aw/
.vim/ .vim/
.pylintrc .pylintrc
.coverage .coverage
.idea
...@@ -32,6 +32,10 @@ def rehash(): ...@@ -32,6 +32,10 @@ def rehash():
_get_parser() _get_parser()
def get_with_fallback(section, option, fallback):
return _get_parser().get(section, option, fallback=fallback)
def get(section, option): def get(section, option):
return _get_parser().get(section, option) return _get_parser().get(section, option)
......
...@@ -27,15 +27,20 @@ def get_sqlalchemy_url(): ...@@ -27,15 +27,20 @@ def get_sqlalchemy_url():
aur_db_backend = aurweb.config.get('database', 'backend') aur_db_backend = aurweb.config.get('database', 'backend')
if aur_db_backend == 'mysql': 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( return constructor(
'mysql+mysqlconnector', 'mysql+mysqlconnector',
username=aurweb.config.get('database', 'user'), username=aurweb.config.get('database', 'user'),
password=aurweb.config.get('database', 'password'), password=aurweb.config.get('database', 'password'),
host=aurweb.config.get('database', 'host'), host=aurweb.config.get('database', 'host'),
database=aurweb.config.get('database', 'name'), database=aurweb.config.get('database', 'name'),
query={ port=port,
'unix_socket': aurweb.config.get('database', 'socket'), query=param_query
},
) )
elif aur_db_backend == 'sqlite': elif aur_db_backend == 'sqlite':
return constructor( return constructor(
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
backend = mysql backend = mysql
host = localhost host = localhost
socket = /var/run/mysqld/mysqld.sock socket = /var/run/mysqld/mysqld.sock
;port = 3306
name = AUR name = AUR
user = aur user = aur
password = aur password = aur
......
...@@ -9,11 +9,14 @@ ...@@ -9,11 +9,14 @@
backend = sqlite backend = sqlite
name = YOUR_AUR_ROOT/aurweb.sqlite3 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 ;backend = mysql
;name = aurweb ;name = aurweb
;user = aur ;user = aur
;password = aur ;password = aur
;host = localhost
;port = 3306
;socket = /var/run/mysqld/mysqld.sock
[options] [options]
aurwebdir = YOUR_AUR_ROOT aurwebdir = YOUR_AUR_ROOT
......
...@@ -56,18 +56,28 @@ def test_sqlalchemy_mysql_url(): ...@@ -56,18 +56,28 @@ def test_sqlalchemy_mysql_url():
aurweb.config.rehash() 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() tmpdir = tempfile.TemporaryDirectory()
tmp = os.path.join(tmpdir.name, "config.tmp") tmp = os.path.join(tmpdir.name, "config.tmp")
with open("conf/config") as f: with open(config_file) as f:
config = re.sub(r'backend = sqlite', f'backend = {backend}', f.read()) config = re.sub(src_str, f'{replace_with}', f.read())
with open(tmp, "w") as o: with open(tmp, "w") as o:
o.write(config) o.write(config)
return (tmpdir, tmp) return tmpdir, tmp
def test_sqlalchemy_unknown_backend(): 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 tmpctx:
with mock.patch.dict(os.environ, {"AUR_CONFIG": tmp}): with mock.patch.dict(os.environ, {"AUR_CONFIG": tmp}):
...@@ -93,7 +103,7 @@ def test_connection_class_without_fail(): ...@@ -93,7 +103,7 @@ def test_connection_class_without_fail():
def test_connection_class_unsupported_backend(): 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 tmpctx:
with mock.patch.dict(os.environ, {"AUR_CONFIG": tmp}): with mock.patch.dict(os.environ, {"AUR_CONFIG": tmp}):
...@@ -106,7 +116,7 @@ def test_connection_class_unsupported_backend(): ...@@ -106,7 +116,7 @@ def test_connection_class_unsupported_backend():
@mock.patch("mysql.connector.connect", mock.MagicMock(return_value=True)) @mock.patch("mysql.connector.connect", mock.MagicMock(return_value=True))
@mock.patch.object(mysql.connector, "paramstyle", "qmark") @mock.patch.object(mysql.connector, "paramstyle", "qmark")
def test_connection_mysql(): def test_connection_mysql():
tmpctx, tmp = make_temp_config("mysql") tmpctx, tmp = make_temp_config("conf/config", "backend = sqlite", "backend = mysql")
with tmpctx: with tmpctx:
with mock.patch.dict(os.environ, { with mock.patch.dict(os.environ, {
"AUR_CONFIG": tmp, "AUR_CONFIG": tmp,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment