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/
.vim/
.pylintrc
.coverage
.idea
......@@ -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)
......
......@@ -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(
......
......@@ -2,6 +2,7 @@
backend = mysql
host = localhost
socket = /var/run/mysqld/mysqld.sock
;port = 3306
name = AUR
user = aur
password = aur
......
......@@ -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
......
......@@ -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,
......
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