Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Leonidas Spyropoulos
aurweb
Commits
64bc9392
Commit
64bc9392
authored
May 18, 2021
by
Leonidas Spyropoulos
Committed by
Kevin Morris
Jun 05, 2021
Browse files
Add support for configuring database with port instead of socket
Signed-off-by:
Leonidas Spyropoulos
<
artafinde@gmail.com
>
parent
ac31f520
Changes
6
Hide whitespace changes
Inline
Side-by-side
.gitignore
View file @
64bc9392
...
...
@@ -17,3 +17,4 @@ fastapi_aw/
.vim/
.pylintrc
.coverage
.idea
aurweb/config.py
View file @
64bc9392
...
...
@@ -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
)
...
...
aurweb/db.py
View file @
64bc9392
...
...
@@ -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
(
...
...
conf/config.defaults
View file @
64bc9392
...
...
@@ -2,6 +2,7 @@
backend = mysql
host = localhost
socket = /var/run/mysqld/mysqld.sock
;port = 3306
name = AUR
user = aur
password = aur
...
...
conf/config.dev
View file @
64bc9392
...
...
@@ -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
...
...
test/test_db.py
View file @
64bc9392
...
...
@@ -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
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment