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

fix: run metadata need handle entity name format probably #93

Merged
merged 2 commits into from
Feb 23, 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
78 changes: 54 additions & 24 deletions dbterd/adapters/algos/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,30 @@ def get_table_name(format: str, **kwargs) -> str:
return ".".join([kwargs.get(x.lower()) or "KEYNOTFOUND" for x in format.split(".")])


def get_test_nodes_by_rule_name(manifest: Manifest, rule_name: str) -> List:
"""Get manifest nodes given the algo rule name.

Default algo rule name is `relationship`,
see `get_algo_rule` function for more details.

Args:
rule_name (str): Rule name
manifest (Manifest): Manifest data

Returns:
List: List of manifest nodes
"""
return [
x
for x in manifest.nodes
if (
x.startswith("test")
and rule_name in x.lower()
and manifest.nodes[x].meta.get(TEST_META_IGNORE_IN_ERD, "0") == "0"
)
]


def get_relationships_from_metadata(data=[], **kwargs) -> List[Ref]:
"""Extract relationships from Metadata result list on test relationship

Expand Down Expand Up @@ -415,30 +439,6 @@ def get_relationships_from_metadata(data=[], **kwargs) -> List[Ref]:
return get_unique_refs(refs=refs)


def get_test_nodes_by_rule_name(manifest: Manifest, rule_name: str) -> List:
"""Get manifest nodes given the algo rule name.

Default algo rule name is `relationship`,
see `get_algo_rule` function for more details.

Args:
rule_name (str): Rule name
manifest (Manifest): Manifest data

Returns:
List: List of manifest nodes
"""
return [
x
for x in manifest.nodes
if (
x.startswith("test")
and rule_name in x.lower()
and manifest.nodes[x].meta.get(TEST_META_IGNORE_IN_ERD, "0") == "0"
)
]


def get_relationships(manifest: Manifest, **kwargs) -> List[Ref]:
"""Extract relationships from dbt artifacts based on test relationship

Expand Down Expand Up @@ -488,6 +488,36 @@ def get_relationships(manifest: Manifest, **kwargs) -> List[Ref]:
return get_unique_refs(refs=refs)


def make_up_relationships(
relationships: List[Ref] = [], tables: List[Table] = []
) -> List[Ref]:
"""Filter Refs given by the parsed Tables & applied the entity name format

Args:
relationships (List[Ref], optional): Parsed relationships. Defaults to [].
tables (List[Table], optional): Parsed tables. Defaults to [].

Returns:
List[Ref]: Cooked relationships
"""
node_names = [x.node_name for x in tables]
relationships = [
Ref(
name=x.name,
table_map=[
[t for t in tables if t.node_name == x.table_map[0]][0].name,
[t for t in tables if t.node_name == x.table_map[1]][0].name,
],
column_map=x.column_map,
type=x.type,
)
for x in relationships
if x.table_map[0] in node_names and x.table_map[1] in node_names
]

return relationships


def get_unique_refs(refs: list[Ref] = []) -> list[Ref]:
"""Remove duplicates in the Relationship list

Expand Down
26 changes: 6 additions & 20 deletions dbterd/adapters/algos/test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ def parse_metadata(data, **kwargs) -> Tuple[List[Table], List[Ref]]:

# Parse Ref
relationships = base.get_relationships_from_metadata(data=data, **kwargs)
node_names = [x.node_name for x in tables]
relationships = [
x
for x in relationships
if x.table_map[0] in node_names and x.table_map[1] in node_names
]
relationships = base.make_up_relationships(
relationships=relationships, tables=tables
)

logger.info(
f"Collected {len(tables)} table(s) and {len(relationships)} relationship(s)"
Expand Down Expand Up @@ -85,20 +82,9 @@ def parse(

# Parse Ref
relationships = base.get_relationships(manifest=manifest, **kwargs)
node_names = [x.node_name for x in tables]
relationships = [
Ref(
name=x.name,
table_map=[
[t for t in tables if t.node_name == x.table_map[0]][0].name,
[t for t in tables if t.node_name == x.table_map[1]][0].name,
],
column_map=x.column_map,
type=x.type,
)
for x in relationships
if x.table_map[0] in node_names and x.table_map[1] in node_names
]
relationships = base.make_up_relationships(
relationships=relationships, tables=tables
)

# Fullfill columns in Tables (due to `select *`)
tables = base.enrich_tables_from_relationships(
Expand Down
2 changes: 1 addition & 1 deletion dbterd/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def deafult_algo() -> str:


def default_resource_types() -> List[str]:
return ["model", "source"]
return ["model"]


def default_entity_name_format() -> str:
Expand Down
Loading