Skip to content

Commit eb08bea

Browse files
Expand environment variable in connection string
1 parent 5a27bc8 commit eb08bea

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/sql/parse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from os.path import expandvars
12
import six
23
from six.moves import configparser as CP
34
from sqlalchemy.engine.url import URL
@@ -9,6 +10,7 @@ def parse(cell, config):
910
parts = [part.strip() for part in cell.split(None, 1)]
1011
if not parts:
1112
return {'connection': '', 'sql': ''}
13+
parts[0] = expandvars(parts[0]) # for environment variables
1214

1315
if parts[0].startswith('[') and parts[0].endswith(']'):
1416
section = parts[0].lstrip('[').rstrip(']')

src/tests/test_parse.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from sql.parse import parse
23
from six.moves import configparser
34
try:
@@ -12,22 +13,29 @@ def test_parse_no_sql():
1213
{'connection': "will:longliveliz@localhost/shakes",
1314
'sql': '',
1415
'flags': default_flags}
15-
16+
1617
def test_parse_with_sql():
17-
assert parse("postgresql://will:longliveliz@localhost/shakes SELECT * FROM work",
18+
assert parse("postgresql://will:longliveliz@localhost/shakes SELECT * FROM work",
1819
empty_config) == \
1920
{'connection': "postgresql://will:longliveliz@localhost/shakes",
2021
'sql': 'SELECT * FROM work',
21-
'flags': default_flags}
22-
22+
'flags': default_flags}
23+
2324
def test_parse_sql_only():
2425
assert parse("SELECT * FROM work", empty_config) == \
2526
{'connection': "",
2627
'sql': 'SELECT * FROM work',
27-
'flags': default_flags}
28-
28+
'flags': default_flags}
29+
2930
def test_parse_postgresql_socket_connection():
3031
assert parse("postgresql:///shakes SELECT * FROM work", empty_config) == \
3132
{'connection': "postgresql:///shakes",
3233
'sql': 'SELECT * FROM work',
33-
'flags': default_flags}
34+
'flags': default_flags}
35+
36+
def test_expand_environment_variables_in_connection():
37+
os.environ['DATABASE_URL'] = 'postgresql:///shakes'
38+
assert parse("$DATABASE_URL SELECT * FROM work", empty_config) == \
39+
{'connection': "postgresql:///shakes",
40+
'sql': 'SELECT * FROM work',
41+
'flags': default_flags}

0 commit comments

Comments
 (0)