-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
App post-deploy hook: sql scripts (#1244)
* add app post deploy sql script hook * default warehouse and database * clarify release notes * add template support * release notes * add custom schema validation * simplify schema validation
- Loading branch information
1 parent
b0aec1f
commit 2444bd5
Showing
18 changed files
with
375 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
# 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 textwrap import dedent | ||
from unittest import mock | ||
|
||
import pytest | ||
from snowflake.cli.api.project.definition_manager import DefinitionManager | ||
from snowflake.cli.api.project.errors import SchemaValidationError | ||
from snowflake.cli.api.project.schemas.native_app.application import ( | ||
ApplicationPostDeployHook, | ||
) | ||
from snowflake.cli.plugins.nativeapp.run_processor import NativeAppRunProcessor | ||
|
||
from tests.nativeapp.patch_utils import mock_connection | ||
from tests.nativeapp.utils import ( | ||
NATIVEAPP_MANAGER_EXECUTE, | ||
NATIVEAPP_MANAGER_EXECUTE_QUERIES, | ||
RUN_PROCESSOR_APP_POST_DEPLOY_HOOKS, | ||
) | ||
from tests.testing_utils.fixtures import MockConnectionCtx | ||
|
||
CLI_GLOBAL_TEMPLATE_CONTEXT = ( | ||
"snowflake.cli.api.cli_global_context._CliGlobalContextAccess.template_context" | ||
) | ||
MOCK_CONNECTION_DB = "tests.testing_utils.fixtures.MockConnectionCtx.database" | ||
MOCK_CONNECTION_WH = "tests.testing_utils.fixtures.MockConnectionCtx.warehouse" | ||
|
||
|
||
def _get_run_processor(working_dir): | ||
dm = DefinitionManager(working_dir) | ||
return NativeAppRunProcessor( | ||
project_definition=dm.project_definition.native_app, | ||
project_root=dm.project_root, | ||
) | ||
|
||
|
||
@mock.patch(NATIVEAPP_MANAGER_EXECUTE) | ||
@mock.patch(NATIVEAPP_MANAGER_EXECUTE_QUERIES) | ||
@mock.patch(CLI_GLOBAL_TEMPLATE_CONTEXT, new_callable=mock.PropertyMock) | ||
@mock_connection() | ||
def test_sql_scripts( | ||
mock_conn, | ||
mock_cli_ctx, | ||
mock_execute_queries, | ||
mock_execute_query, | ||
project_directory, | ||
): | ||
mock_conn.return_value = MockConnectionCtx() | ||
mock_cli_ctx.return_value = { | ||
"ctx": {"native_app": {"name": "myapp"}, "env": {"foo": "bar"}} | ||
} | ||
with project_directory("napp_post_deploy") as project_dir: | ||
processor = _get_run_processor(str(project_dir)) | ||
|
||
processor._execute_post_deploy_hooks() # noqa SLF001 | ||
|
||
assert mock_execute_query.mock_calls == [ | ||
mock.call("use warehouse MockWarehouse"), | ||
mock.call("use database MockDatabase"), | ||
mock.call("use warehouse MockWarehouse"), | ||
mock.call("use database MockDatabase"), | ||
] | ||
assert mock_execute_queries.mock_calls == [ | ||
# Verify template variables were expanded correctly | ||
mock.call( | ||
dedent( | ||
"""\ | ||
-- app post-deploy script (1/2) | ||
select myapp; | ||
select bar; | ||
""" | ||
) | ||
), | ||
mock.call("-- app post-deploy script (2/2)\n"), | ||
] | ||
|
||
|
||
@mock.patch(NATIVEAPP_MANAGER_EXECUTE) | ||
@mock.patch(NATIVEAPP_MANAGER_EXECUTE_QUERIES) | ||
@mock.patch(CLI_GLOBAL_TEMPLATE_CONTEXT, new_callable=mock.PropertyMock) | ||
@mock_connection() | ||
@mock.patch(MOCK_CONNECTION_DB, new_callable=mock.PropertyMock) | ||
@mock.patch(MOCK_CONNECTION_WH, new_callable=mock.PropertyMock) | ||
def test_sql_scripts_with_no_warehouse_no_database( | ||
mock_conn_wh, | ||
mock_conn_db, | ||
mock_conn, | ||
mock_cli_ctx, | ||
mock_execute_queries, | ||
mock_execute_query, | ||
project_directory, | ||
): | ||
mock_conn_wh.return_value = None | ||
mock_conn_db.return_value = None | ||
mock_conn.return_value = MockConnectionCtx(None) | ||
mock_cli_ctx.return_value = { | ||
"ctx": {"native_app": {"name": "myapp"}, "env": {"foo": "bar"}} | ||
} | ||
with project_directory("napp_post_deploy") as project_dir: | ||
processor = _get_run_processor(str(project_dir)) | ||
|
||
processor._execute_post_deploy_hooks() # noqa SLF001 | ||
|
||
# Verify no "use warehouse" and no "use database" were called | ||
assert mock_execute_query.mock_calls == [] | ||
assert mock_execute_queries.mock_calls == [ | ||
mock.call( | ||
dedent( | ||
"""\ | ||
-- app post-deploy script (1/2) | ||
select myapp; | ||
select bar; | ||
""" | ||
) | ||
), | ||
mock.call("-- app post-deploy script (2/2)\n"), | ||
] | ||
|
||
|
||
@mock_connection() | ||
def test_missing_sql_script( | ||
mock_conn, | ||
project_directory, | ||
): | ||
mock_conn.return_value = MockConnectionCtx() | ||
with project_directory("napp_post_deploy_missing_file") as project_dir: | ||
processor = _get_run_processor(str(project_dir)) | ||
|
||
with pytest.raises(FileNotFoundError) as err: | ||
processor._execute_post_deploy_hooks() # noqa SLF001 | ||
|
||
|
||
@mock.patch(RUN_PROCESSOR_APP_POST_DEPLOY_HOOKS, new_callable=mock.PropertyMock) | ||
@mock_connection() | ||
def test_invalid_hook_type( | ||
mock_conn, | ||
mock_deploy_hooks, | ||
project_directory, | ||
): | ||
mock_hook = mock.Mock() | ||
mock_hook.invalid_type = "invalid_type" | ||
mock_hook.sql_script = None | ||
mock_deploy_hooks.return_value = [mock_hook] | ||
mock_conn.return_value = MockConnectionCtx() | ||
with project_directory("napp_post_deploy") as project_dir: | ||
processor = _get_run_processor(str(project_dir)) | ||
|
||
with pytest.raises(ValueError) as err: | ||
processor._execute_post_deploy_hooks() # noqa SLF001 | ||
assert "Unsupported application post-deploy hook type" in str(err) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args,expected_error", | ||
[ | ||
({"sql_script": "/path"}, None), | ||
({}, "missing following fields: ('sql_script',)"), | ||
], | ||
) | ||
def test_post_deploy_hook_schema(args, expected_error): | ||
if expected_error: | ||
with pytest.raises(SchemaValidationError) as err: | ||
ApplicationPostDeployHook(**args) | ||
assert expected_error in str(err) | ||
else: | ||
ApplicationPostDeployHook(**args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/test_data/projects/napp_post_deploy/scripts/post_deploy1.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- app post-deploy script (1/2) | ||
|
||
select &{ ctx.native_app.name }; | ||
select &{ ctx.env.foo }; |
1 change: 1 addition & 0 deletions
1
tests/test_data/projects/napp_post_deploy/scripts/post_deploy2.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- app post-deploy script (2/2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
definition_version: 1.1 | ||
native_app: | ||
name: myapp | ||
|
||
artifacts: | ||
- src: app/* | ||
dest: ./ | ||
|
||
application: | ||
post_deploy: | ||
- sql_script: scripts/post_deploy1.sql | ||
- sql_script: scripts/post_deploy2.sql | ||
|
||
env: | ||
foo: bar |
11 changes: 11 additions & 0 deletions
11
tests/test_data/projects/napp_post_deploy_missing_file/snowflake.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
definition_version: 1 | ||
native_app: | ||
name: myapp | ||
|
||
artifacts: | ||
- src: app/* | ||
dest: ./ | ||
|
||
application: | ||
post_deploy: | ||
- sql_script: scripts/missing.sql |
Oops, something went wrong.