Skip to content

Latest commit

 

History

History
1414 lines (1071 loc) · 52.6 KB

sqitchtutorial-exasol.pod

File metadata and controls

1414 lines (1071 loc) · 52.6 KB

Name

sqitchtutorial-exasol - A tutorial introduction to Sqitch change management on Exasol

Synopsis

sqitch *

Description

This tutorial explains how to create a sqitch-enabled Exasol project, use a VCS for deployment planning, and work with other developers to make sure changes remain in sync and in the proper order.

We'll start by creating a new project from scratch, a fictional antisocial networking site called Flipr. All examples use Git as the VCS and Exasol as the storage engine, but for the most part you can substitute other VCSes and database engines in the examples as appropriate.

If you'd like to manage a PostgreSQL database, see sqitchtutorial.

If you'd like to manage an SQLite database, see sqitchtutorial-sqlite.

If you'd like to manage an Oracle database, see sqitchtutorial-oracle.

If you'd like to manage a MySQL database, see sqitchtutorial-mysql.

If you'd like to manage a Firebird database, see sqitchtutorial-firebird.

If you'd like to manage a Vertica database, see sqitchtutorial-vertica.

If you'd like to manage a Snowflake database, see sqitchtutorial-snowflake.

Connection Configuration

Sqitch requires ODBC to connect to the Exasol database. As such, you'll need to make sure that the Exasol ODBC driver is properly configured. At its simplest, on Unix-like systems, name the driver "Exasol" by adding this entry to odbcinst.ini (usually found in /etc, /usr/etc, or /usr/local/etc):

[Exasol]
Description = ODBC for Exasol
Driver      = /opt/EXASOL_ODBC-6.0.4/lib/linux/x86_64/libexaodbc-uo2214lv2.so

Note that you'll need to adjust the path depending on the version of the ODBC driver, and where you installed it.

You might also consider naming your database connection by putting an entry in ~/.odbc.ini, like so (assuming that Exasol is running on your local host):

[flipr_test]
Driver = Exasol
EXAHOST = 127.0.0.1:8563
EXAUID = sys
EXAPWD = exasol

Putting user and password information here is optional, but probably safer than other available options as long as the file is protected (mode 0600) so that only you can read it.

Normally, of course, you'd have a separate user per project (and a separate ODBC connection defined); the above example is taken from the simplified tutorial setup we're using.

See the Exasol downloads for ODBC drivers, documentation, etc.

Database Setup

While installing and configuring an Exasol instance is beyond the scope of this tutorial, if you just want to follow along, you can start a Docker instance:

> docker run --detach --privileged --stop-timeout 120 -p 127.0.0.1:8563:8888  exasol/docker-db:latest

This will make Exasol available at 127.0.0.1 (localhost) on port 8563, as if you were running it directly on your machine. You may have to wait for a minute until the database finished initializing; you can try connecting with `exaplus` to check status.

See the corresponding GitHub repository for more information about how to run Exasol using Docker, for example if you want your data to persist beyond the lifetime of the container, etc.

Starting a New Project

Usually the first thing to do when starting a new project is to create a source code repository. 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 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, and will prevent the deployment of a different project with the same name. So let's specify one when we initialize Sqitch:

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

Let's have a look at sqitch.conf:

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

Good, it picked up on the fact that we're creating changes for the Exasol engine, thanks to the --engine exasol option, and saved it to the file. Furthermore, it wrote a commented-out [engine "exasol"] section with all the available Exasol 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 Exasol's exaplus client is not in the path on my system, let's go ahead an tell it where to find the client on our computer (don't bother if you're using the Docker image because it uses the client inside the container, not on your host machine):

> sqitch config --user engine.exasol.client /opt/EXAplus/exaplus

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

> sqitch config --user user.name 'Marge N. O’Vera'
> sqitch config --user user.email '[email protected]'

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

> cat ~/.sqitch/sqitch.conf
[engine "exasol"]
        client = /opt/EXAplus/exaplus
[user]
        name = Marge N. O’Vera
        email = [email protected]

