Skip to content

Commit 1b7f775

Browse files
feat: [SNOW-1890085] dbt deploy: use --force to create or replace
1 parent f5ad96e commit 1b7f775

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def deploy(
5252
dbt_adapter_version: str,
5353
force: bool,
5454
) -> SnowflakeCursor:
55-
# TODO: what to do with force?
5655
dbt_project_path = path.joinpath("dbt_project.yml")
5756
if not dbt_project_path.exists():
5857
raise ClickException(f"dbt_project.yml does not exist in provided path.")
@@ -79,7 +78,7 @@ def deploy(
7978

8079
with cli_console.phase("Creating DBT project"):
8180
staged_dbt_project_path = self._get_dbt_project_stage_path(stage_name)
82-
query = f"""CREATE OR REPLACE DBT PROJECT {name}
81+
query = f"""{'CREATE OR REPLACE' if force is True else 'CREATE'} DBT PROJECT {name}
8382
FROM {stage_name} MAIN_FILE='{staged_dbt_project_path}'
8483
DBT_VERSION='{dbt_version}' DBT_ADAPTER_VERSION='{dbt_adapter_version}'"""
8584
return self.execute_query(query)

tests/dbt/test_dbt_commands.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_deploys_project_from_source(
7373
assert result.exit_code == 0, result.output
7474
assert (
7575
mock_connect.mocked_ctx.get_query()
76-
== """CREATE OR REPLACE DBT PROJECT TEST_PIPELINE
76+
== """CREATE DBT PROJECT TEST_PIPELINE
7777
FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage MAIN_FILE='@MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage/dbt_project.yml'
7878
DBT_VERSION='1.2.3' DBT_ADAPTER_VERSION='3.4.5'"""
7979
)
@@ -102,11 +102,32 @@ def test_dbt_version_from_option_has_precedence_over_file(
102102
assert result.exit_code == 0, result.output
103103
assert (
104104
mock_connect.mocked_ctx.get_query()
105-
== """CREATE OR REPLACE DBT PROJECT TEST_PIPELINE
105+
== """CREATE DBT PROJECT TEST_PIPELINE
106106
FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage MAIN_FILE='@MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage/dbt_project.yml'
107107
DBT_VERSION='2.3.4' DBT_ADAPTER_VERSION='3.4.5'"""
108108
)
109109

110+
@mock.patch("snowflake.cli._plugins.dbt.manager.StageManager.put_recursive")
111+
@mock.patch("snowflake.cli._plugins.dbt.manager.StageManager.create")
112+
def test_force_flag_uses_create_or_replace(
113+
self, _mock_create, _mock_put_recursive, mock_connect, runner, dbt_project_path
114+
):
115+
result = runner.invoke(
116+
[
117+
"dbt",
118+
"deploy",
119+
"TEST_PIPELINE",
120+
f"--source={dbt_project_path}",
121+
"--force",
122+
"--dbt-adapter-version=3.4.5",
123+
]
124+
)
125+
126+
assert result.exit_code == 0, result.output
127+
assert mock_connect.mocked_ctx.get_query().startswith(
128+
"CREATE OR REPLACE DBT PROJECT"
129+
)
130+
110131
def test_raises_when_dbt_project_is_not_available(
111132
self, dbt_project_path, mock_connect, runner
112133
):

0 commit comments

Comments
 (0)