Skip to content

Commit 899af66

Browse files
authored
Feat: allow setting register_comments for dbt connection (#2212)
* Allow setting register_comments parameters for dbt config * Add docs * remove unnecessary kwargs pop
1 parent 35d430c commit 899af66

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

docs/integrations/dbt.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ Models **require** a start date for backfilling data through use of the `start`
2222
> +start: Jan 1 2000
2323
```
2424

25+
### Configuration
2526

26-
### Runtime vars
27+
SQLMesh determines a project's configuration settings from its dbt configuration files.
28+
29+
This section describes using runtime variables to create multiple configurations and how to disable SQLMesh's automatic model description and comment registration.
30+
31+
#### Runtime vars
2732

2833
dbt supports passing variable values at runtime with its [CLI `vars` option](https://docs.getdbt.com/docs/build/project-variables#defining-variables-on-the-command-line).
2934

@@ -70,6 +75,21 @@ sqlmesh --config marketing_config plan
7075

7176
Note that the `--config` option is specified between the word `sqlmesh` and the command being executed (e.g., `plan`, `run`).
7277

78+
#### Registering comments
79+
80+
SQLMesh automatically registers model descriptions and column comments with the target SQL engine, as described in the [Models Overview documentation](../concepts/models/overview#model-description-and-comments). Comment registration is on by default for all engines that support it (but off by default for Snowflake).
81+
82+
dbt offers similar comment registration functionality via its [`persist_docs` model configuration parameter](https://docs.getdbt.com/reference/resource-configs/persist_docs), specified by model. SQLMesh comment registration is configured at the project level, so it does not use dbt's model-specific `persist_docs` configuration.
83+
84+
SQLMesh's project-level comment registration defaults are overridden with the `sqlmesh_config()` `register_comments` argument. For example, this configuration turns comment registration off:
85+
86+
```python
87+
config = sqlmesh_config(
88+
Path(__file__).parent,
89+
register_comments=False,
90+
)
91+
```
92+
7393
### Running SQLMesh
7494

7595
Run SQLMesh as with a SQLMesh project, generating and applying [plans](../concepts/overview.md#make-a-plan), running [tests](../concepts/overview.md#tests) or [audits](../concepts/overview.md#audits), and executing models with a [scheduler](../guides/scheduling.md) if desired.

sqlmesh/dbt/loader.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def sqlmesh_config(
3636
state_connection: t.Optional[ConnectionConfig] = None,
3737
dbt_target_name: t.Optional[str] = None,
3838
variables: t.Optional[t.Dict[str, t.Any]] = None,
39+
register_comments: t.Optional[bool] = None,
3940
**kwargs: t.Any,
4041
) -> Config:
4142
project_root = project_root or Path()
@@ -48,9 +49,13 @@ def sqlmesh_config(
4849
if variables is not None:
4950
loader_kwargs["variables"] = variables
5051

52+
target_to_sqlmesh_args = {}
53+
if register_comments is not None:
54+
target_to_sqlmesh_args["register_comments"] = register_comments
55+
5156
return Config(
5257
default_gateway=profile.target_name,
53-
gateways={profile.target_name: GatewayConfig(connection=profile.target.to_sqlmesh(), state_connection=state_connection)}, # type: ignore
58+
gateways={profile.target_name: GatewayConfig(connection=profile.target.to_sqlmesh(**target_to_sqlmesh_args), state_connection=state_connection)}, # type: ignore
5459
loader=DbtLoader,
5560
loader_kwargs=loader_kwargs,
5661
model_defaults=model_defaults,

sqlmesh/dbt/target.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def default_incremental_strategy(self, kind: IncrementalKind) -> str:
112112
"""The default incremental strategy for the db"""
113113
raise NotImplementedError
114114

115-
def to_sqlmesh(self) -> ConnectionConfig:
115+
def to_sqlmesh(self, **kwargs: t.Any) -> ConnectionConfig:
116116
"""Converts target config to SQLMesh connection config"""
117117
raise NotImplementedError
118118

@@ -187,8 +187,7 @@ def relation_class(cls) -> t.Type[BaseRelation]:
187187

188188
return DuckDBRelation
189189

190-
def to_sqlmesh(self) -> ConnectionConfig:
191-
kwargs: t.Dict[str, t.Any] = {}
190+
def to_sqlmesh(self, **kwargs: t.Any) -> ConnectionConfig:
192191
if self.extensions is not None:
193192
kwargs["extensions"] = self.extensions
194193
if self.settings is not None:
@@ -277,7 +276,7 @@ def column_class(cls) -> t.Type[Column]:
277276

278277
return SnowflakeColumn
279278

280-
def to_sqlmesh(self) -> ConnectionConfig:
279+
def to_sqlmesh(self, **kwargs: t.Any) -> ConnectionConfig:
281280
return SnowflakeConnectionConfig(
282281
user=self.user,
283282
password=self.password,
@@ -291,6 +290,7 @@ def to_sqlmesh(self) -> ConnectionConfig:
291290
private_key=self.private_key,
292291
private_key_path=self.private_key_path,
293292
private_key_passphrase=self.private_key_passphrase,
293+
**kwargs,
294294
)
295295

296296
@classproperty
@@ -347,7 +347,7 @@ def _validate_port(cls, v: t.Union[int, str]) -> int:
347347
def default_incremental_strategy(self, kind: IncrementalKind) -> str:
348348
return "delete+insert" if kind is IncrementalByUniqueKeyKind else "append"
349349

350-
def to_sqlmesh(self) -> ConnectionConfig:
350+
def to_sqlmesh(self, **kwargs: t.Any) -> ConnectionConfig:
351351
return PostgresConnectionConfig(
352352
host=self.host,
353353
user=self.user,
@@ -359,6 +359,7 @@ def to_sqlmesh(self) -> ConnectionConfig:
359359
connect_timeout=self.connect_timeout,
360360
role=self.role,
361361
sslmode=self.sslmode,
362+
**kwargs,
362363
)
363364

364365

@@ -419,7 +420,7 @@ def column_class(cls) -> t.Type[Column]:
419420
else:
420421
return super(RedshiftConfig, cls).column_class
421422

422-
def to_sqlmesh(self) -> ConnectionConfig:
423+
def to_sqlmesh(self, **kwargs: t.Any) -> ConnectionConfig:
423424
return RedshiftConnectionConfig(
424425
user=self.user,
425426
password=self.password,
@@ -429,6 +430,7 @@ def to_sqlmesh(self) -> ConnectionConfig:
429430
sslmode=self.sslmode,
430431
timeout=self.connect_timeout,
431432
concurrent_tasks=self.threads,
433+
**kwargs,
432434
)
433435

434436

@@ -465,13 +467,14 @@ def column_class(cls) -> t.Type[Column]:
465467

466468
return DatabricksColumn
467469

468-
def to_sqlmesh(self) -> ConnectionConfig:
470+
def to_sqlmesh(self, **kwargs: t.Any) -> ConnectionConfig:
469471
return DatabricksConnectionConfig(
470472
server_hostname=self.host,
471473
http_path=self.http_path,
472474
access_token=self.token,
473475
concurrent_tasks=self.threads,
474476
catalog=self.database,
477+
**kwargs,
475478
)
476479

477480

@@ -548,7 +551,7 @@ def column_class(cls) -> t.Type[Column]:
548551

549552
return BigQueryColumn
550553

551-
def to_sqlmesh(self) -> ConnectionConfig:
554+
def to_sqlmesh(self, **kwargs: t.Any) -> ConnectionConfig:
552555
job_retries = self.job_retries if self.job_retries is not None else self.retries
553556
job_execution_timeout_seconds = (
554557
self.job_execution_timeout_seconds
@@ -574,6 +577,7 @@ def to_sqlmesh(self) -> ConnectionConfig:
574577
job_retry_deadline_seconds=self.job_retry_deadline_seconds,
575578
priority=self.priority,
576579
maximum_bytes_billed=self.maximum_bytes_billed,
580+
**kwargs,
577581
)
578582

579583

@@ -664,7 +668,7 @@ def column_class(cls) -> t.Type[Column]:
664668
def dialect(self) -> str:
665669
return "tsql"
666670

667-
def to_sqlmesh(self) -> ConnectionConfig:
671+
def to_sqlmesh(self, **kwargs: t.Any) -> ConnectionConfig:
668672
return MSSQLConnectionConfig(
669673
host=self.host,
670674
user=self.user,
@@ -674,6 +678,7 @@ def to_sqlmesh(self) -> ConnectionConfig:
674678
timeout=self.query_timeout,
675679
login_timeout=self.login_timeout,
676680
concurrent_tasks=self.threads,
681+
**kwargs,
677682
)
678683

679684

tests/dbt/test_config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,16 @@ def test_sqlserver_config():
670670
)
671671

672672

673+
def test_connection_args(tmp_path):
674+
dbt_project_dir = "tests/fixtures/dbt/sushi_test"
675+
676+
config = sqlmesh_config(dbt_project_dir)
677+
assert config.gateways["in_memory"].connection.register_comments
678+
679+
config = sqlmesh_config(dbt_project_dir, register_comments=False)
680+
assert not config.gateways["in_memory"].connection.register_comments
681+
682+
673683
@pytest.mark.cicdonly
674684
def test_db_type_to_relation_class():
675685
from dbt.adapters.bigquery.relation import BigQueryRelation

0 commit comments

Comments
 (0)