mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-11 13:24:07 +00:00
add function to get a DSN for psycopg
Converts the PHP DSN syntax into psycopg syntax when necessary.
This commit is contained in:
@@ -29,6 +29,18 @@ class Configuration:
|
|||||||
|
|
||||||
return os.environ.get(name) or self._config[name]
|
return os.environ.get(name) or self._config[name]
|
||||||
|
|
||||||
|
def get_libpq_dsn(self):
|
||||||
|
""" Get configured database DSN converted into the key/value format
|
||||||
|
understood by libpq and psycopg.
|
||||||
|
"""
|
||||||
|
dsn = self.DATABASE_DSN
|
||||||
|
|
||||||
|
if dsn.startswith('pgsql:'):
|
||||||
|
# Old PHP DSN format. Convert before returning.
|
||||||
|
return dsn[6:].replace(';', ' ')
|
||||||
|
|
||||||
|
return dsn
|
||||||
|
|
||||||
def get_os_env(self):
|
def get_os_env(self):
|
||||||
""" Return a copy of the OS environment with the Nominatim configuration
|
""" Return a copy of the OS environment with the Nominatim configuration
|
||||||
merged in.
|
merged in.
|
||||||
|
|||||||
@@ -54,3 +54,22 @@ def test_get_os_env_prefer_os_environ():
|
|||||||
assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
|
assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
|
||||||
|
|
||||||
del os.environ['NOMINATIM_DATABASE_WEBUSER']
|
del os.environ['NOMINATIM_DATABASE_WEBUSER']
|
||||||
|
|
||||||
|
def test_get_libpq_dsn_convert_default():
|
||||||
|
config = Configuration(None, DEFCFG_DIR)
|
||||||
|
|
||||||
|
assert config.get_libpq_dsn() == 'dbname=nominatim'
|
||||||
|
|
||||||
|
def test_get_libpq_dsn_convert_php():
|
||||||
|
config = Configuration(None, DEFCFG_DIR)
|
||||||
|
|
||||||
|
os.environ['NOMINATIM_DATABASE_DSN'] = 'pgsql:dbname=gis;password=foo;host=localhost'
|
||||||
|
|
||||||
|
assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
|
||||||
|
|
||||||
|
def test_get_libpq_dsn_convert_libpq():
|
||||||
|
config = Configuration(None, DEFCFG_DIR)
|
||||||
|
|
||||||
|
os.environ['NOMINATIM_DATABASE_DSN'] = 'host=localhost dbname=gis password=foo'
|
||||||
|
|
||||||
|
assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
|
||||||
|
|||||||
Reference in New Issue
Block a user