Skip to content

Commit c3cb841

Browse files
feat: [SNOW-1966187] update commands according to backend implementation
1 parent c7edd83 commit c3cb841

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def deploy_dbt(
7171
None,
7272
help="Version of dbt tool to be used. Taken from dbt_project.yml if not provided.",
7373
),
74-
dbt_adapter_version: str = typer.Option(
74+
dbt_adapter_version: Optional[str] = typer.Option(
75+
None,
7576
help="dbt-snowflake adapter version to be used",
7677
),
7778
execute_in_warehouse: Optional[str] = typer.Option(
@@ -82,7 +83,6 @@ def deploy_dbt(
8283
"""
8384
Copy dbt files and create or update dbt on Snowflake project.
8485
"""
85-
# TODO: options for DBT version?
8686
if source is None:
8787
path = Path.cwd()
8888
else:
@@ -131,7 +131,6 @@ def before_callback(
131131
def _dbt_execute(
132132
ctx: typer.Context,
133133
) -> CommandResult:
134-
# TODO: figure out how to present logs to users
135134
dbt_cli_args = ctx.args
136135
dbt_command = ctx.command.name
137136
name = ctx.parent.params["name"]

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def execute_query(self, query, **kwargs):
4141

4242
class DBTManager(StdoutExecutionMixin):
4343
def list(self) -> SnowflakeCursor: # noqa: A003
44-
query = "SHOW DBT PROJECT"
44+
query = "SHOW DBT PROJECTS"
4545
return self.execute_query(query)
4646

4747
def deploy(
@@ -78,20 +78,18 @@ def deploy(
7878
cli_console.step(f"Copied {len(results)} files")
7979

8080
with cli_console.phase("Creating DBT project"):
81-
staged_dbt_project_path = self._get_dbt_project_stage_path(stage_name)
8281
query = f"""{'CREATE OR REPLACE' if force is True else 'CREATE'} DBT PROJECT {name}
83-
FROM {stage_name} MAIN_FILE='{staged_dbt_project_path}'
84-
DBT_VERSION='{dbt_version}' DBT_ADAPTER_VERSION='{dbt_adapter_version}'"""
82+
FROM {stage_name}
83+
DBT_VERSION='{dbt_version}'"""
84+
85+
if dbt_adapter_version:
86+
query += f"\nDBT_ADAPTER_VERSION='{dbt_adapter_version}'"
8587
if execute_in_warehouse:
86-
query += f" WAREHOUSE='{execute_in_warehouse}'"
88+
query += f"\nWAREHOUSE='{execute_in_warehouse}'"
8789
return self.execute_query(query)
8890

8991
def execute(self, dbt_command: str, name: str, *dbt_cli_args):
90-
query = f"EXECUTE DBT PROJECT {name} {dbt_command}"
9192
if dbt_cli_args:
92-
query += " " + " ".join([arg for arg in dbt_cli_args])
93+
dbt_command = dbt_command + " " + " ".join([arg for arg in dbt_cli_args])
94+
query = f"EXECUTE DBT PROJECT {name} args='{dbt_command.strip()}'"
9395
return self.execute_query(query)
94-
95-
@staticmethod
96-
def _get_dbt_project_stage_path(stage_name):
97-
return "/".join([stage_name, "dbt_project.yml"])

tests/dbt/test_dbt_commands.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_dbt_list(self, mock_connect, runner):
3737
result = runner.invoke(["dbt", "list"])
3838

3939
assert result.exit_code == 0, result.output
40-
assert mock_connect.mocked_ctx.get_query() == "SHOW DBT PROJECT"
40+
assert mock_connect.mocked_ctx.get_query() == "SHOW DBT PROJECTS"
4141

4242

4343
class TestDBTDeploy:
@@ -75,8 +75,9 @@ def test_deploys_project_from_source(
7575
assert (
7676
mock_connect.mocked_ctx.get_query()
7777
== """CREATE DBT PROJECT TEST_PIPELINE
78-
FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage MAIN_FILE='@MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage/dbt_project.yml'
79-
DBT_VERSION='1.2.3' DBT_ADAPTER_VERSION='3.4.5'"""
78+
FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage
79+
DBT_VERSION='1.2.3'
80+
DBT_ADAPTER_VERSION='3.4.5'"""
8081
)
8182
stage_fqn = FQN.from_string(f"dbt_TEST_PIPELINE_stage").using_context()
8283
mock_create.assert_called_once_with(stage_fqn, temporary=True)
@@ -104,8 +105,9 @@ def test_dbt_version_from_option_has_precedence_over_file(
104105
assert (
105106
mock_connect.mocked_ctx.get_query()
106107
== """CREATE DBT PROJECT TEST_PIPELINE
107-
FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage MAIN_FILE='@MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage/dbt_project.yml'
108-
DBT_VERSION='2.3.4' DBT_ADAPTER_VERSION='3.4.5'"""
108+
FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage
109+
DBT_VERSION='2.3.4'
110+
DBT_ADAPTER_VERSION='3.4.5'"""
109111
)
110112

111113
@mock.patch("snowflake.cli._plugins.dbt.manager.StageManager.put_recursive")
@@ -149,8 +151,10 @@ def test_execute_in_warehouse(
149151
assert result.exit_code == 0, result.output
150152
assert mock_connect.mocked_ctx.get_query() == dedent(
151153
"""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+
FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage
155+
DBT_VERSION='1.2.3'
156+
DBT_ADAPTER_VERSION='3.4.5'
157+
WAREHOUSE='XL'"""
154158
)
155159

156160
def test_raises_when_dbt_project_is_not_available(
@@ -209,7 +213,7 @@ class TestDBTExecute:
209213
"pipeline_name",
210214
"test",
211215
],
212-
"EXECUTE DBT PROJECT pipeline_name test",
216+
"EXECUTE DBT PROJECT pipeline_name args='test'",
213217
id="simple-command",
214218
),
215219
pytest.param(
@@ -221,12 +225,12 @@ class TestDBTExecute:
221225
"-f",
222226
"--select @source:snowplow,tag:nightly models/export",
223227
],
224-
"EXECUTE DBT PROJECT pipeline_name run -f --select @source:snowplow,tag:nightly models/export",
228+
"EXECUTE DBT PROJECT pipeline_name args='run -f --select @source:snowplow,tag:nightly models/export'",
225229
id="with-dbt-options",
226230
),
227231
pytest.param(
228232
["dbt", "execute", "pipeline_name", "compile", "--vars '{foo:bar}'"],
229-
"EXECUTE DBT PROJECT pipeline_name compile --vars '{foo:bar}'",
233+
"EXECUTE DBT PROJECT pipeline_name args='compile --vars '{foo:bar}''",
230234
id="with-dbt-vars",
231235
),
232236
pytest.param(
@@ -242,7 +246,7 @@ class TestDBTExecute:
242246
"--info",
243247
"--config-file=/",
244248
],
245-
"EXECUTE DBT PROJECT pipeline_name compile --format=TXT -v -h --debug --info --config-file=/",
249+
"EXECUTE DBT PROJECT pipeline_name args='compile --format=TXT -v -h --debug --info --config-file=/'",
246250
id="with-dbt-conflicting-options",
247251
),
248252
pytest.param(
@@ -253,7 +257,7 @@ class TestDBTExecute:
253257
"pipeline_name",
254258
"compile",
255259
],
256-
"EXECUTE DBT PROJECT pipeline_name compile",
260+
"EXECUTE DBT PROJECT pipeline_name args='compile'",
257261
id="with-cli-flag",
258262
),
259263
],

0 commit comments

Comments
 (0)