Data Engineering & Lake

Jun 2, 2024

Snowflake Database Change Management (+Git Integration)

Quick Reference: + Snowflake Database Change Management (+SQL Templates, +Jinja)

https://tutorials.demohub.dev/demo/snowflake-database-change-management-(-sql-templates-jinja) 

**********

Version control isn't just for software development anymore. With Snowflake's Git integration, you can apply the same powerful principles of tracking changes, collaboration, and rollback to your database objects like tables, views, stored procedures, and user-defined functions (UDFs). This fosters a more organized, reliable, and efficient data development process.


Why Git Integration Matters

Here's why integrating your Snowflake objects with Git is a game-changer:

  • Version History: Track every change made to your database objects, making it easy to understand the evolution of your data environment.

  • Collaboration: Enable multiple team members to work on database objects simultaneously, with features like branching and merging.

  • Rollback and Recovery: Revert to previous versions if errors occur or unwanted changes are made.

  • Auditing and Compliance: Maintain a clear audit trail of modifications, ensuring regulatory compliance.

  • Continuous Integration/Continuous Delivery (CI/CD): Automate testing and deployment of database changes for a streamlined workflow.

Hands-On with Snowflake and Git

Let's walk through a simple example of how to integrate Snowflake with Git.

Prerequisites:

  • A Snowflake account.

  • A Git repository (e.g., GitHub, GitLab, Bitbucket).

  • Snowflake Snowsight or SnowSQL.

Setting up Git Integration in Snowflake

1. Secure Your Credentials (Optional):

If your Git repository requires authentication, store your credentials securely within Snowflake:

SQL

-- Create a Secret to store your Git credentials (username and password)
CREATE OR REPLACE SECRET GIT_SECRET
    TYPE = password
    USERNAME = 'yourname'  -- Replace with your Git username
    PASSWORD = 'yourcredentials';  -- Replace with your Git password or personal access token

2. Establish the API Connection:

Create an API integration to facilitate communication between Snowflake and your Git provider:

>_ SQL

-- Create API integration for Git HTTPS access
CREATE OR REPLACE API INTEGRATION GIT_API_INTEGRATION
    API_PROVIDER = git_https_api
    API_ALLOWED_PREFIXES = ('https://github.com/frulouis')  -- Replace with your Git repository URL
    ALLOWED_AUTHENTICATION_SECRETS = (GIT_SECRET)  -- Use the created secret
    ENABLED = TRUE

3. Create a Repository Stage:

Link your Snowflake environment to your Git repository:

>_ SQL

-- Create Git Repository using your Git URL
CREATE GIT REPOSITORY Tutorials_DemoHub_Git_Repo
    ORIGIN = 'https://github.com/frulouis/Tutorials-DemoHub.git' -- Replace with your Git repository URL
    API_INTEGRATION = 'GIT_API_INTEGRATION'
    GIT_CREDENTIALS = 'DEMODB.PUBLIC.GIT_SECRET'
    COMMENT = 'Git Integration for Tutorials - DemoHub Repository'

Managing Database Objects with Git

Now that your Snowflake environment is linked to Git, you can manage database objects more effectively:

1. Inspect Repository Details:

Use DESCRIBE to view the properties of your repository stage:

>_ SQL

-- View repository stage properties
DESCRIBE

2. Refresh and Sync:

Use ALTER GIT REPOSITORY ... FETCH to pull the latest changes from your Git repository:

>_ SQL

-- Refresh repository stage to get the latest changes from Git
ALTER GIT REPOSITORY Tutorials_DemoHub_Git_Repo FETCH

3. Explore Repository Contents:

Use the SHOW GIT BRANCHES command to list repository branches or tags, and LS to view repository files:

>_ SQL

-- Show Git branches in the repository
SHOW GIT BRANCHES IN Tutorials_DemoHub_Git_Repo;

-- List files in the repository



4. Executing SQL Scripts from Git:

Utilize the EXECUTE IMMEDIATE FROM command to run SQL scripts stored in your Git repository:

>_ SQL

-- Execute a SQL script from the repository
EXECUTE IMMEDIATE FROM @tutorials_demohub_git_repo/branches/main/DCM/OrdersDB_DDL.sql

Git Integration Made Easy with Snowsight

While the command-line approach offers flexibility and control, Snowflake understands the value of visual interfaces. Snowsight, Snowflake's web-based user interface, makes Git integration even more accessible with its intuitive features.

Using Snowsight for Git Integration

Here's a quick overview of how to leverage Snowsight for Git integration:

  • Connecting to Your Repository: In Snowsight, navigate to the "Databases" section and select the database where you want to create your repository stage. Under "Create," choose "Git Repository." You'll be prompted to enter your repository's origin URL and, if needed, authentication credentials. Snowsight handles the creation of the API integration and secret for you.



  • Managing Objects: Once connected, you can view the files within your Git repository directly in Snowsight. You can easily create new SQL files or modify existing ones, just like working with regular files.



  • Refreshing and Fetching: Snowsight allows you to manually refresh your repository stage to pull the latest changes from your Git repository. You can also configure automatic fetching at regular intervals to keep your Snowflake objects in sync.



  • Versioning and Collaboration: Snowsight provides a visual representation of your Git commit history, making it easy to see the changes that have been made over time. You can even compare different versions of your objects to understand the evolution of your database.

Going Beyond the Basics: Best Practices

Snowflake's Git integration is a powerful tool, and here are some best practices to maximize its benefits:

  • Automating with Snowflake Tasks: To take your efficiency to the next level, you can create Snowflake Tasks to automate the process of fetching new files from your Git repository and executing any associated SQL scripts. This automation eliminates manual intervention, ensuring your database objects are always up-to-date and reducing the risk of errors from manual updates. It's a simple yet powerful way to enhance the efficiency and reliability of your data development process.

  • Branching Strategy: Adopt a clear branching strategy (e.g., feature branches, release branches) to organize your work and isolate changes.

  • Testing and Validation: Implement rigorous testing for your database objects to ensure changes function as expected.

  • CI/CD Pipelines: Automate your testing and deployment processes using CI/CD tools for a more efficient workflow.

  • Documentation: Maintain comprehensive documentation of your database objects and changes for better collaboration and understanding.

Resources

By combining the power of Snowflake's Git integration with the convenience of Snowsight, you can easily establish a robust version control system for your database objects, promoting collaboration, reliability, and efficiency in your data development workflow.

Join Our Community

Join our newsletter list for occasional updates, products and insights.

Join Our Community

Join our newsletter list for occasional updates, products and insights.

Join Our Community

Join our newsletter list for occasional updates, products and insights.

More from
Data Engineering & Lake

Snowflake Dynamic Tables

Snowflake Dynamic Tables

Snowflake Dynamic Tables

Snowflake Database Change Management (+Git Integration)

Snowflake Database Change Management (+Git Integration)

Snowflake Database Change Management (+Git Integration)

Snowflake Database Change Management (+SQL Templates, +Jinja)

Snowflake Database Change Management (+SQL Templates, +Jinja)

Snowflake Database Change Management (+SQL Templates, +Jinja)

Copyright 2020-24 - All Right Reserved

Copyright 2020-24 - All Right Reserved

Copyright 2020-24 - All Right Reserved