Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generalize BaseConfig.update_from + call adapter factory methods in core #9335

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was sad!

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