Skip to content

Commit 8107267

Browse files
feat: [SNOW-1890085] dbt deploy: add execute_in_warehouse flag
1 parent 1b7f775 commit 8107267

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/snowflake/cli/_plugins/dbt/commands.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def deploy_dbt(
7373
dbt_adapter_version: str = typer.Option(
7474
help="dbt-snowflake adapter version to be used",
7575
),
76+
execute_in_warehouse: Optional[str] = typer.Option(
77+
None, help="Warehouse to use when running `dbt execute` commands"
78+
),
7679
**options,
7780
) -> CommandResult:
7881
"""
@@ -85,7 +88,12 @@ def deploy_dbt(
8588
path = Path(source)
8689
return QueryResult(
8790
DBTManager().deploy(
88-
path.resolve(), name, dbt_version, dbt_adapter_version, force=force
91+
path.resolve(),
92+
name,
93+
dbt_version,
94+
dbt_adapter_version,
95+
execute_in_warehouse,
96+
force=force,
8997
)
9098
)
9199

src/snowflake/cli/_plugins/dbt/manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def deploy(
5050
name: FQN,
5151
dbt_version: Optional[str],
5252
dbt_adapter_version: str,
53+
execute_in_warehouse: Optional[str],
5354
force: bool,
5455
) -> SnowflakeCursor:
5556
dbt_project_path = path.joinpath("dbt_project.yml")
@@ -81,6 +82,8 @@ def deploy(
8182
query = f"""{'CREATE OR REPLACE' if force is True else 'CREATE'} DBT PROJECT {name}
8283
FROM {stage_name} MAIN_FILE='{staged_dbt_project_path}'
8384
DBT_VERSION='{dbt_version}' DBT_ADAPTER_VERSION='{dbt_adapter_version}'"""
85+
if execute_in_warehouse:
86+
query += f" WAREHOUSE='{execute_in_warehouse}'"
8487
return self.execute_query(query)
8588

8689
def execute(self, dbt_command: str, name: str, *dbt_cli_args):

tests/dbt/test_dbt_commands.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616

17+
from textwrap import dedent
1718
from unittest import mock
1819

1920
import pytest
@@ -128,6 +129,30 @@ def test_force_flag_uses_create_or_replace(
128129
"CREATE OR REPLACE DBT PROJECT"
129130
)
130131

132+
@mock.patch("snowflake.cli._plugins.dbt.manager.StageManager.put_recursive")
133+
@mock.patch("snowflake.cli._plugins.dbt.manager.StageManager.create")
134+
def test_execute_in_warehouse(
135+
self, _mock_create, _mock_put_recursive, mock_connect, runner, dbt_project_path
136+
):
137+
138+
result = runner.invoke(
139+
[
140+
"dbt",
141+
"deploy",
142+
"TEST_PIPELINE",
143+
f"--source={dbt_project_path}",
144+
"--dbt-adapter-version=3.4.5",
145+
"--execute-in-warehouse=XL",
146+
]
147+
)
148+
149+
assert result.exit_code == 0, result.output
150+
assert mock_connect.mocked_ctx.get_query() == dedent(
151+
"""CREATE DBT PROJECT TEST_PIPELINE
152+
FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage MAIN_FILE='@MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage/dbt_project.yml'
153+
DBT_VERSION='1.2.3' DBT_ADAPTER_VERSION='3.4.5' WAREHOUSE='XL'"""
154+
)
155+
131156
def test_raises_when_dbt_project_is_not_available(
132157
self, dbt_project_path, mock_connect, runner
133158
):

0 commit comments

Comments
 (0)