Skip to content

Commit

Permalink
Enable multi project references and settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailsimsek committed Dec 16, 2024
1 parent 2541657 commit 2686a19
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions opendbt/dbt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def patch_dbt():
dbt.adapters.factory.FACTORY = OpenDbtAdapterContainer()
from opendbt.dbt.v17.task.run import ModelRunner
dbt.task.run.ModelRunner = ModelRunner
from opendbt.dbt.v17.config.runtime import OpenDbtRuntimeConfig
dbt.config.RuntimeConfig = OpenDbtRuntimeConfig
elif Version("1.8.0") <= dbt_version < Version("1.10.0"):
from opendbt.dbt.v18.task.docs.generate import OpenDbtGenerateTask
dbt.task.docs.generate.GenerateTask = OpenDbtGenerateTask
Expand All @@ -33,3 +31,6 @@ def patch_dbt():
dbt.cli.main.sqlfluff = opendbt.dbt.shared.cli.main.sqlfluff
dbt.cli.main.sqlfluff_lint = opendbt.dbt.shared.cli.main.sqlfluff_lint
dbt.cli.main.sqlfluff_fix = opendbt.dbt.shared.cli.main.sqlfluff_fix
#
from opendbt.dbt.shared.config.runtime import OpenDbtRuntimeConfig
dbt.config.RuntimeConfig = OpenDbtRuntimeConfig
Empty file.
53 changes: 53 additions & 0 deletions opendbt/dbt/shared/config/runtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Mapping

from dbt.config import RuntimeConfig
from dbt.config.project import load_yml_dict
from dbt.constants import DEPENDENCIES_FILE_NAME
from dbt.exceptions import (
DbtProjectError, NonUniquePackageNameError,
)
from typing_extensions import override


# pylint: disable=too-many-ancestors
@dataclass
class OpenDbtRuntimeConfig(RuntimeConfig):
def load_dependence_projects(self):
dependencies_yml_dict = load_yml_dict(f"{self.project_root}/{DEPENDENCIES_FILE_NAME}")

if "projects" not in dependencies_yml_dict:
return

projects = dependencies_yml_dict["projects"]
project_root_parent = Path(self.project_root).parent
for project in projects:
path = project_root_parent.joinpath(project['name'])
try:
project = self.new_project(str(path.as_posix()))
except DbtProjectError as e:
raise DbtProjectError(
f"Failed to read depending project: {e} \n project path:{path.as_posix()}",
result_type="invalid_project",
path=path,
) from e

yield project.project_name, project

@override
def load_dependencies(self, base_only=False) -> Mapping[str, "RuntimeConfig"]:
# if self.dependencies is None:

if self.dependencies is None:
# this sets self.dependencies variable!
self.dependencies = super().load_dependencies()

# additionally load `projects` defined in `dependencies.yml`
for project_name, project in self.load_dependence_projects():
print(f"WARN: loading:{project_name} , project:{project}")
if project_name in self.dependencies:
raise NonUniquePackageNameError(project_name)
self.dependencies[project_name] = project

return self.dependencies

0 comments on commit 2686a19

Please sign in to comment.