Which means that Sqitch should be able to find exaplus for any project, and that it will always properly identify us when planning and committing changes.

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

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.

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

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

Our First Change

First, our project will need a schema. This creates a nice 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 deploy, revert, and verify scripts that represent the change. Now we edit these 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 to the deploy script, 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, assuming the default sys user and an ODBC driver named Exasol (see "Connection Configuration" for details):

> sqitch deploy 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
Adding registry tables to db:exasol://sys:@localhost:8563/?Driver=Exasol
Deploying changes to db:exasol://sys:@localhost:8563/?Driver=Exasol
  + appschema .. ok

(If you see an error resolving the TLS certificate, you can disable certificate verification by adding &SSLCERTIFICATE=SSL_VERIFY_NONE to the end of the URL. Only do this for testing!)

First Sqitch created registry tables used to track database changes. The structure and name of the registry varies between databases (Exasol uses a schema to namespace its registry, while SQLite and MySQL use separate databases). Next, Sqitch deploys changes. 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:

> exaplus -q -u sys -p exasol -c localhost:8563 -sql "select schema_name from exa_schemas;"

SCHEMA_NAME
--------------------------------------------------------------------------------------------------------------------------------
SQITCH
FLIPR

Trust, But Verify

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. In Exasol, the simplest way to do so for schema is probably to simply create an object in the schema. Put this SQL into verify/appschema.sql:

CREATE TABLE flipr.verify__ (id int);
DROP   TABLE flipr.verify__;

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:

SELECT 1/COUNT(*) FROM exa_schemas WHERE schema_name = 'FLIPR';

Either way, run the verify script with the verify command:

> sqitch verify 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
Verifying db:exasol://sys:@localhost:8563/?Driver=Exasol
  * appschema .. ok
Verify successful

Looks good! If you want to make sure that the verify script correctly dies if the schema doesn't exist, temporarily change the schema name in the script to something that doesn't exist, something like:

CREATE TABLE nonesuch.verify__ (id int);

Then verify again:

> sqitch verify 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
Verifying db:exasol://sys:@localhost:8563/?Driver=Exasol
* appschema .. Error: [42000] schema NONESUCH not found [line 1, column 40] (Session: 1582884049218108749)

# Verify script "verify/appschema.sql" failed.
not ok

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

It's even nice enough to tell us what the problem is. Or, for the divide-by-zero example, change the schema name:

SELECT 1/COUNT(*) FROM exa_schemas WHERE schema_name = 'nonesuch';

Then the verify will look something like:

> sqitch verify 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
Verifying db:exasol://sys:@localhost:8563/?Driver=Exasol
* appschema .. Error: [22012] data exception - division by zero (Session: 1582884446489810101)

# Verify script "verify/appschema.sql" failed.
not ok

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

Less useful error output, but enough to alert us that something has gone wrong.

Don't forget to change the schema name back before continuing!

Status, Revert, Log, Repeat

For purely informational purposes, we can always see how a deployment was recorded via the status command, which reads the registry tables from the database:

> sqitch status 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
# On database db:exasol://sys:@localhost:8563/?Driver=Exasol
# Project:  flipr
# Change:   f9759f0ed77964b6a3b6c7aa3b6058b4bb7db764
# Name:     appschema
# Deployed: 2014-09-04 15:26:28 -0700
# By:       Marge N. O’Vera <[email protected]>
#
Nothing to deploy (up-to-date)

Let's make sure that we can revert the change:

> sqitch revert 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
Revert all changes from db:exasol://sys:@localhost:8563/?Driver=Exasol? [Yes]
  - appschema .. ok

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:

> exaplus -q -u sys -p exasol -c localhost:8563 -sql "select schema_name from exa_schemas;"

SCHEMA_NAME
--------------------------------------------------------------------------------------------------------------------------------
SQITCH

And the status message should reflect as much:

> sqitch status 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
# On database db:exasol://sys:@localhost:8563/?Driver=Exasol
No changes deployed

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

> sqitch verify 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
Verifying db:exasol://sys:@localhost:8563/?Driver=Exasol
No changes deployed

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

