Working with Git repositories

This topic describes how to perform common repository operations using SQL commands and Snowsight.

您还可以将以下功能与 Git 一起使用,每种功能都有自己执行 Git 操作的方式:

从远程 Git 存储库中提取

您可以在 Snowflake 中将远程存储库的所有分支、标签和提交提取到 Git 存储库克隆中。执行此操作时,您还会削减那些较早提取但远程存储库中已不再存在的分支和提交。

To perform the operations described in this section, you’ll need the Snowflake access described in Access control for ALTER GIT REPOSITORY.

You can fetch from the remote Git repository using either Snowsight or SQL.

You can fetch from the remote Git repository to the Git repository clone in Snowflake by using the ALTER GIT REPOSITORY command.

以下示例中的代码将使用存储库的内容更新 Git 存储库克隆:

ALTER GIT REPOSITORY snowflake_extensions FETCH;

查看存储库分支或标签的列表

您可以查看从远程存储库提取的 Snowflake Git 存储库克隆中可用的分支和标签列表。

要执行本部分中介绍的操作,您需要以下主题中介绍的 Snowflake 访问权限:

You can view a list of branches or tags using either Snowsight or SQL.

You can view branches and tags by using the SHOW GIT BRANCHES and SHOW GIT TAGS commands.

The following example generates output that lists branches in the Git repository snowflake_extensions:

SHOW GIT BRANCHES IN snowflake_extensions;

如前所述的命令会生成类似下面的输出:

--------------------------------------------------------------------------------
| name | path           | checkouts | commit_hash                              |
--------------------------------------------------------------------------------
| main | /branches/main |           | 0f81b1487dfc822df9f73ac6b3096b9ea9e42d69 |
--------------------------------------------------------------------------------

查看存储库文件列表

You can view a list of files in a branch, tag, or commit using either Snowsight or SQL.

You can view a list of files in the repository by using the LIST command in the following forms, specifying the Git repository clone as you would a stage (you can abbreviate LIST to LS):

  • 按分支名称列出:

    LS @repository_name/branches/branch_name;
  • 按标签名称列出:

    LS @repository_name/tags/tag_name;
  • 按提交哈希列出:

    LS @repository_name/commits/commit_hash;

The following example generates output that lists files in the main branch of the Git repository snowflake_extensions:

LS @snowflake_extensions/branches/main;

The preceding command generates output similar to the following. For descriptions of the output columns, see the LIST command reference.

-------------------------------------------------------------------------------------------------------------------------------------------------------
| name                                                         | size | md5 | sha1                                     | last_modified                |
-------------------------------------------------------------------------------------------------------------------------------------------------------
| snowflake_extensions/branches/main/.gitignore                | 10   |     | e43b0f988953ae3a84b00331d0ccf5f7d51cb3cf | Wed, 5 Jul 2023 22:42:34 GMT |
-------------------------------------------------------------------------------------------------------------------------------------------------------
| snowflake_extensions/branches/main/python-handlers/filter.py | 169  |     | c717137b18d7b75005849d76d89037fafc7b5223 | Wed, 5 Jul 2023 22:42:34 GMT |
-------------------------------------------------------------------------------------------------------------------------------------------------------

执行存储库中的代码

可以执行存储库中的文件所包含的代码。

To perform the operations described in this section, you’ll need the Snowflake access described in Access control for EXECUTE IMMEDIATE FROM.

You can execute code by using either Snowsight or SQL.

You can use EXECUTE IMMEDIATE FROM to execute code in a Git repository clone.

Code in the following example executes code in create-database.sql from the Git repository clone snowflake_extensions:

EXECUTE IMMEDIATE FROM @snowflake_extensions/branches/main/sql/create-database.sql;

将基于存储库的代码复制到工作表中

您可以将代码从存储库文件快速复制到工作表中。您可以编辑和运行复制的代码,或者将其作为其他用户的只读模板。

You can copy the content of the following types of files: .sql and .py.

要将更改保存在存储库中,您需要将工作表中的编辑代码复制到本地 Git 存储库中的文件(例如,与从中复制的文件对应的文件)中,然后从那里提交更改。

Snowsight:

You can use Snowsight to copy content from a file in your repository into a worksheet.

  1. Sign in to Snowsight.

  2. In the navigation menu, select Catalog » Database Explorer.

  3. 在对象资源管理器中,选择包含要查看的 Git 存储库克隆的数据库和架构。

  4. Inside the schema, open Git Repositories.

  5. Inside Git Repositories, select a repository to view its details page.

  6. In the repository’s details page, on the Files Explorer tab, select the Branch button.

  7. From the Branch drop-down menu, do one of the following:

    • To view a list of branches cloned from the repository, select Branches.
    • To view a list of the tags cloned from the repository, select Tags.
  8. 选择包含您要复制的代码所属文件的分支或标签。

  9. 在存储库名称下,选择包含要执行的文件的文件夹。

  10. Locate the file whose code you want to execute, and select More options » Copy into worksheet.

Snowflake 将所选文件中的代码复制到新工作表中。

查看 Git 存储库克隆属性

您可以在 Snowflake 中查看与 Git 存储库克隆相关的属性。

To perform the operations described in this section, you’ll need the Snowflake access described in Access control for DESC GIT REPOSITORY.

You can view Git repository clone properties by using either Snowsight or SQL.

You can view Git repository clone properties by using the SQL commands SHOW GIT REPOSITORIES and DESCRIBE GIT REPOSITORY.

The properties information includes the Git origin URL, name of the API integration and credentials (specified as a secret) used to connect with the remote repository, and so on.

DESCRIBE GIT REPOSITORY snowflake_extensions;

如前所述的命令会生成类似下面的输出:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| CREATED_ON                    | NAME                 | DATABASE_NAME | SCHEMA_NAME | ORIGIN                                                 | API_INTEGRATION     | GIT_CREDENTIALS           | OWNER        | OWNER_ROLE_TYPE | COMMENT |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 2023-06-28 08:46:10.886 -0700 | SNOWFLAKE_EXTENSIONS | MY_DB         | MAIN        | https://github.com/my-account/snowflake-extensions.git | GIT_API_INTEGRATION | MY_DB.MAIN.GIT_SECRET     | ACCOUNTADMIN | ROLE            |         |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------