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

Move Adapter Factory to Core #9274

Draft
wants to merge 16 commits into
base: feature/decouple-adapters-from-core
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
2474722
moving types_pb2.py to common/events
colin-rogers-dbt Oct 25, 2023
07743b7
merge
colin-rogers-dbt Oct 25, 2023
252e3e3
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Oct 26, 2023
ff9d519
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Nov 2, 2023
bf079b1
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Nov 28, 2023
3187ded
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Nov 28, 2023
4da67bf
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Dec 5, 2023
c03823f
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Dec 6, 2023
2811ccd
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Dec 7, 2023
e88138a
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Dec 7, 2023
977a842
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Dec 7, 2023
870bc37
initial factory refactor
colin-rogers-dbt Dec 11, 2023
fe82ef2
split adapters factory into client and load_adapter
colin-rogers-dbt Dec 12, 2023
3ddfcdd
Merge branch 'feature/decouple-adapters-from-core' of https://github.…
colin-rogers-dbt Dec 12, 2023
ba1a911
merge feature/decouple-adapters-from-core
colin-rogers-dbt Dec 12, 2023
f113d38
fix import broken on merge
colin-rogers-dbt Dec 12, 2023
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
14 changes: 1 addition & 13 deletions core/dbt/adapters/events/adapter_types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,7 @@ message CacheDumpGraphMsg {


// Skipping E032, E033, E034



// E034
message AdapterRegistered {
string adapter_name = 1;
string adapter_version = 2;
}

message AdapterRegisteredMsg {
AdapterCommonEventInfo info = 1;
AdapterRegistered data = 2;
}
// E034 is defined in dbt-core

// E035
message AdapterImportError {
Expand Down
365 changes: 179 additions & 186 deletions core/dbt/adapters/events/adapter_types_pb2.py

Large diffs are not rendered by default.

12 changes: 1 addition & 11 deletions core/dbt/adapters/events/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@

class AdapterDeprecationWarning(WarnLevel):
def code(self) -> str:
return "D005"

Check warning on line 32 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L32

Added line #L32 was not covered by tests

def message(self) -> str:
description = (

Check warning on line 35 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L35

Added line #L35 was not covered by tests
f"The adapter function `adapter.{self.old_name}` is deprecated and will be removed in "
f"a future release of dbt. Please use `adapter.{self.new_name}` instead. "
f"\n\nDocumentation for {self.new_name} can be found here:"
f"\n\nhttps://docs.getdbt.com/docs/adapter"
)
return line_wrap_message(warning_tag(f"Deprecated functionality\n\n{description}"))

Check warning on line 41 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L41

Added line #L41 was not covered by tests


# =======================================================
Expand All @@ -56,26 +56,26 @@

class AdapterEventInfo(InfoLevel):
def code(self) -> str:
return "E002"

Check warning on line 59 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L59

Added line #L59 was not covered by tests

def message(self) -> str:
return format_adapter_message(self.name, self.base_msg, self.args)

Check warning on line 62 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L62

Added line #L62 was not covered by tests


class AdapterEventWarning(WarnLevel):
def code(self) -> str:
return "E003"

Check warning on line 67 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L67

Added line #L67 was not covered by tests

def message(self) -> str:
return format_adapter_message(self.name, self.base_msg, self.args)

Check warning on line 70 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L70

Added line #L70 was not covered by tests


class AdapterEventError(ErrorLevel):
def code(self) -> str:
return "E004"

Check warning on line 75 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L75

Added line #L75 was not covered by tests

def message(self) -> str:
return format_adapter_message(self.name, self.base_msg, self.args)

Check warning on line 78 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L78

Added line #L78 was not covered by tests


class NewConnection(DebugLevel):
Expand All @@ -96,10 +96,10 @@

class ConnectionLeftOpenInCleanup(DebugLevel):
def code(self) -> str:
return "E007"

Check warning on line 99 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L99

Added line #L99 was not covered by tests

def message(self) -> str:
return f"Connection '{self.conn_name}' was left open."

Check warning on line 102 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L102

Added line #L102 was not covered by tests


class ConnectionClosedInCleanup(DebugLevel):
Expand All @@ -112,10 +112,10 @@

class RollbackFailed(DebugLevel):
def code(self) -> str:
return "E009"

Check warning on line 115 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L115

Added line #L115 was not covered by tests

def message(self) -> str:
return f"Failed to rollback '{self.conn_name}'"

Check warning on line 118 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L118

Added line #L118 was not covered by tests


class ConnectionClosed(DebugLevel):
Expand Down Expand Up @@ -251,14 +251,14 @@
return f"old key {ref_key} not found in self.relations, assuming temporary"
elif self.action == "rename_relation":
return f"Renaming relation {ref_key} to {ref_key_2}"
elif self.action == "uncached_relation":
return (

Check warning on line 255 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L254-L255

Added lines #L254 - L255 were not covered by tests
f"{ref_key_2} references {ref_key} "
f"but {self.ref_key.database}.{self.ref_key.schema}"
"is not in the cache, skipping assumed external relation"
)
else:
return ref_key

Check warning on line 261 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L261

Added line #L261 was not covered by tests


# Skipping E023, E024, E025, E026, E027, E028, E029, E030
Expand All @@ -266,37 +266,27 @@

class CacheDumpGraph(DebugLevel):
def code(self) -> str:
return "E031"

Check warning on line 269 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L269

Added line #L269 was not covered by tests

def message(self) -> str:
return f"dump {self.before_after} {self.action} : {self.dump}"

Check warning on line 272 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L272

Added line #L272 was not covered by tests


# Skipping E032, E033, E034


class AdapterRegistered(InfoLevel):
def code(self) -> str:
return "E034"

def message(self) -> str:
return f"Registered adapter: {self.adapter_name}{self.adapter_version}"


# Skipping E032, E033
class AdapterImportError(InfoLevel):
def code(self) -> str:
return "E035"

Check warning on line 278 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L278

Added line #L278 was not covered by tests

def message(self) -> str:
return f"Error importing adapter: {self.exc}"

Check warning on line 281 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L281

Added line #L281 was not covered by tests


class PluginLoadError(DebugLevel):
def code(self) -> str:
return "E036"

Check warning on line 286 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L286

Added line #L286 was not covered by tests

def message(self) -> str:
return f"{self.exc_info}"

Check warning on line 289 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L289

Added line #L289 was not covered by tests


class NewConnectionOpening(DebugLevel):
Expand All @@ -309,23 +299,23 @@

class CodeExecution(DebugLevel):
def code(self) -> str:
return "E038"

Check warning on line 302 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L302

Added line #L302 was not covered by tests

def message(self) -> str:
return f"On {self.conn_name}: {self.code_content}"

Check warning on line 305 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L305

Added line #L305 was not covered by tests


class CodeExecutionStatus(DebugLevel):
def code(self) -> str:
return "E039"

Check warning on line 310 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L310

Added line #L310 was not covered by tests

def message(self) -> str:
return f"Execution status: {self.status} in {self.elapsed} seconds"

Check warning on line 313 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L313

Added line #L313 was not covered by tests


class CatalogGenerationError(WarnLevel):
def code(self) -> str:
return "E040"

Check warning on line 318 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L318

Added line #L318 was not covered by tests

def message(self) -> str:
return f"Encountered an error while generating catalog: {self.exc}"
Expand All @@ -333,10 +323,10 @@

class WriteCatalogFailure(ErrorLevel):
def code(self) -> str:
return "E041"

Check warning on line 326 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L326

Added line #L326 was not covered by tests

def message(self) -> str:
return (

Check warning on line 329 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L329

Added line #L329 was not covered by tests
f"dbt encountered {self.num_exceptions} failure{(self.num_exceptions != 1) * 's'} "
"while writing the catalog"
)
Expand All @@ -352,10 +342,10 @@

class CannotGenerateDocs(InfoLevel):
def code(self) -> str:
return "E043"

Check warning on line 345 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L345

Added line #L345 was not covered by tests

def message(self) -> str:
return "compile failed, cannot generate docs"

Check warning on line 348 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L348

Added line #L348 was not covered by tests


class BuildingCatalog(InfoLevel):
Expand Down Expand Up @@ -393,25 +383,25 @@

class ConstraintNotEnforced(WarnLevel):
def code(self) -> str:
return "E048"

Check warning on line 386 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L386

Added line #L386 was not covered by tests

def message(self) -> str:
msg = (

Check warning on line 389 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L389

Added line #L389 was not covered by tests
f"The constraint type {self.constraint} is not enforced by {self.adapter}. "
"The constraint will be included in this model's DDL statement, but it will not "
"guarantee anything about the underlying data. Set 'warn_unenforced: false' on "
"this constraint to ignore this warning."
)
return line_wrap_message(warning_tag(msg))

Check warning on line 395 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L395

Added line #L395 was not covered by tests


class ConstraintNotSupported(WarnLevel):
def code(self) -> str:
return "E049"

Check warning on line 400 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L400

Added line #L400 was not covered by tests

def message(self) -> str:
msg = (

Check warning on line 403 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L403

Added line #L403 was not covered by tests
f"The constraint type {self.constraint} is not supported by {self.adapter}, and will "
"be ignored. Set 'warn_unsupported: false' on this constraint to ignore this warning."
)
return line_wrap_message(warning_tag(msg))

Check warning on line 407 in core/dbt/adapters/events/types.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/events/types.py#L407

Added line #L407 was not covered by tests
8 changes: 8 additions & 0 deletions core/dbt/adapters/include/global_project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@

PACKAGE_PATH = os.path.dirname(__file__)
PROJECT_NAME = "dbt"


def get_global_project_path() -> str:
return PACKAGE_PATH

Check warning on line 8 in core/dbt/adapters/include/global_project/__init__.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/include/global_project/__init__.py#L8

Added line #L8 was not covered by tests


def get_global_project_name() -> str:
return PROJECT_NAME

Check warning on line 12 in core/dbt/adapters/include/global_project/__init__.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/include/global_project/__init__.py#L12

Added line #L12 was not covered by tests
26 changes: 26 additions & 0 deletions core/dbt/adapters/load_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import traceback
from importlib import import_module
from types import ModuleType


from dbt.adapters.events.types import AdapterImportError, PluginLoadError
from dbt.common.events.functions import fire_event
from dbt.common.exceptions import DbtRuntimeError


def get_adapter_by_name(name: str) -> ModuleType:
try:
# mypy doesn't think modules have any attributes.
mod: ModuleType = import_module("." + name, "dbt.adapters")
return mod
except ModuleNotFoundError as exc:

Check warning on line 16 in core/dbt/adapters/load_adapter.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/load_adapter.py#L16

Added line #L16 was not covered by tests
# if we failed to import the target module in particular, inform
# the user about it via a runtime error
if exc.name == "dbt.adapters." + name:
fire_event(AdapterImportError(exc=str(exc)))
raise DbtRuntimeError(f"Could not find adapter type {name}!")

Check warning on line 21 in core/dbt/adapters/load_adapter.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/load_adapter.py#L19-L21

Added lines #L19 - L21 were not covered by tests
# otherwise, the error had to have come from some underlying
# library. Log the stack trace.

fire_event(PluginLoadError(exc_info=traceback.format_exc()))
raise

Check warning on line 26 in core/dbt/adapters/load_adapter.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/load_adapter.py#L25-L26

Added lines #L25 - L26 were not covered by tests
2 changes: 1 addition & 1 deletion core/dbt/cli/requires.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dbt.tracking
from dbt.common.invocation import reset_invocation_id
from dbt.version import installed as installed_version
from dbt.adapters.factory import adapter_management, register_adapter, get_adapter
from dbt.clients.adapter import adapter_management, register_adapter, get_adapter
from dbt.flags import set_flags, get_flag_dict
from dbt.cli.exceptions import (
ExceptionExit,
Expand Down
1 change: 1 addition & 0 deletions core/dbt/clients/adapter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from dbt.clients.adapter.client import * # noqa
88 changes: 88 additions & 0 deletions core/dbt/clients/adapter/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from contextlib import contextmanager

from pathlib import Path

from typing import Type, Optional, List
from dbt.adapters.contracts.connection import AdapterRequiredConfig, Credentials
from dbt.adapters.protocol import AdapterProtocol, AdapterConfig, RelationProtocol
from dbt.clients.adapter.container import AdapterContainer

_FACTORY: AdapterContainer = AdapterContainer()


def register_adapter(config: AdapterRequiredConfig) -> None:
global _FACTORY
_FACTORY.register_adapter(config)


def get_adapter(config: AdapterRequiredConfig):
global _FACTORY
return _FACTORY.lookup_adapter(config.credentials.type)


def get_adapter_by_type(adapter_type):
global _FACTORY
return _FACTORY.lookup_adapter(adapter_type)


def reset_adapters():
"""Clear the adapters. This is useful for tests, which change configs."""
global _FACTORY
_FACTORY.reset_adapters()


def cleanup_connections():
"""Only clean up the adapter connections list without resetting the actual
adapters.
"""
global _FACTORY
_FACTORY.cleanup_connections()


def get_adapter_class_by_name(name: str) -> Type[AdapterProtocol]:
global _FACTORY
return _FACTORY.get_adapter_class_by_name(name)

Check warning on line 44 in core/dbt/clients/adapter/client.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/clients/adapter/client.py#L44

Added line #L44 was not covered by tests


def get_config_class_by_name(name: str) -> Type[AdapterConfig]:
global _FACTORY
return _FACTORY.get_config_class_by_name(name)


def get_relation_class_by_name(name: str) -> Type[RelationProtocol]:
global _FACTORY
return _FACTORY.get_relation_class_by_name(name)


def load_plugin(name: str) -> Type[Credentials]:
global _FACTORY
return _FACTORY.load_plugin(name)


def get_include_paths(name: Optional[str]) -> List[Path]:
global _FACTORY
return _FACTORY.get_include_paths(name)


def get_adapter_package_names(name: Optional[str]) -> List[str]:
global _FACTORY
return _FACTORY.get_adapter_package_names(name)


def get_adapter_type_names(name: Optional[str]) -> List[str]:
global _FACTORY
return _FACTORY.get_adapter_type_names(name)


def get_adapter_constraint_support(name: Optional[str]) -> List[str]:
global _FACTORY
return _FACTORY.get_adapter_constraint_support(name)


@contextmanager
def adapter_management():
reset_adapters()
try:
yield
finally:
cleanup_connections()
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import threading
import traceback
from contextlib import contextmanager
from importlib import import_module

from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Type

from dbt.adapters.base.plugin import AdapterPlugin
from dbt.adapters.protocol import AdapterConfig, AdapterProtocol, RelationProtocol
from dbt.adapters.contracts.connection import AdapterRequiredConfig, Credentials
from typing import Dict, Type, Any, Optional, List, Set

import threading

from dbt.adapters.base import AdapterPlugin
from dbt.adapters.contracts.connection import Credentials, AdapterRequiredConfig
from dbt.adapters.include.global_project import (
PROJECT_NAME as GLOBAL_PROJECT_NAME,
PACKAGE_PATH as GLOBAL_PROJECT_PATH,
)
from dbt.adapters import load_adapter
from dbt.adapters.protocol import RelationProtocol, AdapterConfig, AdapterProtocol
from dbt.common.events.functions import fire_event
from dbt.adapters.events.types import AdapterImportError, PluginLoadError, AdapterRegistered
from dbt.common.exceptions import DbtInternalError, DbtRuntimeError
from dbt.adapters.include.global_project import PACKAGE_PATH as GLOBAL_PROJECT_PATH
from dbt.adapters.include.global_project import PROJECT_NAME as GLOBAL_PROJECT_NAME
from dbt.common.exceptions import DbtRuntimeError, DbtInternalError
from dbt.common.semver import VersionSpecifier
from dbt.events.types import AdapterRegistered
from dbt.mp_context import get_mp_context

Adapter = AdapterProtocol


class AdapterContainer:
def __init__(self) -> None:
self.lock = threading.Lock()
self.adapters: Dict[str, Adapter] = {}
self.adapters: Dict[str, AdapterProtocol] = {}
self.plugins: Dict[str, AdapterPlugin] = {}
# map package names to their include paths
self.packages: Dict[str, Path] = {
Expand All @@ -38,7 +40,7 @@ def get_plugin_by_name(self, name: str) -> AdapterPlugin:
message = f"Invalid adapter type {name}! Must be one of {names}"
raise DbtRuntimeError(message)

def get_adapter_class_by_name(self, name: str) -> Type[Adapter]:
def get_adapter_class_by_name(self, name: str) -> Type[AdapterProtocol]:
plugin = self.get_plugin_by_name(name)
return plugin.adapter

Expand All @@ -54,20 +56,8 @@ def load_plugin(self, name: str) -> Type[Credentials]:
# this doesn't need a lock: in the worst case we'll overwrite packages
# and adapter_type entries with the same value, as they're all
# singletons
try:
# mypy doesn't think modules have any attributes.
mod: Any = import_module("." + name, "dbt.adapters")
except ModuleNotFoundError as exc:
# if we failed to import the target module in particular, inform
# the user about it via a runtime error
if exc.name == "dbt.adapters." + name:
fire_event(AdapterImportError(exc=str(exc)))
raise DbtRuntimeError(f"Could not find adapter type {name}!")
# otherwise, the error had to have come from some underlying
# library. Log the stack trace.

fire_event(PluginLoadError(exc_info=traceback.format_exc()))
raise
mod: Any = load_adapter.get_adapter_by_name(name)

plugin: AdapterPlugin = mod.Plugin
plugin_type = plugin.adapter.type()

Expand Down Expand Up @@ -103,10 +93,10 @@ def register_adapter(self, config: AdapterRequiredConfig) -> None:
# this shouldn't really happen...
return

adapter: Adapter = adapter_type(config, get_mp_context()) # type: ignore
adapter: AdapterProtocol = adapter_type(config, get_mp_context()) # type: ignore
self.adapters[adapter_name] = adapter

def lookup_adapter(self, adapter_name: str) -> Adapter:
def lookup_adapter(self, adapter_name: str) -> AdapterProtocol:
return self.adapters[adapter_name]

def reset_adapters(self):
Expand Down Expand Up @@ -168,71 +158,3 @@ def get_adapter_type_names(self, name: Optional[str]) -> List[str]:

def get_adapter_constraint_support(self, name: Optional[str]) -> List[str]:
return self.lookup_adapter(name).CONSTRAINT_SUPPORT # type: ignore


FACTORY: AdapterContainer = AdapterContainer()


def register_adapter(config: AdapterRequiredConfig) -> None:
FACTORY.register_adapter(config)


def get_adapter(config: AdapterRequiredConfig):
return FACTORY.lookup_adapter(config.credentials.type)


def get_adapter_by_type(adapter_type):
return FACTORY.lookup_adapter(adapter_type)


def reset_adapters():
"""Clear the adapters. This is useful for tests, which change configs."""
FACTORY.reset_adapters()


def cleanup_connections():
"""Only clean up the adapter connections list without resetting the actual
adapters.
"""
FACTORY.cleanup_connections()


def get_adapter_class_by_name(name: str) -> Type[AdapterProtocol]:
return FACTORY.get_adapter_class_by_name(name)


def get_config_class_by_name(name: str) -> Type[AdapterConfig]:
return FACTORY.get_config_class_by_name(name)


def get_relation_class_by_name(name: str) -> Type[RelationProtocol]:
return FACTORY.get_relation_class_by_name(name)


def load_plugin(name: str) -> Type[Credentials]:
return FACTORY.load_plugin(name)


def get_include_paths(name: Optional[str]) -> List[Path]:
return FACTORY.get_include_paths(name)


def get_adapter_package_names(name: Optional[str]) -> List[str]:
return FACTORY.get_adapter_package_names(name)


def get_adapter_type_names(name: Optional[str]) -> List[str]:
return FACTORY.get_adapter_type_names(name)


def get_adapter_constraint_support(name: Optional[str]) -> List[str]:
return FACTORY.get_adapter_constraint_support(name)


@contextmanager
def adapter_management():
reset_adapters()
try:
yield
finally:
cleanup_connections()
2 changes: 1 addition & 1 deletion core/dbt/common/contracts/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@

# enable syntax like: config['key'] = value
def __setitem__(self, key, value):
if hasattr(self, key):
setattr(self, key, value)

Check warning on line 32 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L31-L32

Added lines #L31 - L32 were not covered by tests
else:
self._extra[key] = value

Check warning on line 34 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L34

Added line #L34 was not covered by tests

def __delitem__(self, key):
if hasattr(self, key):
msg = (

Check warning on line 38 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L37-L38

Added lines #L37 - L38 were not covered by tests
'Error, tried to delete config key "{}": Cannot delete ' "built-in keys"
).format(key)
raise CompilationError(msg)

Check warning on line 41 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L41

Added line #L41 was not covered by tests
else:
del self._extra[key]

Check warning on line 43 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L43

Added line #L43 was not covered by tests

def _content_iterator(self, include_condition: Callable[[Field], bool]):
seen = set()
Expand All @@ -49,16 +49,16 @@
if include_condition(fld):
yield fld.name

for key in self._extra:
if key not in seen:
seen.add(key)
yield key

Check warning on line 55 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L52-L55

Added lines #L52 - L55 were not covered by tests

def __iter__(self):
yield from self._content_iterator(include_condition=lambda f: True)

def __len__(self):
return len(self._get_fields()) + len(self._extra)

Check warning on line 61 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L61

Added line #L61 was not covered by tests

@staticmethod
def compare_key(
Expand All @@ -69,7 +69,7 @@
if key not in unrendered and key not in other:
return True
elif key not in unrendered and key in other:
return False

Check warning on line 72 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L72

Added line #L72 was not covered by tests
elif key in unrendered and key not in other:
return False
else:
Expand All @@ -88,9 +88,9 @@

for key in chain(unrendered, other):
if key not in seen:
seen.add(key)
if not cls.compare_key(unrendered, other, key):
return False

Check warning on line 93 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L91-L93

Added lines #L91 - L93 were not covered by tests
return True

# This is used in 'add_config_call' to create the combined config_call_dict.
Expand Down Expand Up @@ -145,7 +145,7 @@
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
from dbt.clients.adapter import get_config_class_by_name

dct = self.to_dict(omit_none=False)

Expand All @@ -162,7 +162,7 @@

# any validation failures must have come from the update
if validate:
self.validate(dct)

Check warning on line 165 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L165

Added line #L165 was not covered by tests
return self.from_dict(dct)

def finalize_and_validate(self: T) -> T:
Expand Down Expand Up @@ -224,17 +224,17 @@
return _listify(self_value) + _listify(other_value)
elif merge_behavior == MergeBehavior.Update:
if not isinstance(self_value, dict):
raise DbtInternalError(f"expected dict, got {self_value}")

Check warning on line 227 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L227

Added line #L227 was not covered by tests
if not isinstance(other_value, dict):
raise DbtInternalError(f"expected dict, got {other_value}")

Check warning on line 229 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L229

Added line #L229 was not covered by tests
value = self_value.copy()
value.update(other_value)
return value
elif merge_behavior == MergeBehavior.DictKeyAppend:
if not isinstance(self_value, dict):
raise DbtInternalError(f"expected dict, got {self_value}")

Check warning on line 235 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L235

Added line #L235 was not covered by tests
if not isinstance(other_value, dict):
raise DbtInternalError(f"expected dict, got {other_value}")

Check warning on line 237 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L237

Added line #L237 was not covered by tests
new_dict = {}
for key in self_value.keys():
new_dict[key] = _listify(self_value[key])
Expand All @@ -256,4 +256,4 @@
return new_dict

else:
raise DbtInternalError(f"Got an invalid merge_behavior: {merge_behavior}")

Check warning on line 259 in core/dbt/common/contracts/config/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/common/contracts/config/base.py#L259

Added line #L259 was not covered by tests
2 changes: 1 addition & 1 deletion core/dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from dbt.common.invocation import get_invocation_id
from dbt.flags import get_flags
from dbt.adapters.factory import get_adapter
from dbt.clients.adapter import get_adapter
from dbt.clients import jinja
from dbt.clients.system import make_directory
from dbt.context.providers import generate_runtime_model_context
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/config/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def _credentials_from_profile(
profile: Dict[str, Any], profile_name: str, target_name: str
) -> Credentials:
# avoid an import cycle
from dbt.adapters.factory import load_plugin
from dbt.clients.adapter import load_plugin

# credentials carry their 'type' in their actual type, not their
# attributes. We do want this in order to pick our Credentials class.
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/config/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Type,
)

from dbt.adapters.factory import get_include_paths, get_relation_class_by_name
from dbt.clients.adapter import get_include_paths, get_relation_class_by_name
from dbt.adapters.contracts.connection import AdapterRequiredConfig, Credentials, HasCredentials
from dbt.adapters.contracts.relation import ComponentName
from dbt.flags import get_flags
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/context/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _build_namespace(self):

def _get_namespace_builder(self) -> MacroNamespaceBuilder:
# avoid an import loop
from dbt.adapters.factory import get_adapter_package_names
from dbt.clients.adapter import get_adapter_package_names

internal_packages: List[str] = get_adapter_package_names(self.config.credentials.type)
return MacroNamespaceBuilder(
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from typing_extensions import Protocol

from dbt.adapters.base.column import Column
from dbt.clients.adapter import get_adapter, get_adapter_package_names, get_adapter_type_names
from dbt.common.clients.jinja import MacroProtocol
from dbt.adapters.factory import get_adapter, get_adapter_package_names, get_adapter_type_names
from dbt.common.clients import agate_helper
from dbt.clients.jinja import get_rendered, MacroGenerator, MacroStack
from dbt.config import RuntimeConfig, Project
Expand Down
Loading
Loading