> sqitch log 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
On database db:exasol://sys:@localhost:8563/?Driver=Exasol
Revert f9759f0ed77964b6a3b6c7aa3b6058b4bb7db764
Name:      appschema
Committer: Marge N. O’Vera <[email protected]>
Date:      2014-09-04 16:33:02 -0700

    Add schema for all flipr objects.

Deploy f9759f0ed77964b6a3b6c7aa3b6058b4bb7db764
Name:      appschema
Committer: Marge N. O’Vera <[email protected]>
Date:      2014-09-04 15:26:28 -0700

    Add schema for all flipr objects.

Note that the actions we took are shown in reverse chronological order, with the revert first and then the deploy.

Cool. Now let's commit it.

> git add .
> git commit -m 'Add flipr schema.'
[main 9bee4bd] Add flipr schema.
 5 files changed, 197 insertions(+), 0 deletions(-)
 create mode 100644 deploy/appschema.sql
 create mode 100644 revert/appschema.sql
 create mode 100644 sqitch.sql
 create mode 100644 verify/appschema.sql

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

> sqitch deploy --verify 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
Deploying changes to db:exasol://sys:@localhost:8563/?Driver=Exasol
  + appschema .. ok

And now the schema should be back:

> exaplus -q -u sys -p exasol -c localhost:8563 -sql "select schema_name from exa_schemas;"

SCHEMA_NAME
--------------------------------------------------------------------------------------------------------------------------------
SQITCH
FLIPR

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

> sqitch status 'db:exasol://sys:exasol@localhost:8563/?Driver=Exasol'
# On database db:exasol://sys:@localhost:8563/?Driver=Exasol
# Project:  flipr
# Change:   fef4c2911ae68aee8f6ea164293a32923dc13b67
# Name:     appschema
# Deployed: 2014-09-04 16:37:38 -0700
# By:       Marge N. O’Vera <[email protected]>
#
Nothing to deploy (up-to-date)

On Target

I'm getting a little tired of always having to type db:exasol://sys:exasol@localhost:8563/?Driver=Exasol, 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:exasol://sys:exasol@localhost:8563/?Driver=Exasol'

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 exasol 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.:

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   f9759f0ed77964b6a3b6c7aa3b6058b4bb7db764
# Name:     appschema
# Deployed: 2014-09-04 16:37:38 -0700
# By:       Marge N. O’Vera <[email protected]>
#
Nothing to deploy (up-to-date)

Yay, that allows things to be a little more concise. Let's also make sure that changes are 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 and make some more changes!

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

Deploy with Dependency

Let's add another change, this time to create a table. Our app will need users, of course, so we'll create a table for them. First, add the new 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

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

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

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

CREATE TABLE flipr.users (
    nickname  VARCHAR(64)  PRIMARY KEY,
    password  VARCHAR(256) NOT NULL,
    fullname  VARCHAR(256) NOT NULL,
    twitter   VARCHAR(256) NOT NULL,
    ts        TIMESTAMP WITH LOCAL TIME ZONE DEFAULT NOW() NOT NULL
);

COMMIT;

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

The table itself will be created in the flipr schema. This is why we need to require the appschema change.

Now for the verify script. The simplest way to check that the table was created and has the expected columns without touching the data? Just select from the table with a false WHERE clause. Add this to verify/users.sql:

SELECT nickname, password, fullname, twitter, ts
  FROM flipr.users
 WHERE FALSE;

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

DROP TABLE flipr.users;

Couldn't be much simpler, right? 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 purposes of visibility, let's have a quick look:

> exaplus -q -u sys -p exasol -c localhost:8563 -sql "describe flipr.users;"

COLUMN_NAME                                                                                                                      SQL_TYPE                                 NULLABLE DISTRIBUTION_KEY
-------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------- -------- ----------------
NICKNAME                                                                                                                         VARCHAR(64) UTF8                         FALSE    FALSE
PASSWORD                                                                                                                         VARCHAR(256) UTF8                        FALSE    FALSE
FULLNAME                                                                                                                         VARCHAR(256) UTF8                        FALSE    FALSE
TWITTER                                                                                                                          VARCHAR(256) UTF8                        FALSE    FALSE
TS                                                                                                                               TIMESTAMP WITH LOCAL TIME ZONE           FALSE    FALSE

