Skip to content

Commit

Permalink
generalize BaseConfig.update_from + call adapter factory methods in c…
Browse files Browse the repository at this point in the history
…ore (#9335)
  • Loading branch information
MichelleArk authored Jan 5, 2024
1 parent 43136bb commit 125982a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20240104-135849.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Remove usage of dbt.adapters.factory in dbt/common
time: 2024-01-04T13:58:49.221966-05:00
custom:
Author: michelleark
Issue: "9334"
16 changes: 8 additions & 8 deletions core/dbt/common/contracts/config/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# necessary for annotating constructors
from __future__ import annotations

from dataclasses import dataclass, Field

from itertools import chain
from typing import Callable, Dict, Any, List, TypeVar
from typing import Callable, Dict, Any, List, TypeVar, Type

from dbt.common.contracts.config.metadata import Metadata
from dbt.common.exceptions import CompilationError, DbtInternalError
Expand Down Expand Up @@ -140,21 +143,18 @@ def _merge_dicts(cls, src: Dict[str, Any], data: Dict[str, Any]) -> Dict[str, An
)
return result

def update_from(self: T, data: Dict[str, Any], adapter_type: str, validate: bool = True) -> T:
def update_from(
self: T, data: Dict[str, Any], config_cls: Type[BaseConfig], validate: bool = True
) -> T:
"""Given a dict of keys, update the current config from them, validate
it, and return a new config with the updated values
"""
# sadly, this is a circular import
from dbt.adapters.factory import get_config_class_by_name

dct = self.to_dict(omit_none=False)

adapter_config_cls = get_config_class_by_name(adapter_type)

self_merged = self._merge_dicts(dct, data)
dct.update(self_merged)

adapter_merged = adapter_config_cls._merge_dicts(dct, data)
adapter_merged = config_cls._merge_dicts(dct, data)
dct.update(adapter_merged)

# any remaining fields must be "clobber"
Expand Down
9 changes: 6 additions & 3 deletions core/dbt/context/context_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass
from typing import List, Iterator, Dict, Any, TypeVar, Generic, Optional

from dbt.adapters.factory import get_config_class_by_name
from dbt.config import RuntimeConfig, Project, IsFQNResource
from dbt.contracts.graph.model_config import get_config_for
from dbt.common.contracts.config.base import BaseConfig, _listify
Expand Down Expand Up @@ -199,9 +200,11 @@ def initial_result(self, resource_type: NodeType, base: bool) -> C:
def _update_from_config(self, result: C, partial: Dict[str, Any], validate: bool = False) -> C:
translated = self._active_project.credentials.translate_aliases(partial)
translated = self.translate_hook_names(translated)
updated = result.update_from(
translated, self._active_project.credentials.type, validate=validate
)

adapter_type = self._active_project.credentials.type
adapter_config_cls = get_config_class_by_name(adapter_type)

updated = result.update_from(translated, adapter_config_cls, validate=validate)
return updated

def translate_hook_names(self, project_dict):
Expand Down

0 comments on commit 125982a

Please sign in to comment.