Skip to content

Commit d009a33

Browse files
change the lib to make uri more consistent
1 parent 96b07fe commit d009a33

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/diffa/config.py

Lines changed: 7 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

@@ -54,12 +53,12 @@ def _parse_db_info(self):
5453
# Users can ref an env var in the DB_URI in the command
5554
if self.db_uri.startswith("$"):
5655
self.db_uri = os.getenv(self.db_uri[1:], "")
57-
dns = dsnparse.parse(self.db_uri)
56+
dns = urlparse(self.db_uri)
5857
parsed_db_info = self._extract_db_details(dns)
5958
self._validate_parsed_db_info(parsed_db_info)
6059
return parsed_db_info
61-
except TypeError:
62-
logger.error("Invalid db info", exc_info=True)
60+
except Exception as e:
61+
logger.error(f"Error parsing DB info: {e}")
6362
raise
6463

6564
def _validate_parsed_db_info(self, parsed_db_info: dict):
@@ -70,17 +69,17 @@ def _validate_parsed_db_info(self, parsed_db_info: dict):
7069
)
7170

7271
def _extract_db_details(self, dns):
73-
db_database = self.db_name or dns.database
72+
db_database = self.db_name or dns.path.lstrip("/")
7473
return {
75-
"host": dns.host,
74+
"host": dns.hostname,
7675
"scheme": dns.scheme,
7776
"port": dns.port,
7877
"database": db_database,
7978
"user": dns.username,
8079
"password": dns.password,
8180
"schema": self.db_schema,
8281
"table": self.db_table,
83-
"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}",
8483
}
8584

8685
def get_db_config(self):

tests/configs/test_config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@
9090
"db_uri": "postgresql://user:password@localhost:5432/mydb",
9191
},
9292
),
93+
# Case 5: Complete db_uri but email-format username, missing database parametter
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+
),
93111
],
94112
)
95113
def test_db_config_parse_db_info(

0 commit comments

Comments
 (0)