Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 6c9ab5f

Browse files
author
Sergey Vasilyev
committed
Accept external database types/schemes at runtime
1 parent c1dde75 commit 6c9ab5f

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

data_diff/databases/_connect.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from data_diff.databases.mssql import MsSQL
2727

2828

29-
@attrs.define(frozen=True)
29+
@attrs.frozen
3030
class MatchUriPath:
3131
database_cls: Type[Database]
3232

@@ -98,13 +98,11 @@ class Connect:
9898
"""Provides methods for connecting to a supported database using a URL or connection dict."""
9999

100100
database_by_scheme: Dict[str, Database]
101-
match_uri_path: Dict[str, MatchUriPath]
102101
conn_cache: MutableMapping[Hashable, Database]
103102

104103
def __init__(self, database_by_scheme: Dict[str, Database] = DATABASE_BY_SCHEME):
105104
super().__init__()
106105
self.database_by_scheme = database_by_scheme
107-
self.match_uri_path = {name: MatchUriPath(cls) for name, cls in database_by_scheme.items()}
108106
self.conn_cache = weakref.WeakValueDictionary()
109107

110108
def for_databases(self, *dbs) -> Self:
@@ -157,12 +155,10 @@ def connect_to_uri(self, db_uri: str, thread_count: Optional[int] = 1, **kwargs)
157155
return self.connect_with_dict(conn_dict, thread_count, **kwargs)
158156

159157
try:
160-
matcher = self.match_uri_path[scheme]
158+
cls = self.database_by_scheme[scheme]
161159
except KeyError:
162160
raise NotImplementedError(f"Scheme '{scheme}' currently not supported")
163161

164-
cls = matcher.database_cls
165-
166162
if scheme == "databricks":
167163
assert not dsn.user
168164
kw = {}
@@ -175,6 +171,7 @@ def connect_to_uri(self, db_uri: str, thread_count: Optional[int] = 1, **kwargs)
175171
kw["filepath"] = dsn.dbname
176172
kw["dbname"] = dsn.user
177173
else:
174+
matcher = MatchUriPath(cls)
178175
kw = matcher.match_path(dsn)
179176

180177
if scheme == "bigquery":
@@ -198,7 +195,7 @@ def connect_to_uri(self, db_uri: str, thread_count: Optional[int] = 1, **kwargs)
198195

199196
kw = {k: v for k, v in kw.items() if v is not None}
200197

201-
if issubclass(cls, ThreadedDatabase):
198+
if isinstance(cls, type) and issubclass(cls, ThreadedDatabase):
202199
db = cls(thread_count=thread_count, **kw, **kwargs)
203200
else:
204201
db = cls(**kw, **kwargs)
@@ -209,11 +206,10 @@ def connect_with_dict(self, d, thread_count, **kwargs):
209206
d = dict(d)
210207
driver = d.pop("driver")
211208
try:
212-
matcher = self.match_uri_path[driver]
209+
cls = self.database_by_scheme[driver]
213210
except KeyError:
214211
raise NotImplementedError(f"Driver '{driver}' currently not supported")
215212

216-
cls = matcher.database_cls
217213
if issubclass(cls, ThreadedDatabase):
218214
db = cls(thread_count=thread_count, **d, **kwargs)
219215
else:

0 commit comments

Comments
 (0)