Skip to content

Commit

Permalink
fixup! fixup! Rename ws migrate to migrate-definition
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-turbaszek committed Sep 20, 2024
1 parent d725759 commit 404f39e
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 142 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 migrate-defintion` 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
2 changes: 1 addition & 1 deletion src/snowflake/cli/_plugins/helpers/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)


@app.command(no_args_is_help=True)
@app.command()
def v1_to_v2(
accept_templates: bool = typer.Option(
False, "-t", "--accept-templates", help="Allows the migration of templates."
Expand Down
Empty file added tests/helpers/__init__.py
Empty file.
143 changes: 143 additions & 0 deletions tests/helpers/test_v1_to_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import logging
from pathlib import Path
from textwrap import dedent

import pytest
import yaml


def test_migration_already_v2(
runner,
project_directory,
):
with project_directory("migration_already_v2"):
result = runner.invoke(["helpers", "v1-to-v2"])

assert result.exit_code == 0
assert "Project definition is already at version 2." in result.output


def test_migrations_with_multiple_entities(
runner, project_directory, os_agnostic_snapshot
):
with project_directory("migration_multiple_entities"):
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 0
assert Path("snowflake.yml").read_text() == os_agnostic_snapshot
assert Path("snowflake_V1.yml").read_text() == os_agnostic_snapshot


def test_migration_native_app_missing_manifest(runner, project_directory):
with project_directory("migration_multiple_entities") as project_dir:
(project_dir / "app" / "manifest.yml").unlink()
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 1
assert "manifest.yml file not found" in result.output


def test_migration_native_app_no_artifacts(runner, project_directory):
with project_directory("migration_multiple_entities") as project_dir:
with (project_dir / "snowflake.yml").open("r+") as snowflake_yml:
pdf = yaml.safe_load(snowflake_yml)
pdf["native_app"]["artifacts"] = []
snowflake_yml.seek(0)
yaml.safe_dump(pdf, snowflake_yml)
snowflake_yml.truncate()
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 1
assert "No artifacts mapping found in project definition" in result.output
assert "Could not bundle Native App artifacts" in result.output


def test_migration_native_app_package_scripts(runner, project_directory):
with project_directory("migration_package_scripts") as project_dir:
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 0
package_scripts_dir = project_dir / "package_scripts"
for file in package_scripts_dir.iterdir():
assert file.read_text() == dedent(
"""\
-- Just a demo package script, won't actually be executed in tests
select * from <% ctx.entities.pkg.identifier %>.my_schema.my_table
"""
)


@pytest.mark.parametrize(
"project_directory_name", ["snowpark_templated_v1", "streamlit_templated_v1"]
)
def test_if_template_is_not_rendered_during_migration_with_option_checked(
runner, project_directory, project_directory_name, os_agnostic_snapshot, caplog
):
with project_directory(project_directory_name):
with caplog.at_level(logging.WARNING):
result = runner.invoke(["helpers", "v1-to-v2", "--accept-templates"])

assert result.exit_code == 0
assert Path("snowflake.yml").read_text() == os_agnostic_snapshot
assert Path("snowflake_V1.yml").read_text() == os_agnostic_snapshot
assert (
"Your V1 definition contains templates. We cannot guarantee the correctness of the migration."
in caplog.text
)


@pytest.mark.parametrize(
"project_directory_name", ["snowpark_templated_v1", "streamlit_templated_v1"]
)
def test_if_template_raises_error_during_migrations(
runner, project_directory, project_directory_name, os_agnostic_snapshot
):
with project_directory(project_directory_name):
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 1, result.output
assert "Project definition contains templates" in result.output


def test_migration_with_only_envs(project_directory, runner):
with project_directory("sql_templating"):
result = runner.invoke(["helpers", "v1-to-v2"])

assert result.exit_code == 0


@pytest.mark.parametrize(
"duplicated_entity",
[
"""
- name: test
handler: "test"
signature: ""
returns: string
runtime: "3.10"
""",
"""
streamlit:
name: test
stage: streamlit
query_warehouse: test_warehouse
main_file: "streamlit_app.py"
title: "My Fancy Streamlit"
""",
"""
- name: test
handler: "test"
signature: ""
returns: string
handler: test
runtime: "3.10"
""",
],
)
def test_migrating_a_file_with_duplicated_keys_raises_an_error(
runner, project_directory, os_agnostic_snapshot, duplicated_entity
):
with project_directory("snowpark_procedures") as pd:
definition_path = pd / "snowflake.yml"

with open(definition_path, "a") as definition_file:
definition_file.write(duplicated_entity)

result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 1, result.output
assert result.output == os_agnostic_snapshot
140 changes: 0 additions & 140 deletions tests/workspace/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
# limitations under the License.
from __future__ import annotations

import logging
import os
from pathlib import Path
from textwrap import dedent
from unittest import mock

import pytest
Expand Down Expand Up @@ -86,143 +83,6 @@ def test_bundle_of_invalid_entity_type(temp_dir):
ws_manager.perform_action("app", EntityActions.BUNDLE)


def test_migration_already_v2(
runner,
project_directory,
):
with project_directory("migration_already_v2"):
result = runner.invoke(["helpers", "v1-to-v2"])

assert result.exit_code == 0
assert "Project definition is already at version 2." in result.output


def test_migrations_with_multiple_entities(
runner, project_directory, os_agnostic_snapshot
):
with project_directory("migration_multiple_entities"):
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 0
assert Path("snowflake.yml").read_text() == os_agnostic_snapshot
assert Path("snowflake_V1.yml").read_text() == os_agnostic_snapshot


def test_migration_native_app_missing_manifest(runner, project_directory):
with project_directory("migration_multiple_entities") as project_dir:
(project_dir / "app" / "manifest.yml").unlink()
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 1
assert "manifest.yml file not found" in result.output


def test_migration_native_app_no_artifacts(runner, project_directory):
with project_directory("migration_multiple_entities") as project_dir:
with (project_dir / "snowflake.yml").open("r+") as snowflake_yml:
pdf = yaml.safe_load(snowflake_yml)
pdf["native_app"]["artifacts"] = []
snowflake_yml.seek(0)
yaml.safe_dump(pdf, snowflake_yml)
snowflake_yml.truncate()
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 1
assert "No artifacts mapping found in project definition" in result.output
assert "Could not bundle Native App artifacts" in result.output


def test_migration_native_app_package_scripts(runner, project_directory):
with project_directory("migration_package_scripts") as project_dir:
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 0
package_scripts_dir = project_dir / "package_scripts"
for file in package_scripts_dir.iterdir():
assert file.read_text() == dedent(
"""\
-- Just a demo package script, won't actually be executed in tests
select * from <% ctx.entities.pkg.identifier %>.my_schema.my_table
"""
)


@pytest.mark.parametrize(
"project_directory_name", ["snowpark_templated_v1", "streamlit_templated_v1"]
)
def test_if_template_is_not_rendered_during_migration_with_option_checked(
runner, project_directory, project_directory_name, os_agnostic_snapshot, caplog
):
with project_directory(project_directory_name):
with caplog.at_level(logging.WARNING):
result = runner.invoke(["helpers", "v1-to-v2", "--accept-templates"])

assert result.exit_code == 0
assert Path("snowflake.yml").read_text() == os_agnostic_snapshot
assert Path("snowflake_V1.yml").read_text() == os_agnostic_snapshot
assert (
"Your V1 definition contains templates. We cannot guarantee the correctness of the migration."
in caplog.text
)


@pytest.mark.parametrize(
"project_directory_name", ["snowpark_templated_v1", "streamlit_templated_v1"]
)
def test_if_template_raises_error_during_migrations(
runner, project_directory, project_directory_name, os_agnostic_snapshot
):
with project_directory(project_directory_name):
result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 1
assert "Project definition contains templates" in result.output


def test_migration_with_only_envs(project_directory, runner):
with project_directory("sql_templating"):
result = runner.invoke(["helpers", "v1-to-v2"])

assert result.exit_code == 0


@pytest.mark.parametrize(
"duplicated_entity",
[
"""
- name: test
handler: "test"
signature: ""
returns: string
runtime: "3.10"
""",
"""
streamlit:
name: test
stage: streamlit
query_warehouse: test_warehouse
main_file: "streamlit_app.py"
title: "My Fancy Streamlit"
""",
"""
- name: test
handler: "test"
signature: ""
returns: string
handler: test
runtime: "3.10"
""",
],
)
def test_migrating_a_file_with_duplicated_keys_raises_an_error(
runner, project_directory, os_agnostic_snapshot, duplicated_entity
):
with project_directory("snowpark_procedures") as pd:
definition_path = pd / "snowflake.yml"

with open(definition_path, "a") as definition_file:
definition_file.write(duplicated_entity)

result = runner.invoke(["helpers", "v1-to-v2"])
assert result.exit_code == 1
assert result.output == os_agnostic_snapshot


@pytest.mark.parametrize("definition_version", [1, "1.1"])
def test_migrate_nativeapp_fields_with_username(
runner, project_directory, definition_version
Expand Down

0 comments on commit 404f39e

Please sign in to comment.