설정 스크립트 만들기¶
This topic describes how to use the setup script to create objects in the app when running the CREATE APPLICATION command.
또한 애플리케이션 역할과 설정 스크립트 내에서 이러한 역할을 사용하는 방법도 설명합니다.
설정 스크립트 정보¶
설정 스크립트에는 다음 컨텍스트 중 하나에서 CREATE APPLICATION 명령이 실행될 때 실행되는 SQL 문이 포함됩니다.
컨슈머가 Snowflake Native App 을 설치하거나 업그레이드합니다.
A provider creates or upgrades an app when testing the application package.
참고
설정 스크립트는 SQL 명령 사용만 지원합니다. 다른 언어는 지원되지 않습니다.
The SQL statements in the setup script create objects within the app that are required by the app. This includes database objects, stored procedures, views, and application roles.
매니페스트 파일은 설정 스크립트에 대한 파일 이름과 상대 경로를 지정합니다. 설정 스크립트는 명명된 스테이징에 있어야 하며 앱 패키지에서 액세스할 수 있어야 합니다.
설정 스크립트에 대한 제한 사항¶
다음은 설정 스크립트 내에서 수행할 수 없습니다.
ALTER <오브젝트> 명령을 사용하여 LOG_LEVEL, TRACE_LEVEL 또는 METRIC_LEVEL 속성 설정.
EXECUTE AS CALLER 프로시저 생성 또는 호출.
명명된 스테이지에 파일을 포함하기 위해 IMPORT를 사용하는 Snowpark 사용자 정의 함수(UDF) 또는 프로시저 생성.
애플리케이션 패키지에 포함되지 않은 코드를 참조하는 프로시저, 함수 또는 익명 코드 블록 호출.
CREATE FUNCTION 명령 사용 시 명명된 스테이지에서 코드 파일 가져오기.
CALL 을 사용하여 EXECUTE AS CALLER로 실행되는 프로시저 호출.
버전이 지정된 스키마 내에서 생성된 오브젝트에 대한 추가 제한 사항이 있습니다.
설정 스크립트에서 생성된 오브젝트의 표시 유형¶
The setup script can create most types of database-level objects. Database objects created by the setup script are internal to the app. When a consumer installs an app, by default, these objects are invisible and inaccessible to the consumer account directly.
참고
공급자는 애플리케이션 패키지를 테스트할 때 개발 모드를 사용하여 설정 스크립트에 의해 생성된 오브젝트에 액세스할 수 있습니다. 자세한 내용은 개발, 디버그 및 세션 디버그 모드를 사용하여 앱 테스트하기 섹션을 참조하십시오.
A provider can make these objects visible to the consumer using application roles. An application role created within the setup script is automatically granted to the role owning the app. Application roles granted by the setup script cannot be revoked.
Users that have been granted a role that owns the application object can grant application roles to other roles within their account. For example, the setup script can define an application role, such as APP_ADMIN, and this role can grant permission to access objects within the app.
설정 스크립트에 의해 출력되는 메시지의 로그 수준을 설정합니다.¶
공급자는 설정 스크립트가 실행될 때 생성되는 메시지의 로그 수준을 지정할 수 있습니다. 추가 정보는 Snowflake Scripting에서 메시지 로깅하기 섹션을 참조하십시오.
설정 스크립트의 로그 수준을 구성하려면 다음 시스템 함수 중 하나를 사용하십시오.
예를 들어, 오류 메시지를 기록하도록 설정 스크립트를 구성하려면 설정 스크립트 시작 부분에 다음 명령을 추가하십시오.
SYSTEM$LOG('error', 'Error message');
모듈식 설정 스크립트 만들기¶
일반적인 앱의 설정 스크립트는 규모가 크고 복잡할 수 있습니다. 설정 스크립트를 보다 모듈화하고 유지 관리하기 더 쉽게 만들기 위해 공급자는 여러 보조 설정 스크립트를 호출하는 기본 설정 스크립트를 만들 수 있습니다.
예를 들어, 공급자는 오브젝트 생성, 뷰 생성, 저장 프로시저 생성 등 다양한 유형의 작업을 처리하기 위해 다양한 설정 스크립트를 생성할 수 있습니다.
CREATE APPLICATION 명령이 실행되면 매니페스트 파일에 지정된 기본 설정 스크립트가 실행됩니다. 기본 설정 스크립트에서 추가 설정 스크립트를 실행하려면 EXECUTE IMMEDIATE FROM 명령을 사용합니다.
기본 설정 스크립트에 포함된 설정 스크립트는 나타나는 순서대로 실행됩니다. 이러한 보조 설정 스크립트에는 EXECUTE IMMEDIATE FROM 명령의 인스턴스도 포함될 수 있습니다.
앱에 여러 설정 스크립트 추가하기¶
기본 설정 스크립트의 위치를 매니페스트 파일에 추가합니다.
artifacts: ... setup_script: scripts/setup.sql ...
기본 설정 스크립트를 만듭니다.
다음 예에서는 앱의 일반적인 디렉터리 구조를 보여줍니다.
@test.schema1.stage1: └── / ├── manifest.yml ├── readme.md ├── scripts/setup_script.sql여기서
setup_script.sql은 기본 설정 스크립트입니다.보조 설정 스크립트를 만듭니다.
다음 예에서는 여러 설정 스크립트가 포함된 앱의 일반적인 디렉터리 구조를 보여줍니다.
@test.schema1.stage1: └── / ├── manifest.yml ├── readme.md ├── scripts/setup_script.sql ├── scripts/secondary_script.sql ├── scripts/procs/setup_procs.sql ├── scripts/views/setup_views.sql ├── scripts/data/setup_data.sql기본 설정 스크립트 내에서 EXECUTE IMMEDIATE FROM 명령을 사용하여 각 보조 설정 스크립트에 대한 상대 경로를 지정합니다.
... EXECUTE IMMEDIATE FROM 'secondary_script.sql'; EXECUTE IMMEDIATE FROM './procs/setup_procs.sql'; EXECUTE IMMEDIATE FROM '../scripts/views/setup_views.sql'; EXECUTE IMMEDIATE FROM '/scripts/data/setup_data.sql'; ...
EXECUTE IMMEDIATE FROM 명령에 제공된 경로는 대/소문자를 구분하며 모든 설정 스크립트와 함께 사용할 수 있습니다. 앱 루트 디렉터리의 상대 경로를 나타내려면 마침표와 슬래시(
/)를 사용하고, 설정 스크립트의 현재 디렉터리를 나타내려면 마침표와 슬래시(./)를 사용하며, 설정 스크립트의 상위 디렉터리를 나타내려면 마침표 두 개와 슬래시(../)를 사용합니다.기본 설정 스크립트는 매니페스트에 정의된 스크립트입니다. EXECUTE IMMEDIATE FROM 명령은 모든 설정 스크립트와 함께 사용할 수 있습니다.
설정 스크립트에서 EXECUTE IMMEDIATE FROM 사용에 대한 제한 사항¶
설정 스크립트 내에서 EXECUTE IMMEDIATE FROM 을 사용할 때 다음 제한 사항이 적용됩니다.
EXECUTE IMMEDIATE FROM 을 사용하여 호출된 설정 스크립트에서는 이벤트 로깅이 지원되지 않습니다.
컨슈머 계정의 암호화된 외부 스테이지에 저장된 파일에 대한 액세스는 지원되지 않습니다.
앱 런타임 중에는 슬래시(
/)가 있는 상대 경로 형식만 허용됩니다. 예:EXECUTE IMMEDIATE FROM '/scripts/data/setup_data.sql'.
설정 스크립트 생성 시 모범 사례¶
Snowflake에서는 앱에 대한 설정 스크립트를 생성할 때 다음 모범 사례를 권장합니다.
CREATE 문의 멱등성 형식 사용하기¶
설정 스크립트 내에서 CREATE 명령을 사용하여 오브젝트를 만들 때 이러한 명령의 다음 버전을 사용하는 것이 좋습니다.
CREATE OR REPLACE
CREATE IF NOT EXISTS
설치 및 업그레이드 중에 설정 스크립트를 여러 번 실행할 수 있습니다. 오류가 발생하는 경우 이러한 오브젝트가 이미 존재할 수 있으며, 특히 버전이 지정된 스키마 내에서 생성된 경우에 더욱 그러합니다.
오브젝트 생성 시 대상 스키마 포함하기¶
CREATE SCHEMA 명령은 세션 컨텍스트를 변경하지 않습니다. 오브젝트는 생성될 때 대상 스키마로 정규화되어야 합니다. 예를 들어 설정 스크립트 내에서 스키마를 만들려면 다음 명령을 사용하십시오.
CREATE SCHEMA IF NOT EXISTS app_config;
CREATE TABLE IF NOT EXISTS app_config.params(...);
Do not refer to objects in the app from outside the app¶
Do not create objects outside the app that refer to objects within the app. Although the Snowflake Native App Framework does not prohibit creating these objects, it can lead to problems when a consumer installs the Snowflake Native App.
For example, consider the context where a setup script creates a database, schema, and view outside of the app and the view refers to a table within the app. In this context, the view in the database breaks when the consumer takes ownership of the database and drops the app.
이 모범 사례는 설정 스크립트에서 생성된 테이블, 저장 프로시저, 사용자 정의 함수, 참조에 적용됩니다.
버전이 지정된 스키마나 버전이 지정되지 않은 스키마를 사용할 때 발생할 수 있는 오류를 고려하십시오.¶
버전이 지정된 스키마의 오브젝트는 버전이 지정되지 않은 스키마의 오브젝트를 참조할 수 있으며 그 반대의 경우도 마찬가지입니다. 설정 스크립트는 설치 또는 업그레이드 중에 오류가 발생할 경우 발생할 수 있는 상황을 고려해야 합니다. 예를 들어, 공급자는 초기 실행이 실패할 경우 설정 스크립트가 자동으로 다시 실행될 경우 어떤 일이 발생하는지 고려해야 합니다.
예를 들어, 다음을 사용하여 오브젝트를 생성해 보십시오.
CREATE OR REPLACE PROCEDURE app_state.proc()...;
GRANT USAGE ON PROCEDURE app_state.proc()
TO APPLICATION ROLE app_user;
이 예에서 CREATE OR REPLACE 문은 기존 프로시저를 대체하여 이전에 해당 프로시저에 부여된 권한을 암시적으로 제거합니다. 권한 부여는 나중에 스크립트에서 복원될 수 있지만, 스크립트가 실행 시 실패할 경우 컨슈머는 프로시저에 액세스하지 못하게 될 수 있습니다.
재시도로 해결할 수 없는 문제(예: 구문 오류)로 인해 설정 스크립트가 실패할 경우 컨슈머는 앱이 새 버전이나 패치로 업그레이드되고 권한이 복원될 때까지 프로시저에 액세스할 수 없습니다.
조심
이 섹션의 지침은 Snowflake Native App Framework 컨텍스트 외부의 태그, 마스킹 정책, 행 액세스 정책 에는 적용되지 않습니다.
태그 및 정책 할당은 버전이 지정된 스키마의 증분 버전으로 전파되지 않습니다. 다음 시나리오에서는 오류 메시지가 발생합니다(태그를 예로 사용).
버전이 지정된 스키마에서 태그를 생성하고 다른 스키마의 오브젝트에 태그를 할당합니다.
버전이 지정되지 않은 스키마에서 태그를 생성하고 버전이 지정된 스키마의 오브젝트에 태그를 할당합니다.
버전이 지정된 스키마에서 테이블이나 뷰를 생성하고 버전이 지정되지 않은 스키마에 태그가 있는 경우 테이블이나 뷰에 태그를 할당합니다.
버전이 지정되지 않은 스키마에서 테이블이나 뷰를 생성하고 버전이 지정된 스키마에 태그가 있는 경우 테이블이나 뷰에 태그를 할당합니다.
오류 메시지는 다음과 같습니다.
A TAG in a versioned schema can only be assigned to the objects in the same schema. An object in a versioned schema can only have a TAG assigned that is defined in the same schema.
정책 할당으로 인해 오류 메시지가 발생하는 경우 오류 메시지는 TAG 대신 POLICY 를 지정합니다.
오류 메시지를 방지하는 방법은 다음과 같습니다.
Snowflake Native App 공급자가 버전이 지정된 스키마에 태그 또는 태그가 설정된 오브젝트가 포함된 경우 태그와 동일한 스키마 내의 오브젝트에 태그(또는 정책)가 설정되도록 설정 스크립트를 업데이트해야 합니다. 버전이 지정되지 않은 스키마에 태그 또는 태그가 설정된 오브젝트가 포함되어 있는 경우 설정 스크립트를 업데이트할 필요가 없습니다.
If the Snowflake Native App consumer sees this error message when installing an app, the consumer should ask the provider to update their setup script. Additionally, the consumer should not assign any tag that exists in a versioned schema to any object in their account, such as a warehouse, or assign a policy that exists in a versioned schema to a table or column, or assign a policy or tag to an object that exists in a versioned schema inside the Snowflake Native App. If so, Snowflake returns the same error message.
버전이 지정된 스키마 내에서 뷰 정의하기¶
항상 버전이 지정된 스키마에서 공유 콘텐츠에 대한 뷰를 정의하여 업그레이드 중에 뷰에 액세스하는 모든 코드가 일관적인 뷰를 사용하도록 하십시오. 또한 새 열이나 기타 특성을 추가하거나 제거할 때 버전이 지정된 스키마를 사용해야 합니다.
시간이 많이 걸리는 작업이 호환되는지 확인하기¶
설정 스크립트가 큰 상태 테이블 업그레이드와 같이 매우 오랫동안 실행되는 작업을 수행해야 하는 경우 이러한 업데이트가 이전 버전에서 실행 중인 기존 코드와 호환되는지 확인하십시오.
애플리케이션 역할 정보¶
By default the consumer has no privileges on objects created within the app. Even the ACCOUNTADMIN role cannot view the objects within an app. Objects that the app creates outside itself, such as a database, are visible only to the ACCOUNTADMIN role of the consumer account.
Application roles are similar to database roles, but may only be created within the app. Unlike database roles, application roles can be granted privileges on objects that exist outside of the app.
Application roles should be created by the setup script when the app is installed and are automatically granted to the app owner’s role, who then can grant appropriate application roles to other roles in the consumer account.
참고
Application roles are the only type of role that can be created within an app. Database roles, for example, are not permitted within the app.
Likewise, application roles can only be created in an app and not, for example, in a normal database or at the account level.
Any privileges granted to application roles is passed to the app owner, which is the role used to install the app. The owner may further delegate application roles to other roles within the consumer account.
The setup script can also define an application role (e.g. USER). Using this role, consumers are granted access to use the functionality provided by the app. The setup script can define an application role, such as READ_ONLY, to provide restricted access to select areas of data within the app.
Unlike database roles, application roles may also be granted privileges on objects outside of the installed app. They may therefore be used to grant privileges on objects outside of the app. However, the application role itself must be created within the app.
애플리케이션 역할로 작업하기 위해 지원되는 SQL 명령¶
Snowflake Native App Framework 는 애플리케이션 역할로 작업하기 위해 다음 SQL 명령을 제공합니다.
설정 스크립트에서 애플리케이션 역할 사용하기¶
Application roles defined in the setup script are automatically granted to the role owning the app instance. When the app is installed, the role used to install the app is the owner of the app. However, the app owner can grant privileges to other account roles in the consumer account.
Application roles allow privileges on objects within the app to be granted to the consumer. For example:
CREATE APPLICATION ROLE admin;
CREATE APPLICATION ROLE user;
GRANT APPLICATION ROLE user TO APPLICATION ROLE admin;
CREATE OR ALTER VERSIONED SCHEMA app_code;
GRANT USAGE ON SCHEMA app_code TO APPLICATION ROLE admin;
GRANT USAGE ON SCHEMA app_code TO APPLICATION ROLE user;
CREATE OR REPLACE PROCEDURE app_code.config_app(...)
GRANT USAGE ON PROCEDURE app_code.config_app(..)
TO APPLICATION ROLE admin;
CREATE OR REPLACE FUNCTION app_code.add(x INT, y INT)
GRANT USAGE ON FUNCTION app_code.add(INT, INT)
TO APPLICATION ROLE admin;
GRANT USAGE ON FUNCTION app_code.add(INT, INT)
TO APPLICATION ROLE user;
In this example, the setup script creates application roles named admin and a user. The setup
script then grants both application roles access to the schema containing the app code. It also grants
access to the add function within the schema. The admin role is also granted access to the
config_app procedure.
애플리케이션 역할 및 버전¶
Application roles are not versioned. This means that dropping an application role or revoking a permission from an object that is not in a versioned schema can impact the current version of an application or the version being upgraded. Application roles may only be safely dropped when you have dropped all versions of the app that use those roles.
참고
애플리케이션 역할에는 오브젝트의 소유권을 부여할 수 없습니다. 즉, 설정 스크립트에 정의된 애플리케이션 역할은 컨슈머가 설치된 Snowflake Native App 내의 오브젝트에 액세스할 수 있도록 허용할 목적으로만 사용해야 한다는 뜻입니다.