sqitch...-sqlite(3pm)

✨ NAME ✨

sqitchtutorial-sqlite β€” a tutorial introduction to Sqitch change management on SQLite πŸŽ€


πŸ’« SYNOPSIS πŸ’«

sqitch *

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay bestie, so this whole tutorial is gonna walk you through making a sqitch-enabled SQLite project from scratch, using a VCS for deployment planning, and then working with other developers so all your changes stay in sync and in the right order. No cap. πŸ’…

We're gonna start by creating a brand new project from zero β€” a fictional antisocial networking site called Flipr. All the examples use Git (https://git-scm.com/) as the VCS and SQLite (https://www.sqlite.org/) as the storage engine. The way that combo just works? Obsessed.

If you'd rather manage a database with a different database engine, there are other tutorials for that, and they are:

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

🌸 STARTING A NEW PROJECT 🌸

So usually, the very first thing you do when starting a new project is make a source code repository. Let's do that with Git, obviously:

> mkdir flipr
> cd flipr
> git init .
Initialized empty Git repository in /flipr/.git/
> touch README.md
> git add .
> git commit -m 'Initialize project, add README.'
[main (root-commit) 253542e] Initialize project, add README.
 1 file changed, 37 insertions(+)
 create mode 100644 README.md

If you're a Git girlie and you wanna follow along with the history, the repository used in these examples lives on GitHub at https://github.com/sqitchers/sqitch-sqlite-intro. 🫢

Now that we have a repository, let's get into Sqitch itself. Every Sqitch project must have a name attached to it, and optionally, a unique URI. We highly recommend including the URI, because it:

  • increases the uniqueness of object identifiers internally, and
  • stops you from deploying a different project that happens to have the same name.

So let's specify one when we initialize Sqitch:

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

Now let's peek at sqitch.conf:

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

Good, it clocked that we're creating changes for the SQLite engine β€” thanks to the --engine sqlite option β€” and saved it to the file. Not only that, it also wrote a commented-out [engine "sqlite"] section with all the available SQLite engine-specific settings commented out and ready for you to edit whenever you feel like it. Iconic behaviour honestly.

