Skip to content

Commit

Permalink
Rename ws migrate to migrate-definition (#1598)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-turbaszek authored and sfc-gh-vmaleki committed Sep 20, 2024
1 parent 31c739b commit 412c0cf
Show file tree
Hide file tree
Showing 13 changed files with 577 additions and 180 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* Added support for external access (api integrations and secrets) in Streamlit.
* Added support for `<% ... %>` syntax in SQL templating.
* Support multiple Streamlit application in single snowflake.yml project definition file.
* Added `snow ws migrate` command to migrate `snowflake.yml` file from V1 to V2.
* Added `snow helpers v1-to-v2` command to migrate `snowflake.yml` file from V1 to V2.
* Added `--package-entity-id` and `--app-entity-id` options to `snow app` commands to allow targeting specific entities when the `definition_version` in `snowflake.yml` is `2` or higher and it contains multiple `application package` or `application` entities.
* Added templates expansion of arbitrary files for Native Apps through `templates` processor.
* Added `SNOWFLAKE_..._PRIVATE_KEY_RAW` environment variable to pass private key as a raw string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from snowflake.cli._plugins.connection import plugin_spec as connection_plugin_spec
from snowflake.cli._plugins.cortex import plugin_spec as cortex_plugin_spec
from snowflake.cli._plugins.git import plugin_spec as git_plugin_spec
from snowflake.cli._plugins.helpers import plugin_spec as migrate_plugin_spec
from snowflake.cli._plugins.init import plugin_spec as init_plugin_spec
from snowflake.cli._plugins.nativeapp import plugin_spec as nativeapp_plugin_spec
from snowflake.cli._plugins.notebook import plugin_spec as notebook_plugin_spec
Expand All @@ -31,6 +32,7 @@
def get_builtin_plugin_name_to_plugin_spec():
plugin_specs = {
"connection": connection_plugin_spec,
"helpers": migrate_plugin_spec,
"spcs": spcs_plugin_spec,
"nativeapp": nativeapp_plugin_spec,
"object": object_plugin_spec,
Expand Down
13 changes: 13 additions & 0 deletions src/snowflake/cli/_plugins/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2024 Snowflake Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
57 changes: 57 additions & 0 deletions src/snowflake/cli/_plugins/helpers/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) 2024 Snowflake Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import typer
import yaml
from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
from snowflake.cli.api.output.types import MessageResult
from snowflake.cli.api.project.definition_conversion import (
convert_project_definition_to_v2,
)
from snowflake.cli.api.project.definition_manager import DefinitionManager
from snowflake.cli.api.secure_path import SecurePath

app = SnowTyperFactory(
name="helpers",
help="Helper commands.",
)


@app.command()
def v1_to_v2(
accept_templates: bool = typer.Option(
False, "-t", "--accept-templates", help="Allows the migration of templates."
),
**options,
):
"""Migrates the Snowpark, Streamlit, and Native App project definition files from V1 to V2."""
manager = DefinitionManager()
pd = manager.unrendered_project_definition

if pd.meets_version_requirement("2"):
return MessageResult("Project definition is already at version 2.")

pd_v2 = convert_project_definition_to_v2(manager.project_root, pd, accept_templates)

SecurePath("snowflake.yml").rename("snowflake_V1.yml")
with open("snowflake.yml", "w") as file:
yaml.dump(
pd_v2.model_dump(
exclude_unset=True, exclude_none=True, mode="json", by_alias=True
),
file,
)
return MessageResult("Project definition migrated to version 2.")
30 changes: 30 additions & 0 deletions src/snowflake/cli/_plugins/helpers/plugin_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2024 Snowflake Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from snowflake.cli._plugins.helpers import commands
from snowflake.cli.api.plugins.command import (
SNOWCLI_ROOT_COMMAND_PATH,
CommandSpec,
CommandType,
plugin_hook_impl,
)


@plugin_hook_impl
def command_spec():
return CommandSpec(
parent_command_path=SNOWCLI_ROOT_COMMAND_PATH,
command_type=CommandType.COMMAND_GROUP,
typer_instance=commands.app.create_instance(),
)
34 changes: 1 addition & 33 deletions src/snowflake/cli/_plugins/workspace/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from typing import List, Optional

import typer
import yaml
from snowflake.cli._plugins.nativeapp.artifacts import BundleMap
from snowflake.cli._plugins.nativeapp.common_flags import (
ForceOption,
Expand All @@ -34,46 +33,15 @@
from snowflake.cli.api.entities.common import EntityActions
from snowflake.cli.api.exceptions import IncompatibleParametersError
from snowflake.cli.api.output.types import MessageResult, QueryResult
from snowflake.cli.api.project.definition_conversion import (
convert_project_definition_to_v2,
)
from snowflake.cli.api.project.definition_manager import DefinitionManager
from snowflake.cli.api.secure_path import SecurePath

ws = SnowTyperFactory(
name="ws",
help="Deploy and interact with snowflake.yml-based entities.",
is_hidden=lambda: True,
)
log = logging.getLogger(__name__)


@ws.command()
def migrate(
accept_templates: bool = typer.Option(
False, "-t", "--accept-templates", help="Allows the migration of templates."
),
**options,
):
"""Migrates the Snowpark, Streamlit, and Native App project definition files from V1 to V2."""
manager = DefinitionManager()
pd = manager.unrendered_project_definition

if pd.meets_version_requirement("2"):
return MessageResult("Project definition is already at version 2.")

pd_v2 = convert_project_definition_to_v2(manager.project_root, pd, accept_templates)

SecurePath("snowflake.yml").rename("snowflake_V1.yml")
with open("snowflake.yml", "w") as file:
yaml.dump(
pd_v2.model_dump(
exclude_unset=True, exclude_none=True, mode="json", by_alias=True
),
file,
)
return MessageResult("Project definition migrated to version 2.")


@ws.command(requires_connection=True, hidden=True)
@with_project_definition()
def bundle(
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/test_help_messages.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
| connection Manages connections to Snowflake. |
| cortex Provides access to Snowflake Cortex. |
| git Manages git repositories in Snowflake. |
| helpers Helper commands. |
| init Creates project directory from template. |
| notebook Manages notebooks in Snowflake. |
| object Manages Snowflake objects like warehouses and stages |
Expand All @@ -34,7 +35,6 @@
| sql Executes Snowflake query. |
| stage Manages stages. |
| streamlit Manages a Streamlit app in Snowflake. |
| ws Deploy and interact with snowflake.yml-based entities. |
+------------------------------------------------------------------------------+


Expand Down Expand Up @@ -8161,6 +8161,7 @@
| connection Manages connections to Snowflake. |
| cortex Provides access to Snowflake Cortex. |
| git Manages git repositories in Snowflake. |
| helpers Helper commands. |
| init Creates project directory from template. |
| notebook Manages notebooks in Snowflake. |
| object Manages Snowflake objects like warehouses and stages |
Expand All @@ -8170,7 +8171,6 @@
| sql Executes Snowflake query. |
| stage Manages stages. |
| streamlit Manages a Streamlit app in Snowflake. |
| ws Deploy and interact with snowflake.yml-based entities. |
+------------------------------------------------------------------------------+


Expand Down
Empty file added tests/helpers/__init__.py
Empty file.
Loading

0 comments on commit 412c0cf

Please sign in to comment.