Skip to content

Commit

Permalink
remove usage of dbt.config.PartialProject from dbt/adapters (#8909)
Browse files Browse the repository at this point in the history
* remove usage of dbt.config.PartialProject from dbt/adapters

* add changie

---------

Co-authored-by: Colin <[email protected]>
  • Loading branch information
MichelleArk and colin-rogers-dbt authored Oct 26, 2023
1 parent 1a82548 commit b9a603e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 58 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20231026-123913.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: remove usage of dbt.config.PartialProject from dbt/adapters
time: 2023-10-26T12:39:13.904116-07:00
custom:
Author: MichelleArk
Issue: "8928"
15 changes: 3 additions & 12 deletions core/dbt/adapters/base/plugin.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
from typing import List, Optional, Type
from pathlib import Path

from dbt.adapters.base import Credentials
from dbt.exceptions import CompilationError
from dbt.adapters.protocol import AdapterProtocol


def project_name_from_path(include_path: str) -> str:
# avoid an import cycle
from dbt.config.project import PartialProject

partial = PartialProject.from_project_root(include_path)
if partial.project_name is None:
raise CompilationError(f"Invalid project at {include_path}: name not set!")
return partial.project_name


class AdapterPlugin:
"""Defines the basic requirements for a dbt adapter plugin.
Expand All @@ -29,12 +19,13 @@ def __init__(
credentials: Type[Credentials],
include_path: str,
dependencies: Optional[List[str]] = None,
project_name: Optional[str] = None,
) -> None:

self.adapter: Type[AdapterProtocol] = adapter
self.credentials: Type[Credentials] = credentials
self.include_path: str = include_path
self.project_name: str = project_name_from_path(include_path)
self.project_name: str = project_name or f"dbt_{Path(include_path).name}"
self.dependencies: List[str]
if dependencies is None:
self.dependencies = []
Expand Down
53 changes: 26 additions & 27 deletions tests/unit/test_adapter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,32 @@

class TestGetPackageNames(unittest.TestCase):
def setUp(self):
with mock.patch("dbt.adapters.base.plugin.project_name_from_path") as get_name:
get_name.return_value = "root"
self.root_plugin = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/root/plugin",
dependencies=["childa", "childb"],
)
get_name.return_value = "pkg_childa"
self.childa = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/childa",
)
get_name.return_value = "pkg_childb"
self.childb = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/childb",
dependencies=["childc"],
)
get_name.return_value = "pkg_childc"
self.childc = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/childc",
)
self.root_plugin = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/root/plugin",
dependencies=["childa", "childb"],
project_name="root",
)
self.childa = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/childa",
project_name="pkg_childa",
)
self.childb = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/childb",
dependencies=["childc"],
project_name="pkg_childb",
)
self.childc = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/childc",
project_name="pkg_childc",
)

self._mock_modules = {
"root": self.root_plugin,
Expand Down
37 changes: 18 additions & 19 deletions tests/unit/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1246,25 +1246,24 @@ def test_find_generate_macros_by_name(macros, expectations):

def _materialization_parameter_sets():
# inject the plugins used for materialization parameter tests
with mock.patch("dbt.adapters.base.plugin.project_name_from_path") as get_name:
get_name.return_value = "foo"
FooPlugin = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/root/plugin",
)
FooPlugin.adapter.type.return_value = "foo"
inject_plugin(FooPlugin)

get_name.return_value = "bar"
BarPlugin = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/root/plugin",
dependencies=["foo"],
)
BarPlugin.adapter.type.return_value = "bar"
inject_plugin(BarPlugin)
FooPlugin = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/root/plugin",
project_name="foo",
)
FooPlugin.adapter.type.return_value = "foo"
inject_plugin(FooPlugin)

BarPlugin = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path="/path/to/root/plugin",
dependencies=["foo"],
project_name="bar",
)
BarPlugin.adapter.type.return_value = "bar"
inject_plugin(BarPlugin)

sets = [
FindMaterializationSpec(macros=[], adapter_type="foo", expected=None),
Expand Down

0 comments on commit b9a603e

Please sign in to comment.