-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve catalog client exception handling
- Loading branch information
1 parent
825a667
commit bf3a95f
Showing
5 changed files
with
34 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,7 @@ celerybeat.pid | |
*.sage.py | ||
|
||
# Environments | ||
*.env | ||
.env | ||
.venv | ||
env/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
from dbt.adapters.protocol import CatalogIntegrationProtocol | ||
from dbt.adapters.exceptions import DbtCatalogIntegrationAlreadyExistsError | ||
|
||
from typing import Optional | ||
|
||
class CatalogIntegrations: | ||
def __init__(self): | ||
self._integrations = {} | ||
|
||
def get(self, name: str) -> CatalogIntegrationProtocol: | ||
return self.integrations[name] | ||
def get(self, name: str) -> Optional[CatalogIntegrationProtocol]: | ||
if name in self._integrations: | ||
return self._integrations[name] | ||
return None | ||
|
||
@property | ||
def integrations(self) -> dict[str, CatalogIntegrationProtocol]: | ||
return self._integrations | ||
|
||
def add_integration(self, integration: CatalogIntegrationProtocol, catalog_name: str): | ||
if catalog_name in self._integrations: | ||
raise DbtCatalogIntegrationAlreadyExistsError(catalog_name) | ||
self._integrations[catalog_name] = integration | ||
|
||
|
||
_CATALOG_CLIENT = CatalogIntegrations() | ||
|
||
|
||
def get_catalog(integration_name: str) -> CatalogIntegrationProtocol: | ||
def get_catalog(integration_name: str) -> Optional[CatalogIntegrationProtocol]: | ||
return _CATALOG_CLIENT.get(integration_name) | ||
|
||
|
||
def add_catalog(integration: CatalogIntegrationProtocol, catalog_name: str): | ||
_CATALOG_CLIENT.add_integration(integration, catalog_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
dbt-adapters/src/dbt/adapters/exceptions/catalog_integration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
from dbt_common.exceptions import DbtRuntimeError | ||
|
||
|
||
class DbtCatalogIntegrationAlreadyExistsError(DbtRuntimeError): | ||
def __init__(self, catalog_name: str): | ||
self.catalog_name = catalog_name | ||
msg = f"Catalog integration {self.catalog_name} already exists" | ||
super().__init__(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
import pytest | ||
from dbt.adapters.clients.catalogs import add_catalog, get_catalog | ||
from dbt.adapters.exceptions.catalog_integration import DbtCatalogIntegrationAlreadyExistsError | ||
|
||
|
||
def test_adding_and_getting_catalog_integration(fake_catalog_integration): | ||
catalog = fake_catalog_integration | ||
add_catalog(catalog, catalog_name="fake_catalog") | ||
assert get_catalog("fake_catalog").catalog_name == catalog.catalog_name | ||
|
||
def test_adding_catalog_integration_that_already_exists(fake_catalog_integration): | ||
catalog = fake_catalog_integration | ||
catalog_name = "fake_catalog_2" | ||
add_catalog(catalog, catalog_name=catalog_name) | ||
with pytest.raises(DbtCatalogIntegrationAlreadyExistsError): | ||
add_catalog(catalog, catalog_name=catalog_name) | ||
|
||
def test_getting_catalog_integration_that_does_not_exist(): | ||
assert get_catalog("non_existent_catalog") is None |