By default, Sqitch reads 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 and tell it where to find the client on our computer. (Don't bother if you're using the Docker image at https://hub.docker.com/r/sqitch/sqitch/, because it uses the client inside the container, not on your host machine. πŸ™ˆ)

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

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

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

Have a look at ~/.sqitch/sqitch.conf and you'll see this:

> cat ~/.sqitch/sqitch.conf
[engine "sqlite"]
    client = /opt/local/bin/sqlite3
[user]
    name = Marge N. O’Vera
    email = marge@example.com

Which means Sqitch should be able to find sqlite3 for any project, and it will always properly identify us when planning and committing changes. Main character energy. πŸ‘‘

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-sqlite-intro/

Note that it picked up 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.

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

> git add .
> git commit -m 'Initialize Sqitch configuration.'
[main 91e2f0d] Initialize Sqitch configuration.
 2 files changed, 19 insertions(+)
 create mode 100644 sqitch.conf
 create mode 100644 sqitch.plan

🌸 Our First Change 🌸

Okay bestie, time to actually make something. Buckle up. πŸ’–

Our app is gonna need users β€” obviously β€” so let's create a table for them. Run this:

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

So what just happened? The add command adds a database change to the plan and writes out deploy, revert, and verify scripts that represent that change. Now we edit those files. The deploy script's whole job? Create the table. By default, deploy/users.sql looks like this:

-- Deploy flipr:users to sqlite

BEGIN;

-- XXX Add DDLs here.

COMMIT;

We want to replace that XXX comment with the CREATE TABLE statement, like so:

-- Deploy flipr:users to sqlite

BEGIN;

CREATE TABLE users (
        nickname  TEXT      PRIMARY KEY,
        password  TEXT      NOT NULL,
        fullname  TEXT      NOT NULL,
        twitter   TEXT      NOT NULL,
        timestamp DATETIME  NOT NULL DEFAULT CURRENT_TIMESTAMP
);

COMMIT;

The revert script's job is to precisely undo what the deploy script did, so we edit revert/users.sql to look like this:

-- Revert flipr:users from sqlite

BEGIN;

DROP TABLE users;

COMMIT;

No cap, 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/. Here we've specified a database file, flipr_test.db:

> sqitch deploy db:sqlite:flipr_test.db
Adding registry tables to db:sqlite:sqitch.db
Deploying changes to db:sqlite:flipr_test.db
  + users .. ok

First things first: Sqitch created the registry database and tables β€” the ones used to track database changes. πŸŽ€ This registry is separate from the database the users change got deployed to. By default its name is sqitch.$suffix, where $suffix is the same as the suffix on the target database (if it has one). And it lives in the same directory as the target database.

Why do we care? It's genuinely useful if you use SQLite's ATTACHDATABASE command (that's <https://www.sqlite.org/lang_attach.html>) to manage multiple database files in a single project. In that case you'll want the same file for all the databases. Keep them all in the same directory with the same suffix and, boom, the default sqitch database gives you exactly that. So in this case, we should end up with two databases:

  • sqitch.db

The Sqitch registry database.

  • flipr_test.db

The database Sqitch manages.

Wanna give the registry database a different name? Use sqitch engine add sqlite $name to configure it (or go through the target command β€” more on that below 🫢). This helps if you don't want the same registry database managing multiple databases, or if you do but they live in different directories.

Next: Sqitch deploys changes to the target database, the one we named on the command-line. We've only got one so far; that + 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:

        > sqlite3 flipr_test.db '.tables'
        users

🧐 Trust, But Verify 🧐

But bestie, that's so much work. Do you really wanna do something like that after every single deploy? 😭

Enter the verify script. Its job is to test that the deploy did what it was supposed to do β€” without caring about whatever data might be in the database β€” and to throw an error if the deploy wasn't successful. Easiest way to do that with a table is to just SELECT from it. So put this query into verify/users.sql:

SELECT nickname, password, fullname, twitter
      FROM users
 WHERE 0;

Now you can run the verify script with the verify command:

> sqitch verify db:sqlite:flipr_test.db
Verifying db:sqlite:flipr_test.db
      * users .. ok
Verify successful

Iconic. πŸ’… But if you wanna make sure the verify script actually dies when the table doesn't exist, temporarily change the table name in the script to something that doesn't exist, something like:

SELECT nickname, password, timestamp
      FROM users_nonesuch
 WHERE 0;

Then verify again:

> sqitch verify db:sqlite:flipr_test.db
Verifying db:sqlite:flipr_test.db
  * users .. Error: near line 5: no such table: users_nonesuch
# Verify script "verify/users.sql" failed.
not ok

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

SQLite is kind enough to tell us what the problem is. πŸ¦‹ Don't forget to change the table name back before continuing!


πŸ”„ Status, Revert, Log, Repeat πŸ”„

Purely for informational vibes, we can always see how a deployment was recorded with the status command, which reads the tables from the registry database:

> sqitch status db:sqlite:flipr_test.db
# On database db:sqlite:flipr_test.db
# Project:  flipr
# Change:   f30fe47f5f99501fb8d481e910d9112c5ac0a676
# Name:     users
# Deployed: 2013-12-31 10:26:59 -0800
# By:       Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

Let's make sure we can revert the change:

> sqitch  revert db:sqlite:flipr_test.db
Revert all changes from db:sqlite:flipr_test.db? [Yes]
      - users .. ok

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

        > sqlite3 flipr_test.db '.tables'

And the status message should reflect as much:

> sqitch status db:sqlite:flipr_test.db
# On database db:sqlite:flipr_test.db
No changes deployed

Of course, since nothing is deployed, the verify command has nothing to verify:

> sqitch verify db:sqlite:flipr_test.db
Verifying db:sqlite:flipr_test.db
No changes deployed

But we still have a record that the change happened, visible via the log command:

> sqitch log db:sqlite:flipr_test.db
On database db:sqlite:flipr_test.db
Revert f30fe47f5f99501fb8d481e910d9112c5ac0a676
Name:      users
Committer: Marge N. O’Vera <marge@example.com>
Date:      2013-12-31 10:53:25 -0800

    Creates table to track our users.

Deploy f30fe47f5f99501fb8d481e910d9112c5ac0a676
Name:      users
Committer: Marge N. O’Vera <marge@example.com>
Date:      2013-12-31 10:26:59 -0800

    Creates table to track our users.

Note that the actions we took are shown in reverse chronological order β€” revert first, then the deploy.

Cool. Let's tell Git to ignore *.db files and then commit it. 🌈

> echo '*.db' > .gitignore
> git add .
> git commit -m 'Add users table.'
[main 6725454] Add users table.
 5 files changed, 31 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 deploy/users.sql
 create mode 100644 revert/users.sql
 create mode 100644 verify/users.sql

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

> sqitch deploy db:sqlite:flipr_test.db --verify
Deploying changes to db:sqlite:flipr_test.db
      + users .. ok

And now the users table should be back:

> sqlite3 flipr_test.db '.tables'
users

When we look at the status, the deployment will be there:

> sqitch status db:sqlite:flipr_test.db
# On database db:sqlite:flipr_test.db
# Project:  flipr
# Change:   f30fe47f5f99501fb8d481e910d9112c5ac0a676
# Name:     users
# Deployed: 2013-12-31 10:57:55 -0800
# By:       Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

🎯 On Target 🎯

I'm getting a little tired of always having to type db:sqlite:flipr_test.db, aren't you? This database connection URI β€” https://github.com/libwww-perl/uri-db/ β€” 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 (https://git-scm.com/docs/git-remote), lets you manage one or more named deployment targets. We just added a target named flipr_test, which means we can use the string flipr_test for the target instead of 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, let's be real, we will eventually. But at least our examples get simpler from here on in, e.g.:

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   f30fe47f5f99501fb8d481e910d9112c5ac0a676
# Name:     users
# Deployed: 2013-12-31 10:57:55 -0800
# By:       Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

Yay, so much more concise. πŸ’– Let's also make sure changes get verified after deploying them:

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

We'll see the rebase command a bit later. πŸ‘‘ In the meantime, let's commit the new configuration and go make some more changes!

> git commit -am 'Set default target and always verify.'
[main 5fb57ec] Set default target and always verify.
 1 file changed, 8 insertions(+)

πŸ’– Deploy with Dependency πŸ’–

Okay bestie, time for change number two. Our app needs to store status messages from users, and we're gonna call them β€” and the table that holds them β€” "flips". First, add the change:

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

Notice how we're requiring the "users" change as a dependency of the new "flips" change? Like, yes, that change was already added to the plan and so it's gonna get applied before "flips" anyway, but being explicit about your dependencies is just good manners, fr fr.

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

-- Deploy flipr:flips to sqlite
-- requires: users

BEGIN;

CREATE TABLE flips (
        id        INTEGER   PRIMARY KEY AUTOINCREMENT,
        nickname  TEXT      NOT NULL REFERENCES users(nickname),
        body      TEXT      NOT NULL DEFAULT '' CHECK ( length(body) <= 180 ),
        timestamp DATETIME  NOT NULL DEFAULT CURRENT_TIMESTAMP
);

COMMIT;

Couple things to clock here. Line two? That's the dependence on the "users" change, listed out. It doesn't do anything, but the default deploy template puts it there for your reference while you're editing the file. Useful, right? 🫢

And the users.nickname column references the users table β€” that's literally why we need to require the "users" change.

Now the verify script. Again, all we're doing is a SELECT from the table. And I highly recommend selecting each column by name too, so you can be sure no column went missing. Here's verify/flips.sql:

-- Verify flipr:flips on sqlite

BEGIN;

SELECT id, nickname, body, timestamp
      FROM flips
 WHERE 0;

ROLLBACK;

Then the revert script: literally all we do is drop the table. Add this to revert/flips.sql:

-- Revert flipr:flips from sqlite

BEGIN;

DROP TABLE flips;

COMMIT;

Couldn't be much simpler, right? Let's deploy this bad boy:

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

And since verification is enabled, we know the table must've been created. But for the sake of visibility, let's take a lil peek:

> sqlite3 flipr_test.db '.tables'
flips  users

We can also check every currently deployed change with the verify command:

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

Now let's look at the status:

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   32ee57069c0d7fec52b6f86f453dc0c16bc1090a
# Name:     flips
# Deployed: 2013-12-31 11:02:51 -0800
# 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:

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

Note that we used the --to option to say which change to revert to. And what are we reverting to? The symbolic tag @HEAD, when you pass it to revert, always refers to the last change deployed to the database. (For other commands, it means the last change in the plan.) Slap a caret (^) on the end and that tells Sqitch to pick the change prior to the last deployed change. So we revert to "users", the penultimate change. The other lowkey-useful symbolic tag is @ROOT, which refers to the first change deployed to the database β€” or in the plan, depending on which command you're running.

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

> sqlite3 flipr_test.db '.tables'
users

The status command politely lets us know we've got undeployed changes:

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   f30fe47f5f99501fb8d481e910d9112c5ac0a676
# Name:     users
# Deployed: 2013-12-31 10:57:55 -0800
# By:       Marge N. O’Vera <marge@example.com>
#
Undeployed change:
  * flips

And so does verify:

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

And note the verify is successful, because all the currently-deployed changes are verified. The list of undeployed changes (just flips here) is there to remind us what state we're in.

Okay, let's commit and deploy again:

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

Looks good. Check the status:

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   32ee57069c0d7fec52b6f86f453dc0c16bc1090a
# Name:     flips
# Deployed: 2013-12-31 11:05:44 -0800
# By:       Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

πŸ¦‹ View to a Thrill πŸ¦‹

One more thing before we're ready to ship a first beta release. Let's make a view that lists usernames alongside their flips.

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

Now drop this SQL into deploy/userflips.sql:

CREATE VIEW userflips AS
SELECT f.id, u.nickname, u.fullname, f.body, f.timestamp
      FROM users u
      JOIN flips f ON u.nickname = f.nickname;

Add this SQL to verify/userflips.sql:

SELECT id, nickname, fullname, body, timestamp
      FROM userflips
 WHERE 0;

And add the DROP VIEW statement to revert/userflips.sql:

DROP VIEW userflips;

Now try it out!

> sqitch deploy
Deploying changes to flipr_test
  + userflips .. ok
> sqitch revert -y
Reverting all changes from flipr_test
  - userflips .. ok
  - flips ...... ok
  - users ...... ok
> sqitch deploy
Deploying changes to flipr_test
  + users ...... ok
  + flips ...... ok
  + userflips .. ok

Looks good! Commit it. πŸ’…

> git add .
> git commit -m 'Add the userflips view.'
[main c74bfb4] Add the userflips view.
 4 files changed, 29 insertions(+)
 create mode 100644 deploy/userflips.sql
 create mode 100644 revert/userflips.sql
 create mode 100644 verify/userflips.sql

πŸ‘‘ Ship It! πŸ‘‘

Now we're ready for the first development release of our app. Let's call it 1.0.0-dev1. And since we want 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 7a479fd] Tag the database with v1.0.0-dev1.
 1 file changed, 1 insertion(+)
> git tag v1.0.0-dev1 -am 'Tag v1.0.0-dev1'

We can test-deploy to make sure the tag gets picked up, like so:

> mkdir dev
> sqitch deploy db:sqlite:dev/flipr.db
Adding registry tables to db:sqlite:dev/sqitch.db
Deploying changes to db:sqlite:dev/flipr.db
  + users ................... ok
  + flips ................... ok
  + userflips @v1.0.0-dev1 .. ok

Great, both changes deployed and userflips got tagged with @v1.0.0-dev1. Let's peek at the status:

> sqitch status db:sqlite:dev/flipr.db
# On database db:sqlite:dev/flipr.db
# Project:  flipr
# Change:   60ee3aba0445bf3287f9dc1dd97b1877523fa139
# Name:     userflips
# Tag:      @v1.0.0-dev1
# Deployed: 2013-12-31 11:19:15 -0800
# By:       Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

See the tag listed as part of the status message? Iconic. Now let's bundle everything up for release:

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

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

> cd bundle
> sqitch deploy db:sqlite:flipr_prod.db
Adding registry tables to db:sqlite:sqitch.db
Deploying changes to db:sqlite:flipr_prod.db
  + users ................... ok
  + flips ................... ok
  + userflips @v1.0.0-dev1 .. ok

Looks pretty much the same as before, huh? Package it up and ship it! πŸš€

> rm *.db
> 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 πŸŽ€

okay so the app basics are done, which means it's new-feature o'clock. and obviously we need to track the hashtags on flips?? like, hashtags are the whole point of the internet?? so we're adding a table for them. BUT β€” other people are out here working on other tasks in this same repo, so we do NOT just barrel into main. we work on a branch, everybody stays out of everybody's way. branch time, bestie:

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

now we 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 [flips]" to sqitch.plan

you know the drill by now, no notes needed. put this in deploy/hashtags.sql:

CREATE TABLE hashtags (
        flip_id   INTEGER   NOT NULL REFERENCES flips(id),
        hashtag   TEXT      NOT NULL CHECK ( length(hashtag) > 0 ),
        PRIMARY KEY (flip_id, hashtag)
);

and again, we select from the table over in verify/hashtags.sql:

SELECT flip_id, hashtag FROM hashtags WHERE 0;

and in revert/hashtags.sql we just drop it:

DROP TABLE hashtags;

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:   1352464e8b5f3d5eeac76a1986379f07de43bffd
# Name:     hashtags
# Deployed: 2013-12-31 11:30:53 -0800
# By:       Marge N. O’Vera <marge@example.com>
#
# Tag:
#   @v1.0.0-dev1 - 2013-12-31 11:13:49 -0800 - Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)

see that --show-tags in there? that's what's showing you all the deployed tags, fr fr. and make sure the revert actually works too:

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

ate. now make it so:

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

good, this feature is finished. time to merge back into main. 🫢

Emergency 🚨

let's do it:

> git checkout main
Switched to branch 'main'
> git pull
Updating 7a479fd..47a4107
Fast-forward
 deploy/lists.sql | 13 +++++++++++++
 revert/lists.sql |  7 +++++++
 sqitch.plan      |  2 ++
 verify/lists.sql |  9 +++++++++
 4 files changed, 31 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 somebody else made changes to main β€” they added list support. okay 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 honestly, since both the merged lists branch AND our hashtags branch added changes to the plan. let's try a different approach.

the truth is β€” and i'm saying this with love β€” we got lazy. 😭 those changes when we pulled main from the origin should have raised a red flag. it's considered 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 47a4107 Merge branch 'lists'

that throws out our botched merge, bye bye. 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
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're gonna have 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

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-sqlite-intro/

users 2013-12-31T18:06:04Z Marge N. O’Vera <marge@example.com> # Creates table to track our users.
flips [users] 2013-12-31T19:01:40Z Marge N. O’Vera <marge@example.com> # Adds table for storing flips.
userflips [users flips] 2013-12-31T19:11:11Z Marge N. O’Vera <marge@example.com> # Creates the userflips view.
@v1.0.0-dev1 2013-12-31T19:13:02Z Marge N. O’Vera <marge@example.com> # Tag v1.0.0-dev1.

lists [users] 2013-12-31T19:28:05Z Marge N. O’Vera <marge@example.com> # Adds table for storing lists.
hashtags [flips] 2013-12-31T19:30:13Z 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
Deploying changes to flipr_test
  + 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 4f93ac4] Add `.gitattributes` with union merge for `sqitch.plan`.
 1 file changed, 1 insertion(+)
 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 | 12 ++++++++++++
 revert/hashtags.sql |  7 +++++++
 sqitch.plan         |  1 +
 verify/hashtags.sql |  7 +++++++
 5 files changed, 28 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-sqlite-intro/

