Skip to content

Commit

Permalink
feat: add dbml table note (#66)
Browse files Browse the repository at this point in the history
* feat: add dbml table note

* test: add test case for model description [skip ci]

* chore: format fix [skip ci]
  • Loading branch information
datnguye authored Jan 4, 2024
1 parent ba62236 commit 5ba8122
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dbterd/adapters/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def load_executor(name: str):
except ModuleNotFoundError as exc:
if exc.name == "dbterd.adapters.targets." + name:
raise Exception(f"Could not find adapter target type {name}!")
raise
raise # pragma: no cover
1 change: 1 addition & 0 deletions dbterd/adapters/algos/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def get_table(node_name, manifest_node, catalog_node=None, exposures=[], **kwarg
exposures=[
x.get("exposure_name") for x in exposures if x.get("node_name") == node_name
],
description=manifest_node.description,
)

if catalog_node:
Expand Down
1 change: 1 addition & 0 deletions dbterd/adapters/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Table:
resource_type: str = "model"
exposures: Optional[List[str]] = field(default_factory=lambda: [])
node_name: str = None
description: str = ""


@dataclass
Expand Down
3 changes: 2 additions & 1 deletion dbterd/adapters/targets/dbml/dbml_test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def parse(manifest, catalog, **kwargs):
dbml = "//Tables (based on the selection criteria)\n"
for table in tables:
dbml += f"//--configured at schema: {table.database}.{table.schema}\n"
dbml += 'Table "{table}" {{\n{columns}\n}}\n'.format(
dbml += 'Table "{table}" {{\n{columns}\n\n Note: {table_note}\n}}\n'.format(
table=table.name,
columns="\n".join(
[
Expand All @@ -49,6 +49,7 @@ def parse(manifest, catalog, **kwargs):
for x in table.columns
]
),
table_note=json.dumps(table.description),
)

dbml += "//Refs (based on the DBT Relationship Tests)\n"
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ ignore-init-module-imports = true
remove-unused-variables = true
ignore-pass-statements = false

[tool.coverage.run]
omit = ["tests/*"]

[tool.poe.tasks]
git-hooks = { shell = "pre-commit install --install-hooks && pre-commit install --hook-type commit-msg" }
format = [
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/adapters/algos/test_test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ManifestNode:
database: str = ""
schema_: str = ""
depends_on: ManifestNodeDependsOn = field(default_factory=ManifestNodeDependsOn)
description: str = ""


@dataclass
Expand Down Expand Up @@ -243,6 +244,7 @@ class TestAlgoTestRelationship:
schema="--schema--",
columns=[Column(name="name1", data_type="--name1-type--")],
raw_sql="--irrelevant--",
description="",
),
Table(
name="model.dbt_resto.table_dummy_columns",
Expand All @@ -251,6 +253,7 @@ class TestAlgoTestRelationship:
schema="--schema--",
columns=[Column()],
raw_sql="--irrelevant--",
description="",
),
Table(
name="model.dbt_resto.table2",
Expand All @@ -262,6 +265,7 @@ class TestAlgoTestRelationship:
Column(name="name2"),
],
raw_sql="--irrelevant--",
description="",
),
Table(
name="source.dummy.source_table",
Expand All @@ -274,6 +278,7 @@ class TestAlgoTestRelationship:
],
raw_sql="--irrelevant--",
resource_type="source",
description="",
),
],
),
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/adapters/targets/dbml/test_dbml_test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class TestDbmlTestRelationship:
//--configured at schema: --database--.--schema--
Table "model.dbt_resto.table1" {
"name1" "--name1-type--" [note: "column name 1"]
Note:""
}
//Refs (based on the DBT Relationship Tests)
""",
Expand Down Expand Up @@ -86,15 +87,18 @@ class TestDbmlTestRelationship:
Table "model.dbt_resto.table1" {
"name1" "--name1-type--"
"name-notexist1" "unknown"
Note:""
}
//--configured at schema: --database2--.--schema2--
Table "model.dbt_resto.table2" {
"name2" "--name2-type2--"
"name-notexist2" "unknown"
Note:""
}
//--configured at schema: --database3--.--schema3--
Table "source.dbt_resto.table3" {
"name3" "--name3-type3--"
Note:""
}
//Refs (based on the DBT Relationship Tests)
Ref: "model.dbt_resto.table1"."name1" > "model.dbt_resto.table2"."name2"
Expand Down Expand Up @@ -134,6 +138,7 @@ class TestDbmlTestRelationship:
//--configured at schema: --database--.--schema--
Table "model.dbt_resto.table1" {
"name1" "--name1-type--"
Note:""
}
//Refs (based on the DBT Relationship Tests)
""",
Expand Down Expand Up @@ -184,6 +189,7 @@ class TestDbmlTestRelationship:
//--configured at schema: --database--.--schema--
Table "model.dbt_resto.table1" {
"name1" "name1-type"
Note:""
}
//Refs (based on the DBT Relationship Tests)
""",
Expand Down Expand Up @@ -223,10 +229,12 @@ class TestDbmlTestRelationship:
//--configured at schema: --database--.--schema--
Table "model.dbt_resto.table1" {
"name1" "name1-type"
Note:""
}
//--configured at schema: --database3--.--schema3--
Table "source.dbt_resto.table3" {
"name3" "name3-type3"
Note:""
}
//Refs (based on the DBT Relationship Tests)
""",
Expand All @@ -251,6 +259,7 @@ class TestDbmlTestRelationship:
//--configured at schema: --database--.--schema--
Table "model.dbt_resto.table1" {
"name1" "name1-type"
Note:""
}
//Refs (based on the DBT Relationship Tests)
""",
Expand Down Expand Up @@ -284,6 +293,33 @@ class TestDbmlTestRelationship:
//--configured at schema: --database--.--schema--
Table "model.dbt_resto.table23" {
"name23" "name23-type"
Note:""
}
//Refs (based on the DBT Relationship Tests)
""",
),
(
[
Table(
name="model.dbt_resto.table23",
node_name="model.dbt_resto.table23",
database="--database--",
schema="--schema--",
columns=[Column(name="name23", data_type="name23-type")],
raw_sql="--irrelevant--",
exposures=["dummy2", "dummy3"],
description="model.dbt_resto.table23 description",
),
],
[],
["exposure:dummy2"],
[],
["model"],
"""//Tables (based on the selection criteria)
//--configured at schema: --database--.--schema--
Table "model.dbt_resto.table23" {
"name23" "name23-type"
Note:"model.dbt_resto.table23 description"
}
//Refs (based on the DBT Relationship Tests)
""",
Expand Down

0 comments on commit 5ba8122

Please sign in to comment.