Skip to content

Commit c9045c9

Browse files
Add --if-not-exists flag to project create (#2299)
* Add --if-not-exists flag to project create * update snapshots
1 parent 906f1ed commit c9045c9

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from snowflake.cli.api.cli_global_context import get_cli_context
2727
from snowflake.cli.api.commands.decorators import with_project_definition
2828
from snowflake.cli.api.commands.flags import (
29+
IfNotExistsOption,
2930
OverrideableOption,
3031
PruneOption,
3132
entity_argument,
@@ -118,6 +119,9 @@ def create(
118119
"--no-version",
119120
help="Do not initialize project with a new version, only create the snowflake object.",
120121
),
122+
if_not_exists: bool = IfNotExistsOption(
123+
help="Do nothing if the project already exists."
124+
),
121125
**options,
122126
):
123127
"""
@@ -133,7 +137,11 @@ def create(
133137
)
134138
om = ObjectManager()
135139
if om.object_exists(object_type="project", fqn=project.fqn):
136-
raise CliError(f"Project '{project.fqn}' already exists.")
140+
message = f"Project '{project.fqn}' already exists."
141+
if if_not_exists:
142+
return MessageResult(message)
143+
raise CliError(message)
144+
137145
if not no_version and om.object_exists(
138146
object_type="stage", fqn=FQN.from_stage(project.stage)
139147
):

tests/__snapshots__/test_help_messages.ambr

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7620,14 +7620,15 @@
76207620
| [default: None] |
76217621
+------------------------------------------------------------------------------+
76227622
+- Options --------------------------------------------------------------------+
7623-
| --no-version Do not initialize project with a new version, |
7624-
| only create the snowflake object. |
7625-
| --project -p TEXT Path where the Snowflake project is stored. |
7626-
| Defaults to the current working directory. |
7627-
| --env TEXT String in the format key=value. Overrides |
7628-
| variables from the env section used for |
7629-
| templates. |
7630-
| --help -h Show this message and exit. |
7623+
| --no-version Do not initialize project with a new version, |
7624+
| only create the snowflake object. |
7625+
| --if-not-exists Do nothing if the project already exists. |
7626+
| --project -p TEXT Path where the Snowflake project is stored. |
7627+
| Defaults to the current working directory. |
7628+
| --env TEXT String in the format key=value. Overrides |
7629+
| variables from the env section used for |
7630+
| templates. |
7631+
| --help -h Show this message and exit. |
76317632
+------------------------------------------------------------------------------+
76327633
+- Connection configuration ---------------------------------------------------+
76337634
| --connection,--environment -c TEXT Name of the connection, as |

tests/dcm_project/test_commands.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ def test_create(mock_om, mock_pm, runner, project_directory, no_version):
3737
assert create_kwargs["project"].fqn == FQN.from_string("my_project")
3838

3939

40+
@mock.patch(ProjectManager)
41+
@mock.patch(ObjectManager)
42+
@pytest.mark.parametrize("if_not_exists", [False, True])
43+
def test_create_object_exists(
44+
mock_om, mock_pm, runner, project_directory, if_not_exists
45+
):
46+
mock_om().object_exists.return_value = True
47+
with project_directory("dcm_project"):
48+
command = ["project", "create"]
49+
if if_not_exists:
50+
command.append("--if-not-exists")
51+
result = runner.invoke(command)
52+
assert result.exit_code == 0 if if_not_exists else 1, result.output
53+
assert "Project 'my_project' already exists." in result.output
54+
mock_pm().create.assert_not_called()
55+
56+
4057
@mock.patch(ProjectManager)
4158
@pytest.mark.parametrize(
4259
"prune,_from,expected_prune_value",

tests_integration/test_dcm_project.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ def test_create_corner_cases(
124124
_assert_project_has_versions(
125125
runner, project_name, expected_versions={("VERSION$1", None)}
126126
)
127+
result = runner.invoke_with_connection(["project", "create", "--if-not-exists"])
128+
assert result.exit_code == 0, result.output
129+
assert f"Project '{project_name}' already exists." in result.output
130+
_assert_project_has_versions(
131+
runner, project_name, expected_versions={("VERSION$1", None)}
132+
)
127133

128134

129135
@pytest.mark.integration

0 commit comments

Comments
 (0)