Skip to content

Commit 2d8059b

Browse files
Merge pull request #10 from Kaligo/DATA-5768/env_vars_in_diffa_command
[DATA-5768][DATA-3562] Support env vars passing into the diff command
2 parents ce62559 + 574b473 commit 2d8059b

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/diffa/config.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import json
33
from datetime import date
44
from enum import Enum
5-
6-
import dsnparse
5+
from urllib.parse import urlparse
76

87
from diffa.utils import Logger
98

@@ -51,12 +50,15 @@ def update(self, **kwargs):
5150

5251
def _parse_db_info(self):
5352
try:
54-
dns = dsnparse.parse(self.db_uri)
53+
# Users can ref an env var in the DB_URI in the command
54+
if self.db_uri.startswith("$"):
55+
self.db_uri = os.getenv(self.db_uri[1:], "")
56+
dns = urlparse(self.db_uri)
5557
parsed_db_info = self._extract_db_details(dns)
5658
self._validate_parsed_db_info(parsed_db_info)
5759
return parsed_db_info
58-
except TypeError:
59-
logger.error("Invalid db info", exc_info=True)
60+
except Exception as e:
61+
logger.error(f"Error parsing DB info: {e}")
6062
raise
6163

6264
def _validate_parsed_db_info(self, parsed_db_info: dict):
@@ -67,17 +69,17 @@ def _validate_parsed_db_info(self, parsed_db_info: dict):
6769
)
6870

6971
def _extract_db_details(self, dns):
70-
db_database = self.db_name or dns.database
72+
db_database = self.db_name or dns.path.lstrip("/")
7173
return {
72-
"host": dns.host,
74+
"host": dns.hostname,
7375
"scheme": dns.scheme,
7476
"port": dns.port,
7577
"database": db_database,
7678
"user": dns.username,
7779
"password": dns.password,
7880
"schema": self.db_schema,
7981
"table": self.db_table,
80-
"db_uri": f"{dns.scheme}://{dns.username}:{dns.password}@{dns.host}:{dns.port}/{db_database}",
82+
"db_uri": f"{dns.scheme}://{dns.username}:{dns.password}@{dns.hostname}:{dns.port}/{db_database}",
8183
}
8284

8385
def get_db_config(self):

tests/configs/test_config.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
DIFFA_CHECK_RUNS_TABLE,
1515
)
1616

17-
17+
@patch.dict(os.environ, {"SOURCE_URI": "postgresql://user:password@localhost:5432/mydb"})
1818
@pytest.mark.parametrize(
1919
"db_uri, db_name, db_schema, db_table, expected_parsed_config",
2020
[
@@ -72,6 +72,42 @@
7272
"db_uri": "postgresql://user:password@localhost:5432/mydb",
7373
},
7474
),
75+
# Case 4: Complete db_uri but inputting using env var, missing database parameter
76+
(
77+
"$SOURCE_URI",
78+
None,
79+
"myschema",
80+
"users",
81+
{
82+
"host": "localhost",
83+
"scheme": "postgresql",
84+
"port": 5432,
85+
"database": "mydb",
86+
"user": "user",
87+
"password": "password",
88+
"schema": "myschema",
89+
"table": "users",
90+
"db_uri": "postgresql://user:password@localhost:5432/mydb",
91+
},
92+
),
93+
# Case 5: Complete db_uri but email-format username, missing database parameter
94+
(
95+
"postgresql://[email protected]:password@localhost:5432/mydb",
96+
None,
97+
"myschema",
98+
"users",
99+
{
100+
"host": "localhost",
101+
"scheme": "postgresql",
102+
"port": 5432,
103+
"database": "mydb",
104+
"user": "[email protected]",
105+
"password": "password",
106+
"schema": "myschema",
107+
"table": "users",
108+
"db_uri": "postgresql://[email protected]:password@localhost:5432/mydb",
109+
},
110+
),
75111
],
76112
)
77113
def test_db_config_parse_db_info(

0 commit comments

Comments
 (0)