Skip to content

Commit e53356c

Browse files
committed
Add support for query parameters in DB connection URL
- Make the schema be called “driver” in config
1 parent 3b43ee8 commit e53356c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

example_config.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ debug = True
66
secret = a super secretive string
77

88
[database]
9-
engine = postgresql
9+
driver = postgresql
1010
username = grice
1111
password = grice
1212
host = localhost

grice/db_service.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from collections import namedtuple
2+
import urllib
23

3-
from sqlalchemy.sql import Select
4-
5-
from grice.errors import ConfigurationError, NotFoundError, JoinError
64
from sqlalchemy import create_engine, MetaData, Column, Table, select, not_, or_, asc, desc, and_
75
from sqlalchemy import engine
6+
from sqlalchemy.sql import Select
87
from sqlalchemy.engine import reflection
98

9+
from grice.errors import ConfigurationError, NotFoundError, JoinError
10+
11+
1012
DEFAULT_PAGE = 0
1113
DEFAULT_PER_PAGE = 50
1214
LIST_FILTERS = ['in', 'not_in', 'bt', 'nbt']
@@ -24,6 +26,7 @@ def init_database(db_config):
2426
:param db_config:
2527
:return: SqlAlchemy engine object.
2628
"""
29+
driver = db_config.get('driver', 'postgresql')
2730
try:
2831
db_args = {
2932
'username': db_config['username'],
@@ -32,11 +35,13 @@ def init_database(db_config):
3235
'port': db_config['port'],
3336
'database': db_config['database']
3437
}
38+
if 'query' in db_config:
39+
db_args['query'] = dict(urllib.parse.parse_qsl(db_config['query'], keep_blank_values=True))
3540
except KeyError:
3641
msg = '"username", "password", "host", "port", and "database" are required fields of database config'
3742
raise ConfigurationError(msg)
3843

39-
eng_url = engine.url.URL(db_config.get('engine', 'postgresql'), **db_args)
44+
eng_url = engine.url.URL(driver, **db_args)
4045

4146
return create_engine(eng_url)
4247

0 commit comments

Comments
 (0)