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

Add example for apps in subdirectories #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions apps-in-subdir/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
snowflake.local.yml
output/**
59 changes: 59 additions & 0 deletions apps-in-subdir/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Apps in subdirectories

This Snowflake Native Application sample demonstrates how to utilize subdirectories to hold multiple versions of your Native Application in the same stage. This feature is only available in SnowCLI versions 3.x.x or later.

Comment on lines +3 to +4
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will update the version to replace version 3.x.x when this feature is included in a release.

## Getting Started

### Examine the folders structure

In this example, the application source files are organized into two folders under the root directory. Each folder contains all the source files required to create and run a Native Application.

FIX THIS:

```
.
├── README.md
├── snowflake.yml
├── v1
│ ├── README.md
│ ├── manifest.yml
│ └── setup_script.sql
└── v2
├── README.md
├── manifest.yml
└── setup_script.sql
```

### Project Definition File

To include multiple Native Apps in the same stage, we have to specify subdirectories for the stage that host each applications' source files. Application Package Entity definition includes a `stage_subdirectory` field for this purpose. You can set this field to the subdirectory in the stage that you wish to create an Application object or a Version from.

In this example, we have created two Application Package entities in the definition file, both of which refer to the **same** Application Package in Snowflake (same value for the `identifier` field). Each of this Package entities have a distinct `stage_subdirectory` which tells the CLI to create and manage the application source files within that subdirectory in the stage.

For this example, we will demonstrate creating and versioning apps from distinct subdirectories in the Stage of one Application Package in snowflake using the CLI. To achieve this, we have defined two Application entities in the project definition file, each `from` one of the Application Package entities as the `target`. Notice that the `identifier` fields of the Applications are different, as to not override one another.

## Create a Native Application

Execute the following command:

```bash
snow app run --app-entity-id=app_v1
```
Note that we specified the Application Entity we want to run. Application Entity `app_v1` is created from Package Entity `pkg_v1` as instructed in project definition file. All the artifacts listed in `pkg_v1` are deployed to the stage under the `stage_subdirectory`: `v1`.


Now let's do the same for the other app:

```bash
snow app run --app-entity-id=app_v2
```
The above command will create `app_v2` from Application Entity `pkg_v2`. All the artifacts listed in `pkg_v2` have been deployed to the stage under the `stage_subdirectory`: `v2`.

## Create a Version

Execute the following command:
```bash
snow app version create version1 --app-entity-id=app_v1
```
Above command will create a version from the artifacts for the selected Application Entity which are under `stage_subdirectory`: `v1` in the stage.

36 changes: 36 additions & 0 deletions apps-in-subdir/snowflake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This is a project definition file, a required component if you intend to use Snowflake CLI in a project directory such as this template.

definition_version: 2
entities:
pkg_v1:
type: application package
identifier: <% fn.concat_ids('apps_in_subdir_pkg', ctx.env.suffix) %>
artifacts:
- src: v1/*
dest: ./
stage_subdirectory: v1


pkg_v2:
type: application package
identifier: <% fn.concat_ids('apps_in_subdir_pkg', ctx.env.suffix) %>
artifacts:
- src: v2/*
dest: ./
stage_subdirectory: v2

app_v1:
type: application
from:
target: pkg_v1
identifier: <% fn.concat_ids('apps_in_subdir_v1', ctx.env.suffix) %>

app_v2:
type: application
from:
target: pkg_v2
identifier: <% fn.concat_ids('apps_in_subdir_v2', ctx.env.suffix) %>

env:
suffix: <% fn.concat_ids('_', fn.sanitize_id(fn.get_username('unknown_user')) | lower) %>

20 changes: 20 additions & 0 deletions apps-in-subdir/v1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Welcome to your First Snowflake Native App!

In this Snowflake Native App, you will be able to explore some basic concepts such as application role, versioned schemas and creating procedures and functions within a setup script.

For more information about a Snowflake Native App, please read the [official Snowflake documentation](https://docs.snowflake.com/en/developer-guide/native-apps/native-apps-about) which goes in depth about many additional functionalities of this framework.

## Using the application after installation
To interact with the application after it has successfully installed in your account, switch to the application owner role first.

### Calling a stored procedure

```
CALL <your_application_name>.<schema_name>.<stored_procedure_name_with_args>;
```

### Calling a function

```
SELECT <your_application_name>.<schema_name>.<udf_with_args>;
```
8 changes: 8 additions & 0 deletions apps-in-subdir/v1/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is a manifest.yml file, a required component of creating a Snowflake Native App.
# This file defines properties required by the application package, including the location of the setup script and version definitions.
# Refer to https://docs.snowflake.com/en/developer-guide/native-apps/creating-manifest for a detailed understanding of this file.

manifest_version: 1
artifacts:
setup_script: setup_script.sql
readme: README.md
11 changes: 11 additions & 0 deletions apps-in-subdir/v1/setup_script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- This is the setup script that runs while installing a Snowflake Native App in a consumer account.
-- To write this script, you can familiarize yourself with some of the following concepts:
-- Application Roles
-- Versioned Schemas
-- UDFs/Procs
-- Extension Code
-- Refer to https://docs.snowflake.com/en/developer-guide/native-apps/creating-setup-script for a detailed understanding of this file.

CREATE OR ALTER VERSIONED SCHEMA core;

-- The rest of this script is left blank for purposes of your learning and exploration.
20 changes: 20 additions & 0 deletions apps-in-subdir/v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Welcome to your First Snowflake Native App!

In this Snowflake Native App, you will be able to explore some basic concepts such as application role, versioned schemas and creating procedures and functions within a setup script.

For more information about a Snowflake Native App, please read the [official Snowflake documentation](https://docs.snowflake.com/en/developer-guide/native-apps/native-apps-about) which goes in depth about many additional functionalities of this framework.

## Using the application after installation
To interact with the application after it has successfully installed in your account, switch to the application owner role first.

### Calling a stored procedure

```
CALL <your_application_name>.<schema_name>.<stored_procedure_name_with_args>;
```

### Calling a function

```
SELECT <your_application_name>.<schema_name>.<udf_with_args>;
```
9 changes: 9 additions & 0 deletions apps-in-subdir/v2/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This is a manifest.yml file, a required component of creating a Snowflake Native App.
# This file defines properties required by the application package, including the location of the setup script and version definitions.
# Refer to https://docs.snowflake.com/en/developer-guide/native-apps/creating-manifest for a detailed understanding of this file.

manifest_version: 1

artifacts:
setup_script: setup_script.sql
readme: README.md
11 changes: 11 additions & 0 deletions apps-in-subdir/v2/setup_script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- This is the setup script that runs while installing a Snowflake Native App in a consumer account.
-- To write this script, you can familiarize yourself with some of the following concepts:
-- Application Roles
-- Versioned Schemas
-- UDFs/Procs
-- Extension Code
-- Refer to https://docs.snowflake.com/en/developer-guide/native-apps/creating-setup-script for a detailed understanding of this file.

CREATE OR ALTER VERSIONED SCHEMA core;

-- The rest of this script is left blank for purposes of your learning and exploration.
Loading