Skip to content

Commit cf2ead1

Browse files
feat: [SNOW-1890085] add dbt execute command
1 parent 3634d4b commit cf2ead1

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import logging
1818

19+
import typer
1920
from snowflake.cli._plugins.dbt.manager import DBTManager
2021
from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
2122
from snowflake.cli.api.output.types import CommandResult, QueryResult
@@ -39,3 +40,28 @@ def list_dbts(
3940
List all dbt projects on Snowflake.
4041
"""
4142
return QueryResult(DBTManager().list())
43+
44+
45+
@app.command(
46+
"execute",
47+
requires_connection=True,
48+
context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
49+
)
50+
def execute(
51+
ctx: typer.Context,
52+
dbt_command: str = typer.Argument(
53+
help="dbt command to execute, i. e. run, compile, seed...",
54+
),
55+
name: str = typer.Option(
56+
default=...,
57+
help="Name of the dbt object to execute command on.",
58+
),
59+
**options,
60+
) -> CommandResult:
61+
"""
62+
Execute command on dbt in Snowflake project.
63+
"""
64+
# ctx.args are parameters that were not captured as known cli params (those are in **options).
65+
# as a consequence, we don't support passing params known to snowflake cli further to dbt
66+
dbt_cli_args = ctx.args
67+
return QueryResult(DBTManager().execute(dbt_command, name, *dbt_cli_args))

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,11 @@
2020

2121
class DBTManager(SqlExecutionMixin):
2222
def list(self) -> SnowflakeCursor: # noqa: A003
23-
query = f"show dbt"
23+
query = "SHOW DBT"
24+
return self.execute_query(query)
25+
26+
def execute(self, dbt_command: str, name: str, *dbt_cli_args):
27+
query = f"EXECUTE DBT {name} {dbt_command}"
28+
if dbt_cli_args:
29+
query += " " + " ".join([arg for arg in dbt_cli_args])
2430
return self.execute_query(query)

tests/dbt/test_dbt_commands.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,52 @@ def mock_connect(mock_ctx):
2626
yield _fixture
2727

2828

29-
def test_list_dbt_objects(mock_connect, runner):
29+
def test_dbt_list(mock_connect, runner):
3030

3131
result = runner.invoke(["dbt", "list"])
3232

3333
assert result.exit_code == 0, result.output
34-
assert mock_connect.mocked_ctx.get_query() == "show dbt"
34+
assert mock_connect.mocked_ctx.get_query() == "SHOW DBT"
35+
36+
37+
@pytest.mark.parametrize(
38+
"args,expected_query",
39+
[
40+
(
41+
["dbt", "execute", "compile", "--name=pipeline_name"],
42+
"EXECUTE DBT pipeline_name compile",
43+
),
44+
(
45+
[
46+
"dbt",
47+
"execute",
48+
"compile",
49+
"--name=pipeline_name",
50+
"-f",
51+
"--select @source:snowplow,tag:nightly models/export",
52+
],
53+
"EXECUTE DBT pipeline_name compile -f --select @source:snowplow,tag:nightly models/export",
54+
),
55+
(
56+
["dbt", "execute", "compile", "--name=pipeline_name", "--vars '{foo:bar}'"],
57+
"EXECUTE DBT pipeline_name compile --vars '{foo:bar}'",
58+
),
59+
(
60+
[
61+
"dbt",
62+
"execute",
63+
"compile",
64+
"--name=pipeline_name",
65+
"--format=JSON",
66+
"--debug",
67+
],
68+
"EXECUTE DBT pipeline_name compile",
69+
),
70+
],
71+
)
72+
def test_dbt_execute(mock_connect, runner, args, expected_query):
73+
74+
result = runner.invoke(args)
75+
76+
assert result.exit_code == 0, result.output
77+
assert mock_connect.mocked_ctx.get_query() == expected_query

0 commit comments

Comments
 (0)