We can also verify all currently deployed changes with the verify command:

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

Now have a look at the status:

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   794a6c78816543909d592e2e9f5c0fade5b47406
# Name:     users
# Deployed: 2017-11-02 11:02:40 +0100
# By:       Marge N. O’Vera <[email protected]>
#
Nothing to deploy (up-to-date)

Success! Let's make sure we can revert the change, as well:

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

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 appschema, 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 users table should be gone but the flipr schema should still be around:

> exaplus -q -u sys -p exasol -c localhost:8563 -sql "describe flipr.users;"
Error: [42000] table or view FLIPR.USERS not found [line 1, column 10] (Session: 1582958508294847446)

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

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   f9759f0ed77964b6a3b6c7aa3b6058b4bb7db764
# Name:     appschema
# Deployed: 2014-09-04 16:37:38 -0700
# By:       Marge N. O’Vera <[email protected]>
#
Undeployed change:
  * users

As 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. The list of undeployed changes (just "users" here) reminds us about the current state.

Okay, let's commit and deploy again:

> git add .
> git commit -am 'Add users table.'
[main c7c24c5] Add users table.
 4 files changed, 18 insertions(+), 0 deletions(-)
 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
# On database flipr_test
# Project:  flipr
# Change:   d647ac8c130a7e0b12c9049789e46afb4a4f6e53
# Name:     users
# Deployed: 2014-09-04 17:42:53 -0700
# By:       Marge N. O’Vera <[email protected]>
#
Nothing to deploy (up-to-date)

Excellent. Let's do some more!

Add Two at Once

Let's add a couple more changes. Our app will need to store status messages from users. Let's call them -- and the table to store them -- "flips". And we'll also need a view that lists user names with their flips. Let's add changes for them 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 might be a good time to have a look at the deployment plan:

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

appschema 2014-09-04T18:40:34Z Marge N. O’Vera <[email protected]> # Add schema for all flipr objects.
users [appschema] 2014-09-04T23:40:15Z Marge N. O’Vera <[email protected]> # Creates table to track our users.
flips [appschema users] 2014-09-05T00:16:58Z Marge N. O’Vera <[email protected]> # Adds table for storing flips.
userflips [appschema users flips] 2014-09-05T00:18:43Z Marge N. O’Vera <[email protected]> # Creates the userflips view.

Each change appears on a single line with 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.

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

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

CREATE TABLE flipr.flips (
    id        INTEGER        IDENTITY PRIMARY KEY,
    nickname  VARCHAR(64)    NOT NULL REFERENCES flipr.users(nickname),
    body      VARCHAR(180)   DEFAULT '' NOT NULL,
    ts        TIMESTAMP WITH LOCAL TIME ZONE DEFAULT NOW() NOT NULL
);

COMMIT;

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

-- Verify flipr:flips on exasol

SELECT id, nickname, body, ts
  FROM flipr.flips
 WHERE FALSE;

ROLLBACK;

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

-- Revert flipr:flips from exasol

DROP TABLE flipr.flips;

COMMIT;

Now for userflips; deploy/userflips.sql might look like this:

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

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

COMMIT;

Use a SELECT statement in verify/userflips.sql again:

-- Verify flipr:userflips on exasol

SELECT id, nickname, fullname, body, ts
  FROM flipr.userflips
 WHERE FALSE;

ROLLBACK;

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

-- Revert flipr:userflips from exasol

DROP VIEW flipr.userflips;

COMMIT;

Try em out!

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

Do we have the new table and view? Of course we do, they were verified. Still, have a look:

> exaplus -q -u sys -p exasol -c localhost:8563 -sql "select table_name from exa_all_tables where table_schema = 'FLIPR';"

TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------
USERS
FLIPS

> exaplus -q -u sys -p exasol -c localhost:8563 -sql "describe flipr.userflips;"

