From f792e74656c44a2a051a58d1226522c27b0373ee Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Fri, 4 Oct 2024 13:20:06 -0400 Subject: [PATCH] Convert `snow app deploy` to v2-native --- .../cli/_plugins/nativeapp/commands.py | 30 +++++++------------ .../nativeapp/v2_conversions/compat.py | 12 ++++++-- .../nativeapp/test_feature_metrics.py | 7 +++-- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/snowflake/cli/_plugins/nativeapp/commands.py b/src/snowflake/cli/_plugins/nativeapp/commands.py index 8baeaa148e..32cc1b6442 100644 --- a/src/snowflake/cli/_plugins/nativeapp/commands.py +++ b/src/snowflake/cli/_plugins/nativeapp/commands.py @@ -313,7 +313,7 @@ def app_teardown( @app.command("deploy", requires_connection=True) @with_project_definition() -@nativeapp_definition_v2_to_v1() +@single_app_and_package() def app_deploy( prune: Optional[bool] = typer.Option( default=None, @@ -345,16 +345,6 @@ def app_deploy( Creates an application package in your Snowflake account and syncs the local changes to the stage without creating or updating the application. Running this command with no arguments at all, as in `snow app deploy`, is a shorthand for `snow app deploy --prune --recursive`. """ - - assert_project_type("native_app") - - if force: - policy = AllowAlwaysPolicy() - elif interactive: - policy = AskAlwaysPolicy() - else: - policy = DenyAlwaysPolicy() - has_paths = paths is not None and len(paths) > 0 if prune is None and recursive is None and not has_paths: prune = True @@ -364,24 +354,24 @@ def app_deploy( prune = False if recursive is None: recursive = False - if has_paths and prune: raise IncompatibleParametersError(["paths", "--prune"]) cli_context = get_cli_context() - manager = NativeAppManager( - project_definition=cli_context.project_definition.native_app, + ws = WorkspaceManager( + project_definition=cli_context.project_definition, project_root=cli_context.project_root, ) - - bundle_map = manager.build_bundle() - manager.deploy( - bundle_map=bundle_map, + package_id = options["package_entity_id"] + ws.perform_action( + package_id, + EntityActions.DEPLOY, prune=prune, recursive=recursive, - local_paths_to_sync=paths, + paths=paths, validate=validate, - policy=policy, + interactive=interactive, + force=force, ) return MessageResult( diff --git a/src/snowflake/cli/_plugins/nativeapp/v2_conversions/compat.py b/src/snowflake/cli/_plugins/nativeapp/v2_conversions/compat.py index 522e89d493..beb59963ae 100644 --- a/src/snowflake/cli/_plugins/nativeapp/v2_conversions/compat.py +++ b/src/snowflake/cli/_plugins/nativeapp/v2_conversions/compat.py @@ -316,14 +316,20 @@ def wrapper(*args, **kwargs): is_package = isinstance(entity, ApplicationPackageEntityModel) key = "package_entity_id" if is_package else "app_entity_id" kwargs[key] = entity_id + cm = get_cli_context_manager() + + # Override the project definition so that the command operates on the new entities cm.override_project_definition = pdfv2 + + # Override the template context so that templates refer to the new entities + # Reuse the old ctx.env and other top-level keys in the template context + # since they don't change between v1 and v2 pdfv2_dump = pdfv2.model_dump( exclude_none=True, warnings=False, by_alias=True ) - cm.override_template_context = cm.template_context | dict( - ctx=pdfv2_dump - ) + new_ctx = pdfv2_dump | dict(env=cm.template_context["ctx"]["env"]) + cm.override_template_context = cm.template_context | dict(ctx=new_ctx) else: package_entity_id = kwargs.get("package_entity_id", "") app_entity_id = kwargs.get("app_entity_id", "") diff --git a/tests_integration/nativeapp/test_feature_metrics.py b/tests_integration/nativeapp/test_feature_metrics.py index 33312f7037..f83561a5ea 100644 --- a/tests_integration/nativeapp/test_feature_metrics.py +++ b/tests_integration/nativeapp/test_feature_metrics.py @@ -103,15 +103,16 @@ def test_sql_templating_emits_counter( CLICounterField.PACKAGE_SCRIPTS: 0, }, ), - # ensure that package scripts are picked up + # package scripts are auto-converted to post deploy scripts in v1 ( "app deploy", "integration_external", { CLICounterField.SNOWPARK_PROCESSOR: 0, CLICounterField.TEMPLATES_PROCESSOR: 0, - CLICounterField.POST_DEPLOY_SCRIPTS: 0, - CLICounterField.PACKAGE_SCRIPTS: 1, + CLICounterField.PDF_TEMPLATES: 1, + CLICounterField.POST_DEPLOY_SCRIPTS: 1, + CLICounterField.PACKAGE_SCRIPTS: 0, }, ), # ensure post deploy scripts are picked up for v2