Skip to content

Commit

Permalink
Merge pull request #54 from nicholasyager/fix/cross_project_nodes
Browse files Browse the repository at this point in the history
Fix: Update name resolution for non-model nodes to preserve cross project nodes
  • Loading branch information
nicholasyager authored May 9, 2024
2 parents 3368db3 + 2432682 commit 5f7d1bf
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
34 changes: 30 additions & 4 deletions dbt_loom/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
import os
import re
from pathlib import Path
Expand All @@ -18,6 +19,31 @@
import importlib.metadata


@dataclass
class LoomModelNodeArgs(ModelNodeArgs):
"""A dbt-loom extension of ModelNodeArgs to preserve resource types across lineages."""

resource_type: NodeType = NodeType.Model

def __init__(self, **kwargs):
super().__init__(
**{
key: value
for key, value in kwargs.items()
if key not in ("resource_type")
}
)
self.resource_type = kwargs["resource_type"]

@property
def unique_id(self) -> str:
unique_id = f"{self.resource_type}.{self.package_name}.{self.name}"
if self.version:
unique_id = f"{unique_id}.v{self.version}"

return unique_id


def identify_node_subgraph(manifest) -> Dict[str, ManifestNode]:
"""
Identify all nodes that should be selected from the manifest, and return ManifestNodes.
Expand Down Expand Up @@ -50,10 +76,10 @@ def identify_node_subgraph(manifest) -> Dict[str, ManifestNode]:

def convert_model_nodes_to_model_node_args(
selected_nodes: Dict[str, ManifestNode],
) -> Dict[str, ModelNodeArgs]:
) -> Dict[str, LoomModelNodeArgs]:
"""Generate a dictionary of ModelNodeArgs based on a dictionary of ModelNodes"""
return {
unique_id: ModelNodeArgs(
unique_id: LoomModelNodeArgs(
schema=node.schema_name,
identifier=node.identifier,
**(
Expand Down Expand Up @@ -94,7 +120,7 @@ def __init__(self, project_name: str):
self._manifest_loader = ManifestLoader()

self.config: Optional[dbtLoomConfig] = self.read_config(configuration_path)
self.models: Dict[str, ModelNodeArgs] = {}
self.models: Dict[str, LoomModelNodeArgs] = {}

import dbt.contracts.graph.manifest

Expand Down Expand Up @@ -177,7 +203,7 @@ def get_nodes(self) -> PluginNodes:
Inject PluginNodes to dbt for injection into dbt's DAG.
"""
fire_event(Note(msg="dbt-loom: Injecting nodes"))
return PluginNodes(models=self.models)
return PluginNodes(models=self.models) # type: ignore


plugins = [dbtLoom]
3 changes: 3 additions & 0 deletions dbt_loom/manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from pydantic import BaseModel, Field, validator

from dbt.node_types import NodeType

from dbt_loom.clients.az_blob import AzureClient, AzureReferenceConfig
from dbt_loom.clients.dbt_cloud import DbtCloud, DbtCloudReferenceConfig
from dbt_loom.clients.gcs import GCSClient, GCSReferenceConfig
Expand All @@ -27,6 +29,7 @@ class ManifestNode(BaseModel):
"""A basic ManifestNode that can be referenced across projects."""

name: str
resource_type: NodeType
package_name: str
schema_name: str = Field(alias="schema")
database: Optional[str] = None
Expand Down
4 changes: 4 additions & 0 deletions test_projects/revenue/models/marts/orders_v2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ supplies as (
),
accounts as (
select * from {{ ref('stg_accounts') }}
),
order_items_summary as (
select
Expand Down
11 changes: 10 additions & 1 deletion test_projects/revenue/models/staging/__models.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: 2

models:

- name: stg_locations
description: List of open locations with basic cleaning and transformation applied, one row per location.
columns:
Expand Down Expand Up @@ -49,3 +48,13 @@ models:
tests:
- not_null
- unique

- name: stg_accounts
description: >
List of all accounts.
columns:
- name: name
description: The unique key of our accounts.
tests:
- not_null
- unique
1 change: 1 addition & 0 deletions test_projects/revenue/models/staging/stg_accounts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from {{ ref('accounts') }}
4 changes: 4 additions & 0 deletions test_projects/revenue/seeds/__seeds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ seeds:
# Manually add to config to support dbt-core 1.6.x. Note that you
# cannot have both in latest version of 1.7.x.
access: public

- name: accounts
config:
access: private
4 changes: 4 additions & 0 deletions test_projects/revenue/seeds/accounts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name
foo
bar
baz

0 comments on commit 5f7d1bf

Please sign in to comment.