COLUMN_NAME                                                                                                                      SQL_TYPE                                 NULLABLE DISTRIBUTION_KEY
-------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------- -------- ----------------
ID                                                                                                                               DECIMAL(18,0)
NICKNAME                                                                                                                         VARCHAR(64) UTF8
FULLNAME                                                                                                                         VARCHAR(256) UTF8
BODY                                                                                                                             VARCHAR(180) UTF8
TS                                                                                                                               TIMESTAMP WITH LOCAL TIME ZONE

And what's the status?

> sqitch status
# On database flipr_test
# Project:  flipr
# Change:   d1f998618fb863d93049a724fd0d2b49a29add86
# Name:     userflips
# Deployed: 2014-09-04 17:51:21 -0700
# By:       Marge N. O’Vera <[email protected]>
#
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
> exaplus -q -u sys -p exasol -c localhost:8563 -sql "describe flipr.flips;"
Error: [42000] table or view FLIPR.FLIPS not found [line 1, column 10] (Session: 1582955242708359302)
> exaplus -q -u sys -p exasol -c localhost:8563 -sql "describe flipr.userflips;"
Error: [42000] table or view FLIPR.USERFLIPS not found [line 1, column 10] (Session: 1582955248116468907)

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 c40f23f] Add flips table and userflips view.
 7 files changed, 41 insertions(+), 0 deletions(-)
 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
# On database flipr_test
# Project:  flipr
# Change:   d1f998618fb863d93049a724fd0d2b49a29add86
# Name:     userflips
# Deployed: 2014-09-04 17:59:34 -0700
# By:       Marge N. O’Vera <[email protected]>
#
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 to have it 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 b07ce3d] Tag the database with v1.0.0-dev1.
 1 files changed, 1 insertions(+), 0 deletions(-)
> 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:   d1f998618fb863d93049a724fd0d2b49a29add86
# Name:     userflips
# Tag:      @v1.0.0-dev1
# Deployed: 2014-09-04 17:59:34 -0700
# By:       Marge N. O’Vera <[email protected]>
#
Nothing to deploy (up-to-date)

Note the 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. 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. First, start up a new Docker instance on a different port:

> docker run --detach --privileged --stop-timeout 120 -p 127.0.0.1:9999:8888  exasol/docker-db:latest

Now you should be able to deploy to the new database:

> cd bundle
> sqitch deploy 'db:exasol://sys:exasol@localhost:9999/?Driver=Exasol'
Adding registry tables to db:exasol://sys:@localhost:9999/?Driver=Exasol
Deploying changes to db:exasol://sys:@localhost:9999/?Driver=Exasol
  + appschema ............... ok
  + users ................... ok
  + flips ................... ok
  + userflips @v1.0.0-dev1 .. ok

Notice how the tag on userflips now appears 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? 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. 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:   fda6daef73e0ac12252bf6af5f259ccb207d4197
# Name:     hashtags
# Deployed: 2014-09-05 10:46:20 -0700
# By:       Marge N. O’Vera <[email protected]>
#
# Tag:
#   @v1.0.0-dev1 - 2014-09-05 09:09:38 -0700 - Marge N. O’Vera <[email protected]>
#
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 rebase -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 d893e9c] Add hashtags table.
 4 files changed, 18 insertions(+), 0 deletions(-)
 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 b07ce3d..05d3e5d
Fast-forward
 deploy/lists.sql |   10 ++++++++++
 revert/lists.sql |    3 +++
 sqitch.plan      |    2 ++
 verify/lists.sql |    5 +++++
 4 files changed, 20 insertions(+), 0 deletions(-)
 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 05d3e5d 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...
<stdin>:16: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
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.

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore 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
HEAD is now at d893e9c 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...
<stdin>:16: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
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-exasol-intro/

