Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge v2 entities into template context instead of overriding #1730

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/snowflake/cli/_plugins/nativeapp/v2_conversions/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,15 @@ def wrapper(*args, **kwargs):
# 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
)
new_ctx = pdfv2_dump | dict(env=cm.template_context["ctx"]["env"])
cm.override_template_context = cm.template_context | dict(ctx=new_ctx)
# Add new entities to the template context so that templates in
# migrated package scripts can refer to the new entities
# Keep everything else in the template context since
# the user might still have other files with v1 templates in them
entities = {
k: e.model_dump(exclude_none=True, warnings=False, by_alias=True)
for k, e in pdfv2.entities.items()
}
Comment on lines +301 to +304
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like that's not the right fix, since it can introduce a different behaviour once the contexts are merged (granted, I can't think of a way to trigger this with a valid set of templates, but still...). I expected we'd instead preserve the entire v1 context, separately, and decide at expansion time which of the contexts should apply. I'm still thinking through what it means to merge the contexts though, so I might be off here.

cm.template_context["ctx"]["entities"] = entities
elif single_app_and_package:
package_entity_id = kwargs.get("package_entity_id", "")
app_entity_id = kwargs.get("app_entity_id", "")
Expand Down
4 changes: 3 additions & 1 deletion tests_integration/nativeapp/test_project_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,9 @@ def test_nativeapp_templates_processor_with_run(


@pytest.mark.integration
@pytest.mark.parametrize("test_project", ["napp_templates_processors_v2"])
@pytest.mark.parametrize(
"test_project", ["napp_templates_processors_v1", "napp_templates_processors_v2"]
)
@pytest.mark.parametrize("with_project_flag", [True, False])
def test_nativeapp_templates_processor_with_deploy(
runner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@

CREATE OR ALTER VERSIONED SCHEMA <% ctx.env.schema_name %>;
EXECUTE IMMEDIATE from '/another_script.sql';
select 'ctx.native_app.name: <% ctx.native_app.name %>';
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@

CREATE OR ALTER VERSIONED SCHEMA <% ctx.env.schema_name %>;
EXECUTE IMMEDIATE from '/another_script.sql';
select 'ctx.entities.pkg.identifier: <% ctx.entities.pkg.identifier %>';
Loading