users 2013-12-31T18:06:04Z Marge N. O’Vera <marge@example.com> # Creates table to track our users.
flips [users] 2013-12-31T19:01:40Z Marge N. O’Vera <marge@example.com> # Adds table for storing flips.
userflips [users flips] 2013-12-31T19:11:11Z Marge N. O’Vera <marge@example.com> # Creates the userflips view.
@v1.0.0-dev1 2013-12-31T19:13:02Z Marge N. O’Vera <marge@example.com> # Tag v1.0.0-dev1.

lists [users] 2013-12-31T19:28:05Z Marge N. O’Vera <marge@example.com> # Adds table for storing lists.
hashtags [flips] 2013-12-31T19:30:13Z Marge N. O’Vera <marge@example.com> # Adds table for storing hashtags.

much much better β€” a nice clean main now. 🧚 and because it's 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 7abfd9b] Tag the database with v1.0.0-dev2.
 1 file 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
  + 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 boom πŸ’₯ β€” some folks out there have been testing the 1.0.0-dev2 release and they straight up demanded that Twitter user links get added to Flipr pages. Bestie, why anyone would want to shove social network links into 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 this feature) so it includes the Twitter user names. Periodt.

Normally, modifying views in database changes is a total PITA (https://www.urbandictionary.com/define.php?term=pita). You end up doing 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, you can have Sqitch do most of the work for you 🫢. The only requirement is that a tag appears between the two instances of a change we want to modify. And honestly, you're gonna make a change like this right after a release, which you've tagged anyway, right? Well, we have β€” with @v1.0.0-dev2 added back 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 ✨. So how does Sqitch pull that off? 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 ahead of 'origin/main' by 4 commits.
#   (use "git push" to publish your local commits)
#
# 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 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 from here on out Sqitch will find them when it needs to run scripts for the first instance of the userflips change. So, super important: it's important not to change them again 🚫. But hey β€” if you're reworking the change, you shouldn't need to anyway.

The other thing to notice? 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 (https://en.wikipedia.org/wiki/Idempotence) β€” 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.

So 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:

@@ -4,8 +4,9 @@

 BEGIN;

+DROP VIEW IF EXISTS userflips;
 CREATE VIEW userflips AS
-SELECT f.id, u.nickname, u.fullname, f.body, f.timestamp
+SELECT f.id, u.nickname, u.fullname, u.twitter, f.body, f.timestamp
   FROM users u
   JOIN flips f ON u.nickname = f.nickname;

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

@@ -2,7 +2,7 @@

 BEGIN;

-SELECT id, nickname, fullname, body, timestamp
+SELECT id, nickname, fullname, twitter, body, timestamp
   FROM userflips
  WHERE 0;

And finally, modify revert/userflips.sql to drop the view before creating it:

@@ -4,6 +4,7 @@

 BEGIN;

+DROP VIEW IF EXISTS userflips;
 CREATE VIEW userflips AS
 SELECT f.id, u.nickname, u.fullname, f.body, f.timestamp
   FROM users u

Quick note: if we had included that statement when we originally created the userflips change, we wouldn't have to change this file at all 🀯.

Now try a deployment:

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

So… are the changes deployed?

> sqlite3 flipr_test.db '.schema userflips'
CREATE VIEW userflips AS
SELECT f.id, u.nickname, u.fullname, u.twitter, f.body, f.timestamp
  FROM users u
  JOIN flips f ON u.nickname = f.nickname;

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?

> sqlite3 flipr_test.db '.schema userflips'
CREATE VIEW userflips AS
SELECT f.id, u.nickname, u.fullname, f.body, f.timestamp
      FROM users u
      JOIN flips f ON u.nickname = f.nickname;

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 3eb96d9] Add the twitter column to the userflips view.
 7 files changed, 40 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, bestie β€” literally always evolving 🧚. 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 the whole page, bestie πŸ’– this was sqitch...-sqlite(3pm) from perl v5.40.1, dated 2026-07-26. Go forth and rework your changes in place like the icon you are βœ¨πŸ¦‹

πŸ“¦ where this page comes from