appschema 2014-09-04T18:40:34Z Marge N. O’Vera <[email protected]> # Add schema for all flipr objects.
users [appschema] 2014-09-04T23:40:15Z Marge N. O’Vera <[email protected]> # Creates table to track our users.
flips [appschema users] 2014-09-05T00:16:58Z Marge N. O’Vera <[email protected]> # Adds table for storing flips.
userflips [appschema users flips] 2014-09-05T00:18:43Z Marge N. O’Vera <[email protected]> # Creates the userflips view.
@v1.0.0-dev1 2014-09-05T16:04:48Z Marge N. O’Vera <[email protected]> # Tag v1.0.0-dev1.

lists [appschema users] 2014-09-05T17:33:43Z Marge N. O’Vera <[email protected]> # Adds table for storing lists.
hashtags [appschema flips] 2014-09-05T17:39:53Z Marge N. O’Vera <[email protected]> # 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 2f065a3] 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 recursive.
 .gitattributes      |    1 +
 deploy/hashtags.sql |   10 ++++++++++
 revert/hashtags.sql |    3 +++
 sqitch.plan         |    1 +
 verify/hashtags.sql |    3 +++
 5 files changed, 18 insertions(+), 0 deletions(-)
 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-exasol-intro/

appschema 2014-09-04T18:40:34Z Marge N. O’Vera <[email protected]> # Add schema for all flipr objects.
users [appschema] 2014-09-04T23:40:15Z Marge N. O’Vera <[email protected]> # Creates table to track our users.
flips [appschema users] 2014-09-05T00:16:58Z Marge N. O’Vera <[email protected]> # Adds table for storing flips.
userflips [appschema users flips] 2014-09-05T00:18:43Z Marge N. O’Vera <[email protected]> # Creates the userflips view.
@v1.0.0-dev1 2014-09-05T16:04:48Z Marge N. O’Vera <[email protected]> # Tag v1.0.0-dev1.

lists [appschema users] 2014-09-05T17:33:43Z Marge N. O’Vera <[email protected]> # Adds table for storing lists.
hashtags [appschema flips] 2014-09-05T17:39:53Z Marge N. O’Vera <[email protected]> # 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 8a6a73b] Tag the database with v1.0.0-dev2.
 1 files changed, 1 insertions(+), 0 deletions(-)
> 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

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:

  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 twiter column.

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

> sqitch rework userflips -n 'Adds userflips.twitter.'
Added "userflips [[email protected]]" 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
# Changed but not updated:
#   (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/[email protected]
#       revert/[email protected]
#       verify/[email protected]
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 [email protected]. 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 -- 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/[email protected].

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:

@@ -4,6 +4,6 @@
 -- requires: flips

 CREATE OR REPLACE VIEW flipr.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 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 exasol

-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?

> exaplus -q -u sys -p exasol -c localhost:8563 -sql "describe flipr.userflips;"

COLUMN_NAME                                                                                                                      SQL_TYPE                                 NULLABLE DISTRIBUTION_KEY
-------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------- -------- ----------------
ID                                                                                                                               DECIMAL(18,0)
NICKNAME                                                                                                                         VARCHAR(64) UTF8
FULLNAME                                                                                                                         VARCHAR(256) UTF8
TWITTER                                                                                                                          VARCHAR(256) UTF8
BODY                                                                                                                             VARCHAR(180) UTF8
TS                                                                                                                               TIMESTAMP WITH LOCAL TIME ZONE

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?

> exaplus -q -u sys -p exasol -c localhost:8563 -sql "describe flipr.userflips;"

COLUMN_NAME                                                                                                                      SQL_TYPE                                 NULLABLE DISTRIBUTION_KEY
-------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------- -------- ----------------
ID                                                                                                                               DECIMAL(18,0)
NICKNAME                                                                                                                         VARCHAR(64) UTF8
FULLNAME                                                                                                                         VARCHAR(256) UTF8
BODY                                                                                                                             VARCHAR(180) UTF8
TS                                                                                                                               TIMESTAMP WITH LOCAL TIME ZONE

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 95d6dd0] Add the twitter column to the userflips view.
 7 files changed, 30 insertions(+), 4 deletions(-)
 create mode 100644 deploy/[email protected]
 create mode 100644 revert/[email protected]
 create mode 100644 verify/[email protected]

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 <[email protected]>

License

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

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.