From b47489fc1da57325441f75c73024fb8cbd3bcddc Mon Sep 17 00:00:00 2001 From: Themis Valtinos <73662635+themisvaltinos@users.noreply.github.com> Date: Sat, 5 Apr 2025 11:51:55 +0300 Subject: [PATCH 1/2] Fix: Issue with dbt init command requiring a dialect --- docs/integrations/dbt.md | 2 +- sqlmesh/cli/example_project.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/integrations/dbt.md b/docs/integrations/dbt.md index 88ddafebff..bd1fa9a7f1 100644 --- a/docs/integrations/dbt.md +++ b/docs/integrations/dbt.md @@ -41,7 +41,7 @@ Learn more about [SQLMesh installation and extras here](../installation.md#insta Prepare an existing dbt project to be run by SQLMesh by executing the `sqlmesh init` command *within the dbt project root directory* and with the `dbt` template option: ```bash -$ sqlmesh init -t dbt # e.g. sqlmesh init -t dbt snowflake +$ sqlmesh init -t dbt ``` SQLMesh will use the data warehouse connection target in your dbt project `profiles.yml` file. The target can be changed at any time. diff --git a/sqlmesh/cli/example_project.py b/sqlmesh/cli/example_project.py index 435a258278..7af8c5d0a6 100644 --- a/sqlmesh/cli/example_project.py +++ b/sqlmesh/cli/example_project.py @@ -28,7 +28,13 @@ def _gen_config( start: t.Optional[str], template: ProjectTemplate, ) -> str: - if not settings: + connection_settings = ( + settings + or """ type: duckdb + database: db.db""" + ) + + if not settings and template != ProjectTemplate.DBT: doc_link = "https://sqlmesh.readthedocs.io/en/stable/integrations/engines{engine_link}" engine_link = "" @@ -69,8 +75,6 @@ def _gen_config( " # https://sqlmesh.readthedocs.io/en/stable/reference/configuration/#connections\n" f" # {doc_link.format(engine_link=engine_link)}\n{connection_settings}" ) - else: - connection_settings = settings default_configs = { ProjectTemplate.DEFAULT: f"""gateways: From 1394793ea3a36ef14748b632986e848a32fbced5 Mon Sep 17 00:00:00 2001 From: Themis Valtinos <73662635+themisvaltinos@users.noreply.github.com> Date: Sat, 5 Apr 2025 12:17:42 +0300 Subject: [PATCH 2/2] Add unit test --- tests/cli/test_cli.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.py index 6ab850c91c..d4d13fe789 100644 --- a/tests/cli/test_cli.py +++ b/tests/cli/test_cli.py @@ -1614,3 +1614,24 @@ def test_state_import_local(runner: CliRunner, tmp_path: Path) -> None: assert result.exit_code == 1 assert "State file is marked as not importable" in result.output assert "Aborting" in result.output + + +def test_dbt_init(tmp_path): + # The dbt init project doesn't require a dialect + init_example_project(tmp_path, dialect=None, template=ProjectTemplate.DBT) + + config_path = tmp_path / "config.py" + assert config_path.exists() + + with open(config_path) as file: + config = file.read() + + assert ( + config + == """from pathlib import Path + +from sqlmesh.dbt.loader import sqlmesh_config + +config = sqlmesh_config(Path(__file__).parent) +""" + )