Skip to content

Commit

Permalink
feat: add --omit-entity-name-quotes option for dbml (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
datnguye committed Jun 22, 2024
1 parent f25c7bb commit 45507e7
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 55 deletions.
8 changes: 5 additions & 3 deletions dbterd/adapters/targets/dbml.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str:

# Build DBML content
dbml = "//Tables (based on the selection criteria)\n"
quote = "" if kwargs.get("omit_entity_name_quotes") else '"'
for table in tables:
dbml += f"//--configured at schema: {table.database}.{table.schema}\n"
dbml += 'Table "{table}" {{\n{columns}\n\n Note: {table_note}\n}}\n'.format(
dbml += "Table {quote}{table}{quote} {{\n{columns}\n\n Note: {table_note}\n}}\n".format(
quote=quote,
table=table.name,
columns="\n".join(
[
Expand All @@ -61,8 +63,8 @@ def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str:

dbml += "//Refs (based on the DBT Relationship Tests)\n"
for rel in relationships:
key_from = f'"{rel.table_map[1]}"."{rel.column_map[1]}"'
key_to = f'"{rel.table_map[0]}"."{rel.column_map[0]}"'
key_from = f'{quote}{rel.table_map[1]}{quote}."{rel.column_map[1]}"'
key_to = f'{quote}{rel.table_map[0]}{quote}."{rel.column_map[0]}"'
dbml += f"Ref: {key_from} {get_rel_symbol(rel.type)} {key_to}\n"
return dbml

Expand Down
7 changes: 7 additions & 0 deletions dbterd/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def common_params(func):
show_default=True,
type=click.STRING,
)
@click.option(
"--omit-entity-name-quotes",
help="Flag to omit double quotes in the entity name. Currently only dbml is supported",
is_flag=True,
default=False,
show_default=True,
)
@click.option(
"--output",
"-o",
Expand Down
Binary file added docs/assets/images/dbdocs-enf-with-quotes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/images/dbdocs-enf-without-quotes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions docs/nav/guide/cli-references.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,33 @@ Currently, it supports the following keys in the format:
dbterd run --entity-name-format table # with table name only
```

### dbterd run --omit-entity-name-quotes

Flag to omit the double quotes in the generated entity name. Currently only `dbml` is supported.

> Default to `False`
Enabled it to allow `dbdocs` to recognize the schemas and display them as grouping:

- With quotes:

![dbdocs-enf-with-quotes](../../assets/images/dbdocs-enf-with-quotes.png)

- Without quotes:

![dbdocs-enf-without-quotes](../../assets/images/dbdocs-enf-without-quotes.png)

> ⚠️ As of 2024 June: DBML doesn't support nested schema in the entity name which means 'database.schema.table' won't be allowed, but 'schema.table' does!
**Examples:**
=== "CLI"

```bash
dbterd run --entity-name-format resource.package.model --omit-entity-name-quotes # ❌
dbterd run --entity-name-format database.schema.table --omit-entity-name-quotes # ❌
dbterd run --entity-name-format schema.table --omit-entity-name-quotes # ✅
```

### dbterd run --dbt-cloud

Decide to download artifact files from dbt Cloud Job Run instead of compiling locally.
Expand Down
110 changes: 60 additions & 50 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 45507e7

Please sign in to comment.