sqitch...wflake(3pm)

โœจ NAME โœจ

sqitchtutorial-snowflake โ€” a lil' tutorial introduction to Sqitch change management on Snowflake, straight out of the User Contributed Perl Documentation section (3pm). Bestie, this is the glow-up guide. ๐Ÿ’–

๐ŸŒธ SYNOPSIS ๐ŸŒธ

sqitch *

๐Ÿ’… DESCRIPTION ๐Ÿ’…

Okay so this whole thing is gonna walk you through how to make a sqitch-enabled Snowflake project, how to use a VCS for deployment planning, and how to work with your other devs so everybody's changes stay in sync and in the right order. No chaos, only choreography. โœจ

We're gonna start from literal scratch with a new project โ€” a fictional antisocial networking site called Flipr. Every example here uses Git (<https://git-scm.com/>) as the VCS and Snowflake (<https://www.snowflake.net/>) as the storage engine, but honestly for the most part you can swap in other VCSes and database engines wherever it makes sense. She's flexible like that. ๐Ÿฆ‹

If you wanna manage a database with a different engine, go peek at one of these other tutorials, they're all iconic in their own way:

  • PostgreSQL, Yugabyte, and CockroachDB Tutorial
  • SQLite Tutorial
  • Oracle Tutorial
  • MySQL and MariaDB Tutorial
  • Firebird Tutorial
  • Vertica Tutorial
  • Exasol Tutorial
  • ClickHouse Tutorial

๐Ÿ”Œ Connection Configuration ๐Ÿ”Œ

