How to Version Control Snowflake Queries

Galaxy Glossary

How do I version control Snowflake queries?

Keep Snowflake SQL objects in Git and track every change with repeatable, idempotent scripts.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why should you version control Snowflake queries?

Version control prevents silent changes, enables code review, and lets teams roll back broken logic quickly. Treat SQL like application code for faster, safer releases.

What objects belong in Git?

Store CREATE OR REPLACE statements for tables, views, stages, procedures, and user-defined functions. Exclude transient data loads and one-off ad-hoc queries.

How do you structure a Snowflake repo?

Organize scripts by object type (/tables, /views, /procedures). Prefix files with incremental numbers so CI runs them in order (001_create_customers.sql, 002_create_orders.sql).

Which CI/CD tool can apply changes?

Use GitHub Actions, GitLab CI, or CircleCI. Configure a job that runs the SnowSQL CLI with your connection secrets to execute every changed script in pull requests and on merge to main.

CI job example

# .github/workflows/snowflake.yml
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install SnowSQL
run: sudo apt-get install -y snowflake-cli
- name: Run SQL scripts
env:
SNOWSQL_ACCOUNT: ${{ secrets.SF_ACCOUNT }}
SNOWSQL_USER: ${{ secrets.SF_USER }}
SNOWSQL_PWD: ${{ secrets.SF_PASSWORD }}
run: |
for file in $(git diff --name-only $GITHUB_SHA $GITHUB_BASE_SHA | grep .sql); do
snowsql -f $file
done

How do you add version numbers inside Snowflake?

Add a comment header to each object. Example: CREATE OR REPLACE VIEW analytics.v_customer_orders /* v1.3 */ AS .... Query SHOW VIEWS to audit versions.

Best practices to remember

Keep scripts idempotent (CREATE OR REPLACE). Store sample data loads separately. Enforce reviews before merging to main. Tag releases in Git to match production deploys.

Common mistakes

Editing objects directly in the Snowflake UI (bypasses review) and committing compiled objects (like DESCRIBE output) cause merge conflicts.

Why How to Version Control Snowflake Queries is important

How to Version Control Snowflake Queries Example Usage


-- Track a change: add stock column to the view
/* Version: v1.1 */
CREATE OR REPLACE VIEW analytics.v_customer_orders AS
SELECT  c.id          AS customer_id,
        c.name        AS customer_name,
        c.email       AS customer_email,
        o.id          AS order_id,
        o.order_date,
        o.total_amount,
        SUM(oi.quantity * p.price) AS line_total,
        p.stock
FROM    Customers   c
JOIN    Orders      o  ON o.customer_id = c.id
JOIN    OrderItems  oi ON oi.order_id   = o.id
JOIN    Products    p  ON p.id          = oi.product_id
GROUP BY c.id, c.name, c.email, o.id, o.order_date, o.total_amount, p.stock;

How to Version Control Snowflake Queries Syntax


-- Versioned view script example
/* File: 003_create_view_customer_orders.sql */
/* Version: v1.0 */
CREATE OR REPLACE VIEW analytics.v_customer_orders AS
SELECT  c.id          AS customer_id,
        c.name        AS customer_name,
        c.email       AS customer_email,
        o.id          AS order_id,
        o.order_date,
        o.total_amount,
        SUM(oi.quantity * p.price) AS line_total
FROM    Customers   c
JOIN    Orders      o  ON o.customer_id = c.id
JOIN    OrderItems  oi ON oi.order_id   = o.id
JOIN    Products    p  ON p.id          = oi.product_id
GROUP BY c.id, c.name, c.email, o.id, o.order_date, o.total_amount;

Common Mistakes

Frequently Asked Questions (FAQs)

Can I use Liquibase or Flyway with Snowflake?

Yes. Both tools support Snowflake and manage migrations, checksums, and rollbacks automatically. Point them at your .sql files and configure Snowflake drivers.

How do I roll back a bad change?

Create a new script that restores the previous DDL or reverts data fixes. Tag the rollback commit and redeploy via CI.

Is version control needed for ad-hoc reports?

Less critical, but saving repeatable ad-hoc analysis in a "sandbox" folder promotes reuse and transparency.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.