-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
732693a
commit f1762f2
Showing
7 changed files
with
118 additions
and
130 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
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
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,33 @@ | ||
from importlib import import_module | ||
|
||
from dbt.adapters.factory import Adapter | ||
from dbt.events.functions import fire_event | ||
from dbt.events.types import AdapterRegistered | ||
from dbt.semver import VersionSpecifier | ||
|
||
|
||
def register_adapter(self, config: 'AdapterRequiredConfig') -> None: | ||
# ==== CUSTOM CODE ==== | ||
# ==== END CUSTOM CODE ==== | ||
adapter_name = config.credentials.type | ||
adapter_type = self.get_adapter_class_by_name(adapter_name) | ||
adapter_version = import_module(f".{adapter_name}.__version__", "dbt.adapters").version | ||
# ==== CUSTOM CODE ==== | ||
custom_adapter_class_name: str = self.get_custom_adapter_config_value(config) | ||
if custom_adapter_class_name and custom_adapter_class_name.strip(): | ||
# OVERRIDE DEFAULT ADAPTER BY USER GIVEN ADAPTER CLASS | ||
adapter_type = self.get_custom_adapter_class_by_name(custom_adapter_class_name) | ||
# ==== END CUSTOM CODE ==== | ||
adapter_version_specifier = VersionSpecifier.from_version_string( | ||
adapter_version | ||
).to_version_string() | ||
fire_event( | ||
AdapterRegistered(adapter_name=adapter_name, adapter_version=adapter_version_specifier) | ||
) | ||
with self.lock: | ||
if adapter_name in self.adapters: | ||
# this shouldn't really happen... | ||
return | ||
|
||
adapter: Adapter = adapter_type(config) # type: ignore | ||
self.adapters[adapter_name] = adapter |
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,38 @@ | ||
from multiprocessing.context import SpawnContext | ||
from typing import Optional | ||
|
||
from dbt.adapters.contracts.connection import AdapterRequiredConfig | ||
from dbt.adapters.events.types import ( | ||
AdapterRegistered, | ||
) | ||
from dbt.adapters.factory import Adapter | ||
from dbt_common.events.base_types import EventLevel | ||
from dbt_common.events.functions import fire_event | ||
|
||
|
||
def register_adapter( | ||
self, | ||
config: 'AdapterRequiredConfig', | ||
mp_context: SpawnContext, | ||
adapter_registered_log_level: Optional[EventLevel] = EventLevel.INFO, | ||
) -> None: | ||
adapter_name = config.credentials.type | ||
adapter_type = self.get_adapter_class_by_name(adapter_name) | ||
adapter_version = self._adapter_version(adapter_name) | ||
# ==== CUSTOM CODE ==== | ||
custom_adapter_class_name: str = self.get_custom_adapter_config_value(config) | ||
if custom_adapter_class_name and custom_adapter_class_name.strip(): | ||
# OVERRIDE DEFAULT ADAPTER BY USER GIVEN ADAPTER CLASS | ||
adapter_type = self.get_custom_adapter_class_by_name(custom_adapter_class_name) | ||
# ==== END CUSTOM CODE ==== | ||
fire_event( | ||
AdapterRegistered(adapter_name=adapter_name, adapter_version=adapter_version), | ||
level=adapter_registered_log_level, | ||
) | ||
with self.lock: | ||
if adapter_name in self.adapters: | ||
# this shouldn't really happen... | ||
return | ||
|
||
adapter: Adapter = adapter_type(config, mp_context) # type: ignore | ||
self.adapters[adapter_name] = adapter |
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,32 @@ | ||
import importlib | ||
|
||
DBT_CUSTOM_ADAPTER_VAR = 'dbt_custom_adapter' | ||
|
||
|
||
def get_custom_adapter_config_value(self, config: 'AdapterRequiredConfig') -> str: | ||
# FIRST: it's set as cli value: dbt run --vars {'dbt_custom_adapter': 'custom_adapters.DuckDBAdapterV1Custom'} | ||
if hasattr(config, 'cli_vars') and DBT_CUSTOM_ADAPTER_VAR in config.cli_vars: | ||
custom_adapter_class_name: str = config.cli_vars[DBT_CUSTOM_ADAPTER_VAR] | ||
if custom_adapter_class_name and custom_adapter_class_name.strip(): | ||
return custom_adapter_class_name | ||
# SECOND: it's set inside dbt_project.yml | ||
if hasattr(config, 'vars') and DBT_CUSTOM_ADAPTER_VAR in config.vars.to_dict(): | ||
custom_adapter_class_name: str = config.vars.to_dict()[DBT_CUSTOM_ADAPTER_VAR] | ||
if custom_adapter_class_name and custom_adapter_class_name.strip(): | ||
return custom_adapter_class_name | ||
|
||
return None | ||
|
||
|
||
def get_custom_adapter_class_by_name(self, custom_adapter_class_name: str): | ||
if "." not in custom_adapter_class_name: | ||
raise ValueError(f"Unexpected adapter class name: `{custom_adapter_class_name}` ," | ||
f"Expecting something like:`my.sample.library.MyAdapterClass`") | ||
|
||
__module, __class = custom_adapter_class_name.rsplit('.', 1) | ||
try: | ||
user_adapter_module = importlib.import_module(__module) | ||
user_adapter_class = getattr(user_adapter_module, __class) | ||
return user_adapter_class | ||
except ModuleNotFoundError as mnfe: | ||
raise Exception(f"Module of provided adapter not found, provided: {custom_adapter_class_name}") from mnfe |
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
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