Sqitch needs ODBC to talk to Snowflake. Periodt. So you gotta make sure the Snowflake ODBC driver (<https://docs.snowflake.com/en/user-guide/odbc.html>) is installed and properly configured โ€” no half-done setups, we don't do that here. ๐Ÿ’€

At its simplest, on Unix-like systems, you name the driver Snowflake by adding this entry to odbcinst.ini (usually hanging out in /etc, /usr/etc, or /usr/local/etc):

[Snowflake]
Description = ODBC for Snowflake
Driver      = /usr/lib64/snowflake/odbc/lib/libSnowflake.so

Heads up bestie: you'll need to adjust that path depending on which version of the ODBC driver you've got and where you installed it. ๐Ÿ™ˆ

For the full tea on downloading, installing, and configuring ODBC for your platform, see the Snowflake ODBC documentation (<https://docs.snowflake.com/en/user-guide/odbc.html>).

๐ŸŽ€ Starting a New Project ๐ŸŽ€

Usually the very first thing you do when starting a new project is make a source code repository. Icons only. So let's do that with Git:

> mkdir flipr
> cd flipr
> git init .
Initialized empty Git repository in /flipr/.git/
> touch README.md
> git add .
> git commit -am 'Initialize project, add README.'

If you're a Git user and you wanna follow along with the history, the repository used in these examples lives on GitHub (<https://github.com/sqitchers/sqitch-snowflake-intro>). ๐Ÿซถ

So now that we have a repo, let's get into Sqitch itself. Every Sqitch project must have a name attached to it, and optionally a unique URI. And like โ€” we recommend including the URI, because it makes your object identifiers more unique internally, and it prevents you from deploying a totally different project that happens to have the same name. Vibe check: passed. So let's specify one when we initialize Sqitch:

> sqitch init flipr --uri https://github.com/sqitchers/sqitch-snowflake-intro/ --engine snowflake
Created sqitch.conf
Created sqitch.plan
Created deploy/
Created revert/
Created verify/

Let's have a little look-see at sqitch.conf:

> cat sqitch.conf
[core]
  engine = snowflake
  # plan_file = sqitch.plan
  # top_dir = .
# [engine "snowflake"]
  # target = db:snowflake:
  # registry = sqitch
  # client = snowsql

Slay. It picked up on the fact that we're making changes for the Snowflake engine โ€” thank you --engine snowflake โ€” and saved it to the file. And it even wrote out a commented-out [engine "snowflake"] section with all the available Snowflake engine-specific settings commented out, just sitting there ready to be edited whenever you feel like it. So considerate. ๐Ÿ˜ญ

By default, Sqitch reads sqitch.conf in the current directory for settings. But it also reads ~/.sqitch/sqitch.conf for user-specific settings. Since Snowflake's snowsql client is not in the path on my system, let's go ahead and tell it where to find the client on our computer:

Don't even bother with this next bit if you're using the Docker image (<https://hub.docker.com/r/sqitch/sqitch/>), because it uses the client inside the container, not on your host machine. ๐Ÿ’…
> sqitch config --user engine.snowflake.client /Applications/SnowSQL.app/Contents/MacOS/snowsql

And let's also tell it who we are, since this data is gonna be used in all of our projects:

> sqitch config --user user.name 'Marge N. Oโ€™Vera'
> sqitch config --user user.email 'marge@example.com'

Take a peek at ~/.sqitch/sqitch.conf and you'll see this:

> cat ~/.sqitch/sqitch.conf
[engine "snowflake"]
  client = /Applications/SnowSQL.app/Contents/MacOS/snowsql
[user]
  name = Marge N. Oโ€™Vera
  email = marge@example.com

Which means Sqitch should be able to find snowsql for any project, and it will always properly identify us when we're planning and committing changes. Main character energy, honestly. ๐Ÿ‘‘

Back to the repository. Have a look at the plan file, sqitch.plan:

> cat sqitch.plan
%syntax-version=1.0.0
%project=flipr
%uri=https://github.com/sqitchers/sqitch-snowflake-intro/

Note that it picked up on the name and URI of the app we're building. Sqitch uses this data to manage cross-project dependencies. And the %syntax-version pragma? That's always set by Sqitch, so it always knows how to parse the plan, even if the format changes in the future. Future-proofing, we love to see it. โญ

Let's commit these changes and start creating the database changes.

> git add .
> git commit -am 'Initialize Sqitch configuration.'
[main b731cc3] Initialize Sqitch configuration.
 2 files changed, 15 insertions(+)
 create mode 100644 sqitch.conf
 create mode 100644 sqitch.plan

๐Ÿฆ‹ Our First Change ๐Ÿฆ‹

First, our project is gonna need a schema. This creates a nice lil' namespace for all of the objects that will be part of the flipr app. Run this command:

> sqitch add appschema -n 'Add schema for all flipr objects.'
Created deploy/appschema.sql
Created revert/appschema.sql
Created verify/appschema.sql
Added "appschema" to sqitch.plan

The add command adds a database change to the plan and writes the deploy, revert, and verify scripts that represent the change. Now we edit those files. The deploy script's job is to create the schema, so we add this to deploy/appschema.sql:

CREATE SCHEMA flipr;

The revert script's job is to precisely revert the change that the deploy script made, so we add this to revert/appschema.sql:

DROP SCHEMA flipr;

Now we can try deploying this change. We tell Sqitch where to send the change via a database URI (<https://github.com/libwww-perl/uri-db/>). Let's say we're using the account name example, username movera, database flipr, and warehouse sqitch, plus an ODBC driver named Snowflake (see "Connection Configuration" above for the deets). The URI would be structured like this:

db:snowflake://movera@example/flipr?Driver=Snowflake;warehouse=sqitch

A few notes on the URL format, bestie, don't skip these:

db:snowflake://movera@example/flipr?Driver=Snowflake;warehouse=%22sqitch.dev%22&role=%22app.sqitch%22

Of course that can be super tricky to use on the command line, so there are also environment variables that can simplify things:

export SNOWSQL_ROLE='"app.sqitch"'
export SNOWSQL_WAREHOUSE='"sqitch.dev"'

Back to the change. We just tell Sqitch to use that URI to deploy it:

> sqitch deploy 'db:snowflake://movera@example/flipr?Driver=Snowflake'
Adding registry tables to db:snowflake://movera@example/flipr?Driver=Snowflake
Deploying changes to db:snowflake://movera@example/flipr?Driver=Snowflake
  + appschema .. ok

First, Sqitch created the registry tables used to track database changes. The structure and name of the registry varies between databases (Snowflake uses a schema to namespace its registry, while SQLite and MySQL use separate databases). Next, Sqitch deploys changes. We only have one so far, and that + reinforces the idea that the change is being added to the database. ๐ŸŽ€

Okay so this process can take quite a bit of time. Sqitch connects to the database via ODBC and retains the connection throughout, but the creation of the registry and all change scripts run through individual runs of snowsql. These connections can be quite slow. So if Sqitch seems hung, just wait โ€” it's most likely just waiting on Snowflake. Breathe, babe. ๐Ÿง˜โ€โ™€๏ธ

With this change deployed, if you connect to the database, you'll be able to see the schema:

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW TERSE SCHEMAS LIKE 'flipr'"
+-------------------------------+-------+------+---------------+-------------+
| created_on                    | name  | kind | database_name | schema_name |
|-------------------------------+-------+------+---------------+-------------|
| 2018-07-27 14:47:22.614 +0000 | FLIPR | NULL | DWHEELER      | NULL        |
+-------------------------------+-------+------+---------------+-------------+
1 Row(s) produced. Time Elapsed: 0.283s

๐Ÿ” Trust, But Verify ๐Ÿ”

But bestie, that's way too much work. Do you really wanna do something like that after every single deploy? Be honest. ๐Ÿ˜ญ

This is where the verify script comes in. Its job is to test that the deploy did what it was supposed to. It should do that without any regard to whatever data might be in the database, and it should throw an error if the deploy was not successful. In Snowflake, the simplest way to do that for a schema is probably to just create an object in the schema. So put this SQL into verify/appschema.sql:

CREATE TEMPORARY TABLE flipr.verify__ (id INT);

In truth, you can use any query that generates an SQL error if the schema doesn't exist. Another handy way to do that is to divide by zero if an object doesn't exist. For example, to throw an error when the flipr schema does not exist, you could do something like this:

USE WAREHOUSE &warehouse;
SELECT 1/COUNT(*) FROM information_schema.schemata WHERE schema_name = 'FLIPR';

Okay so, real talk: that USE WAREHOUSE statement? Not just for the vibes, bestie โ€” it ships in the default Snowflake change script templates. Why do we care? Because any script running queries that need actual compute resources (we're talking DML and SELECT statements, the hungry ones) has to point at a virtual warehouse (that's the link, go look). This statement just says "hey, use whatever warehouse Sqitch itself is already using for its registry" โ€” which is honestly a super reasonable default, since Sqitch is literally already living there. Want a different warehouse? Change it, it's your world. And if you don't? Sqitch always sets this variable (plus &registry, which holds the name of the Sqitch registry schema) for every single deploy, revert, and verify script execution. No cap. ๐Ÿ’…

Now let's run the verify script with the verify command:

> sqitch verify 'db:snowflake://movera@example/flipr?Driver=Snowflake'
Verifying db:snowflake://movera@example/flipr?Driver=Snowflake
  * appschema .. ok
Verify successful

Slay! It looks good! But obsessed as we are with correctness, maybe you wanna double-check that your verify script actually dies when the schema isn't there. Do a lil test: temporarily change the schema name in the script to something that does not exist, like:

CREATE TEMPORARY TABLE nonesuch.verify__ (id INT);

Then verify again:

> sqitch verify 'db:snowflake://movera@example/flipr?Driver=Snowflake'
Verifying db:snowflake://movera@example/flipr?Driver=Snowflake
* appschema ..
002003 (02000): SQL compilation error:
Schema 'FLIPR.NONESUCH' does not exist.
# Verify script "verify/appschema.sql" failed.
not ok

Verify Summary Report
---------------------
Changes: 1
Errors:  1
Verify failed

The way it's even nice enough to tell us exactly what the problem is?? ๐Ÿ˜ญ Iconic behavior fr fr.

Or, if you're doing the divide-by-zero example instead, change the schema name:

USE WAREHOUSE &warehouse;
SELECT 1/COUNT(*) FROM information_schema.schemata WHERE schema_name = 'NONESUCH';

Then the verify will look something like:

> sqitch verify 'db:snowflake://movera@example/flipr?Driver=Snowflake'
Verifying db:snowflake://movera@example/flipr?Driver=Snowflake
* appschema ..
100051 (22012): Division by zero
# Verify script "verify/appschema.sql" failed.
not ok

Verify Summary Report
---------------------
Changes: 1
Errors:  1
Verify failed

Honestly? Less useful error output. But it's enough to alert us that something has gone wrong, and that's the whole point. ๐Ÿซถ

Careful, bestie!! Don't forget to change the schema name back before continuing! ๐Ÿšจ

โœจ Status, Revert, Log, Repeat โœจ

Purely for informational purposes (no tech, just gossip), we can always see how a deployment was recorded via the status command โ€” which reads the registry tables straight out of the database:

> sqitch status 'db:snowflake://movera@example/flipr?Driver=Snowflake'
# On database db:snowflake://movera@example/flipr?Driver=Snowflake
# Project:  flipr
# Change:   5a2ac4ae6801bfe392483ee5912b4e3592cdd57a
# Name:     appschema
# Deployed: 2018-07-27 10:47:23 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

Now let's make sure we can revert the change:

> sqitch revert 'db:snowflake://movera@example/flipr?Driver=Snowflake'
Revert all changes from db:snowflake://movera@example/flipr?Driver=Snowflake? [Yes]
  - appschema .. ok

See how revert prompts first to make sure we really wanna do this? That's to prevent unnecessary accidents, and we love a safety feature. ๐Ÿ˜Œ You can pass the -y option to shut the prompt up if you're feeling bold. Also peep the - before the change name in the output โ€” it's reinforcing that the change is being removed from the database. And now the schema should be gone:

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW TERSE SCHEMAS LIKE 'flipr'"
+------------+------+------+---------------+-------------+
| created_on | name | kind | database_name | schema_name |
|------------+------+------+---------------+-------------|
+------------+------+------+---------------+-------------+
0 Row(s) produced. Time Elapsed: 0.204s

And the status message should reflect as much:

> sqitch status 'db:snowflake://movera@example/flipr?Driver=Snowflake'
# On database db:snowflake://movera@example/flipr?Driver=Snowflake
No changes deployed

And obviously, since nothing is deployed, the verify command has nothing to verify:

> sqitch verify 'db:snowflake://movera@example/flipr?Driver=Snowflake'
Verifying db:snowflake://movera@example/flipr?Driver=Snowflake
No changes deployed

But bestie โ€” we still have a record that the change happened, and you can see it via the log command:

> sqitch log 'db:snowflake://movera@example/flipr?Driver=Snowflake'
On database db:snowflake://movera@example/flipr?Driver=Snowflake
Revert 5a2ac4ae6801bfe392483ee5912b4e3592cdd57a
Name:      appschema
Committer: Marge N. Oโ€™Vera <marge@example.com>
Date:      2018-07-27 10:48:48 -0400

    Add schema for all flipr objects.

Deploy 5a2ac4ae6801bfe392483ee5912b4e3592cdd57a
Name:      appschema
Committer: Marge N. Oโ€™Vera <marge@example.com>
Date:      2018-07-27 10:47:24 -0400

    Add schema for all flipr objects.

Notice how the actions we took show up in reverse chronological order โ€” the revert first, then the deploy. It's giving time traveler. โณ

Cool. Now let's commit it.

> git add .
> git commit -m 'Add flipr schema.'
[main 7fd5ace] Add flipr schema.
4 files changed, 10 insertions(+)
create mode 100644 deploy/appschema.sql
create mode 100644 revert/appschema.sql
create mode 100644 verify/appschema.sql

And then deploy again. This time, let's flex the --verify option, so that the verify script gets applied when the change is deployed:

> sqitch deploy --verify 'db:snowflake://movera@example/flipr?Driver=Snowflake'
Deploying changes to db:snowflake://movera@example/flipr?Driver=Snowflake
  + appschema .. ok

And now the schema should be back, baby:

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW TERSE SCHEMAS LIKE 'flipr'"
+-------------------------------+-------+------+---------------+-------------+
| created_on                    | name  | kind | database_name | schema_name |
|-------------------------------+-------+------+---------------+-------------|
| 2018-07-27 14:52:50.116 +0000 | FLIPR | NULL | DWHEELER      | NULL        |
+-------------------------------+-------+------+---------------+-------------+
1 Row(s) produced. Time Elapsed: 0.283s

And when we look at the status, the deployment will be there:

> sqitch status 'db:snowflake://movera@example/flipr?Driver=Snowflake'
# On database db:snowflake://movera@example/flipr?Driver=Snowflake
# Project:  flipr
# Change:   5a2ac4ae6801bfe392483ee5912b4e3592cdd57a
# Name:     appschema
# Deployed: 2018-07-27 10:52:54 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

๐ŸŽฏ On Target ๐ŸŽฏ

Okay ngl, I'm getting a little tired of always having to type out db:snowflake://movera@example/flipr?Driver=Snowflake. Aren't you? ๐Ÿ˜ญ That database connection URI (the link, right there) tells Sqitch how to connect to the deployment target, but we do not have to keep using the URI. We can name the target:

> sqitch target add flipr_test 'db:snowflake://movera@example/flipr?Driver=Snowflake'

The target command โ€” inspired by git-remote (link over there) โ€” lets you manage one or more named deployment targets. We just added a target called flipr_test, which means we can now use the string flipr_test instead of the whole URI. And since we're doing so much testing anyway, we can also tell Sqitch to deploy to the flipr_test target by default:

> sqitch engine add snowflake flipr_test

Now we can omit the target argument altogether โ€” unless we need to deploy to another database. Which we will, eventually. But at least our examples get simpler from here on out, e.g.:

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   5a2ac4ae6801bfe392483ee5912b4e3592cdd57a
# Name:     appschema
# Deployed: 2018-07-27 10:52:54 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

Yayyy, things are a little more concise now. ๐ŸŽ€ Let's also make sure that changes get verified after we deploy them:

> sqitch config --bool deploy.verify true
> sqitch config --bool rebase.verify true

We'll meet the rebase command a bit later, don't worry. In the meantime, let's commit the new configuration and make some more changes!

> git commit -am 'Set default deployment target and always verify.'
[main 3834a8d] Set default deployment target and always verify.
 1 files changed, 8 insertions(+), 0 deletions(-)

๐Ÿš€ Deploy with Dependency

Ok bestie, next up: let's add another change, and this one is gonna create a whole table. Our app needs users (obviously, everyone needs users ๐Ÿ’–), so we're making a users table. First things first, add the change:

> sqitch add users --requires appschema -n 'Creates table to track our users.'
Created deploy/users.sql
Created revert/users.sql
Created verify/users.sql
Added "users [appschema]" to sqitch.plan

Not me making appschema a requirement of the shiny new users change ๐Ÿ‘€. Yeah, even though appschema was already added to the plan (so it's always gonna get applied before users anyway), it's still a really good idea to spell out your dependencies explicitly. Be loud about what you need, you know?


Now go edit those scripts. When you're done, deploy/users.sql should look exactly like this:

-- Deploy flipr:users to snowflake
-- requires: appschema

USE WAREHOUSE &warehouse;
CREATE TABLE flipr.users (
    nickname  TEXT         PRIMARY KEY,
    password  TEXT         NOT NULL,
    fullname  TEXT         NOT NULL,
    twitter   TEXT         NOT NULL,
    timestamp TIMESTAMP_TZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

A few things to clock here, bestie:

  • On line two, the dependency on the appschema change is listed. This doesn't do anything โ€” it's just there because the default deploy Snowflake template lists it for your reference while you're editing the file. Useful, right? ๐Ÿซถ
  • The table itself gets created in the flipr schema. That's literally why we need to require the appschema change โ€” no schema, no table, no vibe.
  • On line four, that USE WAREHOUSE statement was inserted by the default Snowflake template. We don't actually need it to create a table, but leaving it there hurts nobody. Immaculate.

Now for the verify script. What's the easiest way to check that the table got created and has the columns we expect โ€” without touching any of the actual data? Just SELECT from the table with a WHERE clause that's false. Here the USE WAREHOUSE statement is required, so that the SELECT can actually execute. Probably easiest to just leave the default, which uses the warehouse that Sqitch uses to maintain its registry. Edit verify/users.sql to look like this:

USE WAREHOUSE &warehouse;
SELECT nickname, password, fullname, twitter, timestamp
  FROM flipr.users
WHERE FALSE;

Now the revert script: all we have to do is drop the table. Add this to revert/users.sql:

DROP TABLE flipr.users;

Could it get any simpler? No it literally could not. Let's deploy this bad boy:

> sqitch deploy
Deploying changes to flipr_test
  + users .. ok

We know, since verification is enabled, that the table must have been created. But for the sake of visibility, let's take a quick lil peek:

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW TERSE TABLES LIKE 'users' IN flipr"
+-------------------------------+-------+-------+---------------+-------------+
| created_on                    | name  | kind  | database_name | schema_name |
|-------------------------------+-------+-------+---------------+-------------|
| 2018-07-27 15:13:21.767 +0000 | USERS | TABLE | DWHEELER      | FLIPR       |
+-------------------------------+-------+-------+---------------+-------------+
1 Row(s) produced. Time Elapsed: 0.318s

There she is ๐Ÿ˜ญ๐Ÿ’…. We can also verify all currently deployed changes with the verify command:

> sqitch verify
Verifying flipr_test
  * appschema .. ok
  * users ...... ok
Verify successful

Now let's look at the status:

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   d251b2c9b4bc46a4b4db6b7a8a637951484e6f6b
# Name:     users
# Deployed: 2018-07-27 11:09:12 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

Success! โœจ Now let's make sure we can revert the change too, because we're thorough like that:

> sqitch revert --to @HEAD^ -y
Reverting changes to appschema from flipr_test
  - users .. ok

Okay so notice we used the --to option to specify which change to revert to. And what are we reverting to? The symbolic tag @HEAD โ€” when you pass it to revert, it always refers to the last change deployed to the database. (For other commands, it means the last change in the plan.) Slapping a caret (^) on the end tells Sqitch to pick the change prior to the last deployed change. So we're reverting to appschema, the penultimate change. The other symbolic tag you'll wanna know about is @ROOT, which refers to the first change deployed to the database (or the first in the plan, depending on which command you're running).

Back to the database, bestie. The users table should be gone, but the flipr schema should still be hanging around:

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW TERSE TABLES LIKE 'users' IN flipr"
+------------+------+------+---------------+-------------+
| created_on | name | kind | database_name | schema_name |
|------------+------+------+---------------+-------------+
+------------+------+------+---------------+-------------+
0 Row(s) produced. Time Elapsed: 0.367s

Empty. As expected. The status command politely informs us that we have undeployed changes:

# On database flipr_test
# Project:  flipr
# Change:   5a2ac4ae6801bfe392483ee5912b4e3592cdd57a
# Name:     appschema
# Deployed: 2018-07-27 10:52:54 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
Undeployed change:
  * users

And so does the verify command:

> sqitch verify
Verifying flipr_test
  * appschema .. ok
Undeployed change:
  * users
Verify successful

Note that the verify is successful โ€” because all currently-deployed changes are verified. That list of undeployed changes (just users here) is just there to remind us about the current state. A lil status check-in, if you will ๐ŸŒธ.


Okay! Let's commit and deploy again:

> git add .
> git commit -am 'Add users table.'
[main 8c16c09] Add users table.
 4 files changed, 22 insertions(+)
 create mode 100644 deploy/users.sql
 create mode 100644 revert/users.sql
 create mode 100644 verify/users.sql
> sqitch deploy
Deploying changes to flipr_test
  + users .. ok

Looks good. Check the status:

> sqitch status
# Project:  flipr
# Change:   d251b2c9b4bc46a4b4db6b7a8a637951484e6f6b
# Name:     users
# Deployed: 2018-07-27 11:19:30 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

Excellent. Let's do some more! ๐Ÿ”ฅ

โœจ Add Two at Once โœจ

Okay bestie, we're cooking now ๐Ÿ’… Let's add a couple more changes. Our app needs to store status messages from users โ€” let's call them and the table that stores them flips. And we also need a view that lists user names with their flips. So we're adding changes for both:

> sqitch add flips -r appschema -r users -n 'Adds table for storing flips.'
Created deploy/flips.sql
Created revert/flips.sql
Created verify/flips.sql
Added "flips [appschema users]" to sqitch.plan

> sqitch add userflips -r appschema -r users -r flips \
  -n 'Creates the userflips view.'
Created deploy/userflips.sql
Created revert/userflips.sql
Created verify/userflips.sql
Added "userflips [appschema users flips]" to sqitch.plan

Now is honestly a perfect moment to peek at the deployment plan, bestie:

> cat sqitch.plan
%syntax-version=1.0.0
%project=flipr
%uri=https://github.com/sqitchers/sqitch-snowflake-intro/

appschema 2018-07-27T14:27:24Z Marge N. Oโ€™Vera <marge@example.com> # Add schema for all flipr objects.
users [appschema] 2018-07-27T15:03:56Z Marge N. Oโ€™Vera <marge@example.com> # Creates table to track our users.
flips [appschema users] 2018-07-27T15:23:41Z Marge N. Oโ€™Vera <marge@example.com> # Adds table for storing flips.
userflips [appschema users flips] 2018-07-27T15:23:50Z Marge N. Oโ€™Vera <marge@example.com> # Creates the userflips view.

See how every change gets its own single line? It's giving โœจone line per changeโœจ: the name of the change, a bracketed list of dependencies, a timestamp, the name and email address of the user who planned the change, and a note. Chef's kiss ๐Ÿ‘‘

Time to write the actual code for the new changes. Here's what deploy/flips.sql should look like:

-- Deploy flipr:flips to snowflake
-- requires: appschema
-- requires: users

USE WAREHOUSE &warehouse;
CREATE TABLE flipr.flips (
    id        INTEGER        PRIMARY KEY,
    nickname  TEXT           NOT NULL REFERENCES flipr.users(nickname),
    body      VARCHAR(180)   NOT NULL DEFAULT '',
    timestamp TIMESTAMP_TZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

And here's what verify/flips.sql might look like:

-- Verify flipr:flips on snowflake

USE WAREHOUSE &warehouse;
SELECT id, nickname, body, timestamp
  FROM flipr.flips
 WHERE FALSE;

And revert/flips.sql should look something like this:

-- Revert flipr:flips from snowflake

USE WAREHOUSE &warehouse;
DROP TABLE flipr.flips;

Now for userflips โ€” deploy/userflips.sql might look like this:

-- Deploy flipr:userflips to snowflake
-- requires: appschema
-- requires: users
-- requires: flips

USE WAREHOUSE &warehouse;
CREATE OR REPLACE VIEW flipr.userflips AS
SELECT f.id, u.nickname, u.fullname, f.body, f.timestamp
  FROM flipr.users u
  JOIN flipr.flips f ON u.nickname = f.nickname;

We're using a SELECT statement in verify/userflips.sql again, because of course we are:

-- Verify flipr:userflips on snowflake

USE WAREHOUSE &warehouse;
SELECT id, nickname, fullname, body, timestamp
  FROM flipr.userflips
 WHERE FALSE;

And obviously its revert script, revert/userflips.sql, should look something like:

-- Revert flipr:userflips from snowflake

USE WAREHOUSE &warehouse;
DROP VIEW flipr.userflips;

Try em out, bestie!

> sqitch deploy
Deploying changes to flipr_test
  + flips ...... ok
  + userflips .. ok

Do we have the new table and view? Um, obviously we do โ€” they were verified ๐Ÿ’โ€โ™€๏ธ But still, have a look:

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW TERSE TABLES LIKE 'flips' IN flipr"
+-------------------------------+-------+-------+---------------+-------------+
| created_on                    | name  | kind  | database_name | schema_name |
|-------------------------------+-------+-------+---------------+-------------|
| 2018-07-27 15:31:07.137 +0000 | FLIPS | TABLE | DWHEELER      | FLIPR       |
+-------------------------------+-------+-------+---------------+-------------+
1 Row(s) produced. Time Elapsed: 0.225s

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW TERSE VIEWS LIKE 'userflips' IN flipr"
+-------------------------------+-----------+------+---------------+-------------+
| created_on                    | name      | kind | database_name | schema_name |
|-------------------------------+-----------+------+---------------+-------------|
| 2018-07-27 15:29:25.733 +0000 | USERFLIPS | VIEW | DWHEELER      | FLIPR       |
+-------------------------------+-----------+------+---------------+-------------+
1 Row(s) produced. Time Elapsed: 0.299s

And what's the status, you ask?

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   73cd50c99de2a8b3eab206c73514afbeb952023c
# Name:     userflips
# Deployed: 2018-07-27 11:31:24 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

Looks good ๐Ÿ˜Œ Let's make sure revert works:

> sqitch revert -y --to @HEAD^^
Reverting changes to users from flipr_test
  - userflips .. ok
  - flips ...... ok

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW TERSE TABLES LIKE 'flips' IN flipr"
+------------+------+------+---------------+-------------+
| created_on | name | kind | database_name | schema_name |
|------------+------+------+---------------+-------------+
+------------+------+------+---------------+-------------+
0 Row(s) produced. Time Elapsed: 0.306s
๐Ÿ‘€ Note the use of @HEAD^^ to specify that the revert be to two changes prior the last deployed change.

Looks good. Let's do the commit and re-deploy dance ๐Ÿ•บ

> git add .
> git commit -m 'Add flips table and userflips view.'
[main b36f48b] Add flips table and userflips view.
 7 files changed, 43 insertions(+)
 create mode 100644 deploy/flips.sql
 create mode 100644 deploy/userflips.sql
 create mode 100644 revert/flips.sql
 create mode 100644 revert/userflips.sql
 create mode 100644 verify/flips.sql
 create mode 100644 verify/userflips.sql

> sqitch deploy
Deploying changes to flipr_test
  + flips ...... ok
  + userflips .. ok

> sqitch status
# Project:  flipr
# Change:   73cd50c99de2a8b3eab206c73514afbeb952023c
# Name:     userflips
# Deployed: 2018-07-27 11:38:02 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

> sqitch verify
Verifying flipr_test
  * appschema .. ok
  * users ...... ok
  * flips ...... ok
  * userflips .. ok
Verify successful

Great, we're fully up-to-date! ๐ŸŽ‰


๐Ÿšข Ship It! ๐Ÿšข

Let's do a first release of our app. Let's call it 1.0.0-dev1. Since we want it to go out with deployments tied to the release, let's tag it:

> sqitch tag v1.0.0-dev1 -n 'Tag v1.0.0-dev1.'
Tagged "userflips" with @v1.0.0-dev1
> git commit -am 'Tag the database with v1.0.0-dev1.'
[main 84ed9db] Tag the database with v1.0.0-dev1.
 1 files changed, 1 insertion(+)
> git tag v1.0.0-dev1 -am 'Tag v1.0.0-dev1'

We can try deploying to make sure the tag gets picked up, like so:

> sqitch deploy
Nothing to deploy (up-to-date)
> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   73cd50c99de2a8b3eab206c73514afbeb952023c
# Name:     userflips
# Tag:      @v1.0.0-dev1
# Deployed: 2018-07-27 11:38:02 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

Notice the shiny new Tag line in the output of sqitch status: no new changes needed to be deployed, but Sqitch did deploy the tag on the userflips change. Iconic behavior honestly ๐Ÿ’– Now let's bundle everything up for release:

> sqitch bundle
Bundling into bundle
Writing config
Writing plan
Writing scripts
  + appschema
  + users
  + flips
  + userflips @v1.0.0-dev1

Now we can package the bundle directory and distribute it. When it gets installed somewhere, users can use Sqitch to deploy to the database. Let's try deploying it to another database, flipr_prod:

> cd bundle
> sqitch deploy 'db:snowflake://movera@example/flipr_prod?Driver=Snowflake'
Adding registry tables to db:snowflake://movera@example/flipr_prod?Driver=Snowflake'
Deploying changes to db:snowflake://movera@example/flipr_prod?Driver=Snowflake'
  + appschema ............... ok
  + users ................... ok
  + flips ................... ok
  + userflips @v1.0.0-dev1 .. ok

Notice how the tag on userflips now shows up in the deploy output. Nice, eh? ๐Ÿ˜ Now, package it up and ship it!

> cd ..
> mv bundle flipr-v1.0.0-dev1
> tar -czf flipr-v1.0.0-dev1.tgz flipr-v1.0.0-dev1

๐Ÿงฎ Making a Hash of Things ๐Ÿงฎ

Now that we've got the basics of the app done, let's add a feature. Gotta track the hashtags associated with flips, right? So let's add a table for them. But since other folks are working on other tasks in the repository, we'll work on a branch, so we can all stay out of each other's way. So let's branch:

> git checkout -b hashtags
Switched to a new branch 'hashtags'

Now we can add a new change to create a table for hashtags.

> sqitch add hashtags --requires flips -n 'Adds table for storing hashtags.'
Created deploy/hashtags.sql
Created revert/hashtags.sql
Created verify/hashtags.sql
Added "hashtags [appschema flips]" to sqitch.plan

You know the drill by now, bestie. Add this to deploy/hashtags.sql:

CREATE TABLE flipr.hashtags (
    flip_id   INTEGER       NOT NULL REFERENCES flipr.flips(id),
    hashtag   VARCHAR(128)  NOT NULL,
    PRIMARY KEY (flip_id, hashtag)
);

Again, select from the table in verify/hashtags.sql:

SELECT flip_id, hashtag FROM flipr.hashtags WHERE FALSE;

And drop it in revert/hashtags.sql:

DROP TABLE flipr.hashtags;

And give it a whirl:

> sqitch deploy
Deploying changes to flipr_test
  + hashtags .. ok

Look good?

> sqitch status --show-tags
# On database flipr_test
# Project:  flipr
# Change:   d750cbeec487841c45715115a31297739fbb4046
# Name:     hashtags
# Deployed: 2018-07-27 11:53:02 -0400
# By:       Marge N. Oโ€™Vera <marge@example.com>
#
# Tag:
#   @v1.0.0-dev1 - 2018-07-27 11:41:13 -0400 - Marge N. Oโ€™Vera <marge@example.com>
#
Nothing to deploy (up-to-date)
โœจ Note the use of --show-tags to show all the deployed tags.

Make sure we can revert, too:

> sqitch revert -y --onto @HEAD^
Reverting changes to userflips @v1.0.0-dev1 from flipr_test
  - hashtags .. ok
> sqitch deploy
Deploying changes to flipr_test
  + hashtags .. ok

Great! Now make it so:

> git add .
> git commit -m 'Add hashtags table.'
[hashtags 06a0bf4] Add hashtags table.
 4 files changed, 19 insertions(+)
 create mode 100644 deploy/hashtags.sql
 create mode 100644 revert/hashtags.sql
 create mode 100644 verify/hashtags.sql

Good, we've finished this feature. Time to merge back into "main".

๐Ÿšจ Emergency ๐Ÿšจ ๐Ÿ’€

Let's do it:

> git checkout main
Switched to branch 'main'
> git pull
Updating 84ed9db..31d026c
Fast-forward
 deploy/lists.sql | 11 +++++++++++
 revert/lists.sql |  4 ++++
 sqitch.plan      |  2 ++
 verify/lists.sql |  6 ++++++
 4 files changed, 23 insertions(+)
 create mode 100644 deploy/lists.sql
 create mode 100644 revert/lists.sql
 create mode 100644 verify/lists.sql

Hrm, that's interesting. Looks like someone made some changes to "main". They added list support. Well, let's see what happens when we merge our changes.

> git merge --no-ff hashtags
Auto-merging sqitch.plan
CONFLICT (content): Merge conflict in sqitch.plan
Automatic merge failed; fix conflicts and then commit the result.

Oh, a conflict in sqitch.plan. Not too surprising, since both the merged "lists" branch and our "hashtags" branch added changes to the plan. Let's try a different approach.

The truth is, we got lazy. Those changes when we pulled main from the origin should have raised a red flag. It's considered a bad practice not to look at what's changed in "main" before merging in a branch. What one should do is either:

  • Rebase the hashtags branch from main before merging. This "rewinds" the branch changes, pulls from "main", and then replays the changes back on top of the pulled changes.
  • Create a patch and apply that to main. This is the sort of thing you might have to do if you're sending changes to another user, especially if the VCS is not Git.

So let's restore things to how they were at main:

> git reset --hard HEAD
HEAD is now at 31d026c Merge branch 'lists'

That throws out our botched merge ๐Ÿ˜Œ. Now let's go back to our branch and rebase it on "main":

> git checkout hashtags
Switched to branch 'hashtags'
> git rebase main
First, rewinding head to replay your work on top of it...
Applying: Add hashtags table.
Using index info to reconstruct a base tree...
M     sqitch.plan
Falling back to patching base and 3-way merge...
Auto-merging sqitch.plan
CONFLICT (content): Merge conflict in sqitch.plan
error: Failed to merge in the changes.
Patch failed at 0001 Add hashtags table.
Use 'git am --show-current-patch' to see the failed patch

Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".

Oy, that's kind of a pain ๐Ÿ˜ญ. It seems like no matter what we do, we'll need to resolve conflicts in that file. Except in Git. Fortunately for us, we can tell Git to resolve conflicts in sqitch.plan differently. Because we only ever append lines to the file, we can have it use the "union" merge driver, which, according to its docs <https://git-scm.com/docs/gitattributes#_built-in_merge_drivers>:

Run 3-way file level merge for text files, but take lines from both versions, instead of leaving conflict markers. This tends to leave the added lines in the resulting file in random order and the user should verify the result. Do not use this if you do not understand the implications.

This has the effect of appending lines from all the merging files, which is exactly what we need. So let's give it a try. First, back out the botched rebase:

> git rebase --abort
HEAD is now at 06a0bf4 Add hashtags table.

Now add the union merge driver to .gitattributes for sqitch.plan and rebase again:

> echo sqitch.plan merge=union > .gitattributes
> git rebase main
First, rewinding head to replay your work on top of it...
Applying: Add hashtags table.
Using index info to reconstruct a base tree...
M     sqitch.plan
Falling back to patching base and 3-way merge...
Auto-merging sqitch.plan

Ah, that looks a bit better โœจ. Let's have a look at the plan:

> cat sqitch.plan
%syntax-version=1.0.0
%project=flipr
%uri=https://github.com/sqitchers/sqitch-snowflake-intro/

appschema 2018-07-27T14:27:24Z Marge N. Oโ€™Vera <marge@example.com> # Add schema for all flipr objects.
users [appschema] 2018-07-27T15:03:56Z Marge N. Oโ€™Vera <marge@example.com> # Creates table to track our users.
flips [appschema users] 2018-07-27T15:23:41Z Marge N. Oโ€™Vera <marge@example.com> # Adds table for storing flips.
userflips [appschema users flips] 2018-07-27T15:23:50Z Marge N. Oโ€™Vera <marge@example.com> # Creates the userflips view.
@v1.0.0-dev1 2018-07-27T15:40:25Z Marge N. Oโ€™Vera <marge@example.com> # Tag v1.0.0-dev1.

lists [appschema flips] 2018-07-27T16:00:00Z Marge N. Oโ€™Vera <marge@example.com> # Adds table for storing lists.
hashtags [flips] 2018-07-27T15:51:16Z Marge N. Oโ€™Vera <marge@example.com> # Adds table for storing hashtags.

Note that it has appended the changes from the merged "lists" branch, and then merged the changes from our "hashtags" branch. Test it to make sure it works as expected:

> sqitch rebase -y
Reverting all changes from flipr_test
  - hashtags ................ ok
  - userflips @v1.0.0-dev1 .. ok
  - flips ................... ok
  - users ................... ok
  - appschema ............... ok
Deploying changes to flipr_test
  + appschema ............... ok
  + users ................... ok
  + flips ................... ok
  + userflips @v1.0.0-dev1 .. ok
  + lists ................... ok
  + hashtags ................ ok

Note the use of "rebase", which combines a "revert" and a "deploy" into a single command. Handy, right? It correctly reverted our changes, and then deployed them all again in the proper order ๐Ÿ˜Œ. So let's commit .gitattributes; seems worthwhile to keep that change:

> git add .
> git commit -m 'Add `.gitattributes` with union merge for `sqitch.plan`.'
[hashtags 86596a9] Add `.gitattributes` with union merge for `sqitch.plan`.
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 .gitattributes

๐Ÿ’– Merges Mastered ๐Ÿ’– ๐Ÿ‘‘

And now, finally, we can merge into "main":

> git checkout main
Switched to branch 'main'
> git merge --no-ff hashtags -m "Merge branch 'hashtags'"
Merge made by the 'recursive' strategy.
 .gitattributes      | 1 +
 deploy/hashtags.sql | 9 ++++++++++
 revert/hashtags.sql | 4 ++++
 sqitch.plan         | 1 +
 verify/hashtags.sql | 4 ++++
 5 files changed, 19 insertions(+)
 create mode 100644 .gitattributes
 create mode 100644 deploy/hashtags.sql
 create mode 100644 revert/hashtags.sql
 create mode 100644 verify/hashtags.sql

And double-check our work:

> cat sqitch.plan
%syntax-version=1.0.0
%project=flipr
%uri=https://github.com/sqitchers/sqitch-snowflake-intro/

appschema 2018-07-27T14:27:24Z Marge N. Oโ€™Vera <marge@example.com> # Add schema for all flipr objects.
users [appschema] 2018-07-27T15:03:56Z Marge N. Oโ€™Vera <marge@example.com> # Creates table to track our users.
flips [appschema users] 2018-07-27T15:23:41Z Marge N. Oโ€™Vera <marge@example.com> # Adds table for storing flips.
userflips [appschema users flips] 2018-07-27T15:23:50Z Marge N. Oโ€™Vera <marge@example.com> # Creates the userflips view.
@v1.0.0-dev1 2018-07-27T15:40:25Z Marge N. Oโ€™Vera <marge@example.com> # Tag v1.0.0-dev1.

lists [appschema flips] 2018-07-27T16:00:00Z Marge N. Oโ€™Vera <marge@example.com> # Adds table for storing lists.
hashtags [flips] 2018-07-27T15:51:16Z Marge N. Oโ€™Vera <marge@example.com> # Adds table for storing hashtags.

Much much better, a nice clean main now ๐Ÿงš. And because it is now identical to the "hashtags" branch, we can just carry on. Go ahead and tag it, bundle, and release:

> sqitch tag v1.0.0-dev2 -n 'Tag v1.0.0-dev2.'
Tagged "hashtags" with @v1.0.0-dev2
> git commit -am 'Tag the database with v1.0.0-dev2.'
[main 1c67e0d] Tag the database with v1.0.0-dev2.
 1 files changed, 1 insertion(+)
> git tag v1.0.0-dev2 -am 'Tag v1.0.0-dev2'
> sqitch bundle --dest-dir flipr-1.0.0-dev2
Bundling into flipr-1.0.0-dev2
Writing config
Writing plan
Writing scripts
  + appschema
  + users
  + flips
  + userflips @v1.0.0-dev1
  + lists
  + hashtags @v1.0.0-dev2

Note the use of the --dest-dir option to sqitch bundle. Just a nicer way to create the top-level directory name so we don't have to rename it from bundle ๐Ÿ’….

๐Ÿฆ‹ In Place Changes ๐Ÿฆ‹

okay so some besties have been testing the "1.0.0-dev2" release and they are demanding that Twitter user links get added to Flipr pages. Why anyone would want social network links inside an anti-social networking app is genuinely beyond us programmers, but we're just the plumbers, right? Gotta go with what Product demands. ๐Ÿ’€ The upshot: we need to update the "userflips" view (the one used for that feature) so it includes the Twitter user names.

Normally, modifying views in database changes is a PITA <https://www.urbandictionary.com/define.php?term=pita>. Like, you have to do ALL of this:

  1. Copy deploy/userflips.sql to deploy/userflips_twitter.sql.
  2. Edit deploy/userflips_twitter.sql to drop and re-create the view with the "twitter" column to the view.
  3. Copy deploy/userflips.sql to revert/userflips_twitter.sql. Yes, copy the original change script to the new revert change.
  4. Add a "DROP VIEW" statement to revert/userflips_twitter.sql.
  5. Copy verify/userflips.sql to verify/userflips_twitter.sql.
  6. Modify verify/userflips_twitter.sql to include a check for the "twitter" column.
  7. Test the changes to make sure you can deploy and revert the "userflips_twitter" change.

But bestie, Sqitch can do most of that for you. ๐Ÿซถ The only requirement is that a tag appear between the two instances of a change we want to modify. And like, in general you're gonna make a change like this after a release, which you've tagged anyway, right? Well we have โ€” with "@v1.0.0-dev2" added in the previous section. With that in place, we can let Sqitch do most of the hard work for us, thanks to the "rework" command, which is similar to "add":

> sqitch rework userflips -n 'Adds userflips.twitter.'
Added "userflips [userflips@v1.0.0-dev2]" to sqitch.plan.
Modify these files as appropriate:
  * deploy/userflips.sql
  * revert/userflips.sql
  * verify/userflips.sql

Oh, so we can edit those files in place. Nice! ๐Ÿ˜Œ How does Sqitch do it? Well, in point of fact, it has copied the files to stand in for the previous instance of the "userflips" change, which we can see via "git status":

> git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

  modified:   revert/userflips.sql
  modified:   sqitch.plan

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  deploy/userflips@v1.0.0-dev2.sql
  revert/userflips@v1.0.0-dev2.sql
  verify/userflips@v1.0.0-dev2.sql

no changes added to commit (use "git add" and/or "git commit -a")

The "Untracked files" part of the output is the first thing to notice. ๐Ÿ‘€ They're all named "userflips@v1.0.0-dev2.sql". What that means is: "the "userflips" change as it was implemented as of the "@v1.0.0-dev2" tag." These are copies of the original scripts, and thereafter Sqitch will find them when it needs to run scripts for the first instance of the "userflips" change. As such, it's important not to change them again. But hey, if you're reworking the change, you shouldn't need to. ๐Ÿ’…

The other thing to notice is that revert/userflips.sql has changed. Sqitch replaced it with the original deploy script. As of now, deploy/userflips.sql and revert/userflips.sql are identical. This is on the assumption that the deploy script will be changed (we're reworking it, remember?), and that the revert script should actually change things back to how they were before. Of course, the original deploy script may not be idempotent <https://en.wikipedia.org/wiki/Idempotence> โ€” that is, able to be applied multiple times without changing the result beyond the initial application. If it's not, you will likely need to modify it so that it properly restores things to how they were after the original deploy script was deployed. Or, more simply, it should revert changes back to how they were as-of the deployment of deploy/userflips@v1.0.0-dev2.sql.

Fortunately, our view deploy scripts are already idempotent, thanks to the use of the "OR REPLACE" expression. No matter how many times a deployment script is run, the end result will be the same instance of the view, with no duplicates or errors. โœจ

As a result, there is no need to explicitly add changes. So go ahead. Modify the script to add the "twitter" column to the view. Make this change to deploy/userflips.sql:

@@ -5,6 +5,6 @@

USE WAREHOUSE &warehouse;
CREATE OR REPLACE VIEW flipr.userflips AS
-SELECT f.id, u.nickname, u.fullname, f.body, f.timestamp
+SELECT SELECT f.id, u.nickname, u.fullname, u.twitter, f.body, f.timestamp
  FROM flipr.users u
  JOIN flipr.flips f ON u.nickname = f.nickname;

Next, modify verify/userflips.sql to check for the "twitter" column. Here's the diff:

@@ -1,6 +1,6 @@
 -- Verify flipr:userflips on snowflake

-SELECT id, nickname, fullname, body, timestamp
+SELECT id, nickname, fullname, twitter, body, timestamp
   FROM flipr.userflips
 WHERE FALSE;

Now try a deployment:

> sqitch deploy
Deploying changes to flipr_test
  + userflips .. ok

So, are the changes deployed? ๐Ÿ”

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW VIEWS LIKE 'userflips' IN flipr"
+-------------------------------+-----------+----------+---------------+-------------+--------+---------+---------------------------------------------------------------------+-----------+
| created_on                    | name      | reserved | database_name | schema_name | owner  | comment | text                                                                | is_secure |
|-------------------------------+-----------+----------+---------------+-------------+--------+---------+---------------------------------------------------------------------+-----------|
| 2018-07-27 18:19:29.818 +0000 | USERFLIPS |          | DWHEELER      | FLIPR       | SQITCH |         | CREATE OR REPLACE VIEW flipr.userflips AS                           | false     |
|                               |           |          |               |             |        |         | SELECT f.id, u.nickname, u.fullname, u.twitter, f.body, f.timestamp |           |
|                               |           |          |               |             |        |         |   FROM flipr.users u                                                |           |
|                               |           |          |               |             |        |         |   JOIN flipr.flips f ON u.nickname = f.nickname;                    |           |
+-------------------------------+-----------+----------+---------------+-------------+--------+---------+---------------------------------------------------------------------+-----------+
1 Row(s) produced. Time Elapsed: 0.413s

Awesome, the view now includes the "twitter" column. ๐Ÿ”ฅ But can we revert?

> sqitch revert --to @HEAD^ -y
Reverting changes to hashtags @v1.0.0-dev2 from flipr_test
  - userflips .. ok

Did that work, is the "twitter" column gone?

> snowsql --accountname example --username movera --dbname flipr -o friendly=false \
  --query "SHOW VIEWS LIKE 'userflips' IN flipr"
+-------------------------------+-----------+----------+---------------+-------------+--------+---------+----------------------------------------------------------+-----------+
| created_on                    | name      | reserved | database_name | schema_name | owner  | comment | text                                                     | is_secure |
|-------------------------------+-----------+----------+---------------+-------------+--------+---------+----------------------------------------------------------+-----------|
| 2018-07-27 18:50:52.064 +0000 | USERFLIPS |          | DWHEELER      | FLIPR       | SQITCH |         | CREATE OR REPLACE VIEW flipr.userflips AS                | false     |
|                               |           |          |               |             |        |         | SELECT f.id, u.nickname, u.fullname, f.body, f.timestamp |           |
|                               |           |          |               |             |        |         |   FROM flipr.users u                                     |           |
|                               |           |          |               |             |        |         |   JOIN flipr.flips f ON u.nickname = f.nickname;         |           |
+-------------------------------+-----------+----------+---------------+-------------+--------+---------+----------------------------------------------------------+-----------+
1 Row(s) produced. Time Elapsed: 0.362s

Yes, it works! ๐ŸŽ‰ Sqitch properly finds the original instances of these changes in the new script files that include tags.

Excellent. Let's go ahead and commit these changes:

> git add .
> git commit -m 'Add the twitter column to the userflips view.'
[main c004445] Add the twitter column to the userflips view.
 7 files changed, 31 insertions(+), 4 deletions(-)
 create mode 100644 deploy/userflips@v1.0.0-dev2.sql
 create mode 100644 revert/userflips@v1.0.0-dev2.sql
 create mode 100644 verify/userflips@v1.0.0-dev2.sql

๐ŸŒˆ More to Come ๐ŸŒˆ

Sqitch is a work in progress. Better integration with version control systems is planned to make managing idempotent reworkings even easier. Stay tuned. ๐Ÿซถโœจ


๐Ÿ‘‘ Author ๐Ÿ‘‘

David E. Wheeler <david@justatheory.com>


๐Ÿ’œ License ๐Ÿ’œ

Copyright (c) 2012-2026 David E. Wheeler, 2012-2021 iovation Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


and that's a wrap bestie!! ๐ŸŽ€ this lil tour was brought to you by perl v5.40.1, dated 2026-07-26, filed under sqitch...wflake(3pm). go forth and rework ur views, u absolute icon, no cap ๐Ÿ’–๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from