If you're a Git user and want to follow along the history, the repository used in these examples is on GitHub.

Now that we have a repository, let's get started with Sqitch. Every Sqitch project must have a name associated with it, and, optionally, a unique URI. We recommend including the URI, as it increases the uniqueness of object identifiers internally, so let's specify one when we initialize Sqitch:

Good, it picked up on the fact that we're creating changes for the SQLite engine, thanks to the -engine sqlite option, and saved it to the file. Furthermore, it wrote a commented-out [engine "sqlite"] section with all the available SQLite engine-specific settings commented out and ready to be edited as appropriate.

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

> sqitch config --user engine.sqlite.client /opt/local/bin/sqlite3

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

Note that it has picked up on the name and URI of the app we're building. Sqitch uses this data to manage cross-project dependencies. The %syntax-version pragma is always set by Sqitch, so that it always knows how to parse the plan, even if the format changes in the future.

The add command adds a database change to the plan and writes deploy, revert, and verify scripts that represent the change. Now we edit these files. The deploy script's job is to create the table. By default, the deploy/users.sql file looks like this:

-- Deploy flipr:users to sqlite
BEGIN;
-- XXX Add DDLs here.
COMMIT;

What we want to do is to replace the XXX comment with the CREATE TABLE statement, like so:

First Sqitch created the registry database and tables used to track database changes. The registry is separate from the database to which the users change was deployed; by default, its name is sqitch.$suffix, where $suffix is the same as the suffix on the target database, if any. It lives in the same directory as the target database. This will be useful if you use the SQLite ATTACHDATABASE command to manage multiple database files in a single project. In that case, you will want to use the same file for all the databases. Keep them all in the same directory with the same suffix and you get just that with the default sqitch database.

If you'd like it to have a different name for the registry database, use sqitch engine add sqlite $name to configure it (or via the target command; more below). This will be useful if you don't want to use the same registry database to manage multiple databases, or if you do, but they live in different directories.

Next, Sqitch deploys changes to the target database, which we specified on the command-line. We only have one so far; the + reinforces the idea that the change is being added to the database.

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

But that's too much work. do you really want to do something like that after every deploy?

Here's where the verify script comes in. Its job is to test that the deploy did was it was supposed to. It should do so without regard to any data that might be in the database, and should throw an error if the deploy was not successful. The easiest way to do that with a table is to simply SELECT from it. Put this query into verify/users.sql:

The revert command first prompts to make sure that we really do want to revert. This is to prevent unnecessary accidents. You can pass the -y option to disable the prompt. Also, notice the - before the change name in the output, which reinforces that the change is being removed from the database. And now the schema should be gone:

I'm getting a little tired of always having to type db:sqlite:flipr_test.db, aren't you? This database connection URI tells Sqitch how to connect to the deployment target, but we don't have to keep using the URI. We can name the target:

> sqitch target add flipr_test db:sqlite:flipr_test.db

The target command, inspired by git-remote, allows management of one or more named deployment targets. We've just added a target named flipr_test, which means we can use the string flipr_test for the target, rather than the URI. But since we're doing so much testing, we can also tell Sqitch to deploy to the flipr_test target by default:

> sqitch engine add sqlite 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 will be simpler from here on in, e.g.:

Note that we're requiring the users change as a dependency of the new flips change. Although that change has already been added to the plan and therefore should always be applied before the flips change, it's a good idea to be explicit about dependencies.

Now edit the scripts. When you're done, deploy/flips.sql should look like this:

A couple things to notice here. On the second line, the dependence on the users change has been listed. This doesn't do anything, but the default deploy template lists it here for your reference while editing the file. Useful, right?

The users.nickname column references the users table. This is why we need to require the users change.

Now for the verify script. Again, all we need to do is SELECT from the table. I recommend selecting each column by name, too, to be sure that no column is missing. Here's the verify/flips.sql:

Note that we've used the --to option to specify the change to revert to. And what do we revert to? The symbolic tag @HEAD, when passed to revert, always refers to the last change deployed to the database. (For other commands, it refers to the last change in the plan.) Appending the caret (^) tells Sqitch to select the change prior to the last deployed change. So we revert to users, the penultimate change. The other potentially useful symbolic tag is @ROOT, which refers to the first change deployed to the database (or in the plan, depending on the command).

Back to the database. The flips table should be gone but the users table should still be around:

> sqlite3 flipr_test.db '.tables'
users

The status command politely informs us that we have undeployed changes:

Now that we've got the basics of the app done, let's add a feature. Gotta track the hashtags associated with flips, right? 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:

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 master from the origin should have raised a red flag. It's considered a bad practice not to look at what's changed in master before merging in a branch. What one should do is either:

Rebase the hashtags branch from master before merging. This "rewinds" the branch changes, pulls from master, and then replays the changes back on top of the pulled changes.

Create a patch and apply that to master. 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 master:

> git reset --hard HEAD
HEAD is now at 47a4107 Merge branch 'lists'

That throws out our botched merge. Now let's go back to our branch and rebase it on master:

> git checkout hashtags
Switched to branch 'hashtags'
> git rebase master
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
Failed to merge in the changes.
Patch failed at 0001 Add hashtags table.
The copy of the patch that failed is found in:
.git/rebase-apply/patch
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, 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:

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

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

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:

Well, some folks have been testing the 1.0.0-dev2 release and have demanded that Twitter user links be added to Flipr pages. Why anyone would want to include social network links in an anti-social networking app is beyond us programmers, but we're just the plumbers, right? Gotta go with what Product demands. The upshot is that we need to update the userflips view, which is used for the feature in question, to include the Twitter user names.

Normally, modifying views in database changes is a PITA. You have to make changes like these:

Copy deploy/userflips.sql to deploy/userflips_twitter.sql.

Edit deploy/userflips_twitter.sql to drop and re-create the view with the twitter column to the view.

Copy deploy/userflips.sql to revert/userflips_twitter.sql. Yes, copy the original change script to the new revert change.

Add a DROP VIEW statement to revert/userflips_twitter.sql.

Copy verify/userflips.sql to verify/userflips_twitter.sql.

Modify verify/userflips_twitter.sql to include a check for the twiter column.

Test the changes to make sure you can deploy and revert the userflips_twitter change.

But you can have Sqitch do most of the work for you. The only requirement is that a tag appear between the two instances of a change we want to modify. In general, you're going to 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, we can let Sqitch do most of the hard work for us, thanks to the rework command, which is similar to add:

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:

The "untracked files" part of the output is the first thing to notice. They are 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 won't be idempotent -- that is, able to be applied multiple times without changing the result beyond the initial application. It could be if SQLite supported CREATE OR REPLACE VIEW, but since it doesn't, we will have to edit the script to drop the view before creating it. Or, more simply, it needs to be updated to revert changes back to how they were as-of the deployment of deploy/userflips@v1.0.0-dev2.sql.

Modify deploy/userflips.sql to add the twitter column; in fact, let's also add a DROP VIEW IF EXISTS statement, in case we need to rework this change again in the future:

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.