Skip to content

Commit

Permalink
snow ws migrate fixes #1 (#1505)
Browse files Browse the repository at this point in the history
Fix for templated names
  • Loading branch information
sfc-gh-jsikorski committed Aug 29, 2024
1 parent f436d93 commit 97e473b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/snowflake/cli/_plugins/snowpark/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
SnowparkObject,
SnowparkObjectManager,
StageToArtefactMapping,
is_name_a_templated_one,
)
from snowflake.cli._plugins.snowpark.package.anaconda_packages import (
AnacondaPackages,
Expand Down Expand Up @@ -474,7 +475,7 @@ def migrate_v1_snowpark_to_v2(pd: ProjectDefinition):
if entity.schema_name is not None:
identifier["schema"] = entity.schema_name

if entity.name.startswith("<%") and entity.name.endswith("%>"):
if is_name_a_templated_one(entity.name):
entity_name = f"snowpark_entity_{index}"
else:
entity_name = entity.name
Expand Down
4 changes: 4 additions & 0 deletions src/snowflake/cli/_plugins/snowpark/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,7 @@ def _compare_imports(
}

return project_imports != object_imports


def is_name_a_templated_one(name: str) -> bool:
return ("<!" in name and "!>" in name) or ("<%" in name and "%>" in name)
5 changes: 3 additions & 2 deletions src/snowflake/cli/_plugins/streamlit/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
add_object_command_aliases,
scope_option,
)
from snowflake.cli._plugins.snowpark.common import is_name_a_templated_one
from snowflake.cli._plugins.streamlit.manager import StreamlitManager
from snowflake.cli.api.cli_global_context import get_cli_context
from snowflake.cli.api.commands.decorators import (
Expand Down Expand Up @@ -204,8 +205,8 @@ def migrate_v1_streamlit_to_v2(pd: ProjectDefinition):
if pd.streamlit.database:
identifier["database"] = pd.streamlit.database

if pd.streamlit.name.startswith("<%") and pd.streamlit.name.endswith("%>"):
streamlit_name = "streamlit_entity_1"
if is_name_a_templated_one(pd.streamlit.name):
streamlit_name = "streamlit_entity_1" # we don't care about numbering, as only 1 streamlit is allowed in V1
else:
streamlit_name = pd.streamlit.name

Expand Down
18 changes: 18 additions & 0 deletions tests/snowpark/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
_convert_resource_details_to_dict,
_snowflake_dependencies_differ,
_sql_to_python_return_type_mapper,
is_name_a_templated_one,
)
from snowflake.cli.api.project.schemas.entities.snowpark_entity import (
ProcedureEntityModel,
Expand Down Expand Up @@ -165,3 +166,20 @@ def test_check_if_replace_is_required_file_changes(
)
== expected
)


@pytest.mark.parametrize(
"name,expected",
[
("foo", False),
("<% ctx.env.foo %>", True),
("<! name | to_snowflake_identifier !>", True),
("app_<% ctx.env.USERNAME %>", True),
("<Unnecesarily_!_complicated!_name>", False),
("<% fn.concat_ids(ctx.native_app.name, ctx.env.pkg_suffix) %>", True),
("myapp_base_name_<% fn.sanitize_id(fn.get_username()) %>", True),
("<myapp>", False),
],
)
def test_is_name_is_templated_one(name: str, expected: bool):
assert is_name_a_templated_one(name) == expected
2 changes: 1 addition & 1 deletion tests/workspace/__snapshots__/test_manager.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
'''
definition_version: '2'
entities:
<! name | to_snowflake_identifier !>:
streamlit_entity_1:
artifacts:
- streamlit_app.py
- environment.yml
Expand Down

0 comments on commit 97e473b

Please sign in to comment.