.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "sqitchtutorial-oracle 3pm"
.TH sqitchtutorial-oracle 3pm 2024-02-08 "perl v5.38.2" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH Name
.IX Header "Name"
sqitchtutorial-oracle \- A tutorial introduction to Sqitch change management on Oracle
.SH Synopsis
.IX Header "Synopsis"
.Vb 1
\& sqitch *
.Ve
.SH Description
.IX Header "Description"
This tutorial explains how to create a sqitch-enabled Oracle project, use a
VCS for deployment planning, and work with other developers to make sure
changes remain in sync and in the proper order.
.PP
We'll start by creating new project from scratch, a fictional antisocial
networking site called Flipr. All examples use Git as
the VCS and Oracle as the
storage engine. Note that you will need to set
\&\f(CW$ORACLE_HOME\fR so that all the
database connections will work.
.PP
If you'd like to manage a PostgreSQL database, see sqitchtutorial.
.PP
If you'd like to manage an SQLite database, see sqitchtutorial-sqlite.
.PP
If you'd like to manage a MySQL database, see sqitchtutorial-mysql.
.PP
If you'd like to manage a Firebird database, see sqitchtutorial-firebird.
.PP
If you'd like to manage a Vertica database, see sqitchtutorial-vertica.
.PP
If you'd like to manage an Exasol database, see sqitchtutorial-exasol.
.PP
If you'd like to manage a Snowflake database, see sqitchtutorial-snowflake.
.SS Prerequisites
.IX Subsection "Prerequisites"
Sqitch requires Oracle Instant Client
and the DBD::Oracle Perl module to manage Oracle databases. Skip this section
if you already have them installed and configured.
.PP
\fIInstant Client\fR
.IX Subsection "Instant Client"
.PP
Sqitch requires three Instant Client packages:
.IP 1. 4
Instant Client Basic
.IP 2. 4
Instant Client SQL*Plus
.IP 3. 4
Instant Client SDK
.PP
Download all three as appropriate for your platform and OS, unpack them and
put them all in a single directory, such as \fIinstantclient\fR in your home
directory. Then set the \f(CW$ORACLE_HOME\fR environment variable to that directory
and add it to the path the PATH and LD_LIBRARY_PATH variables. For example:
.PP
.Vb 3
\& export ORACLE_HOME=$HOME/instantclient
\& export PATH=$ORACLE_HOME:$PATH
\& export LD_LIBRARY_PATH=$ORACLE_HOME
.Ve
.PP
\fIDBD::Oracle\fR
.IX Subsection "DBD::Oracle"
.PP
With these variables in place, install DBD::Oracle, like so:
.PP
.Vb 1
\& cpanm DBD::Oracle
.Ve
.PP
If you run into issues, consult DBD::Oracle::Troubleshooting and the
relevant platform-specific \f(CW\*(C`DBD::Oracle::Troubleshooting::*\*(C'\fR guides
listed here .
.SS "Test Environment"
.IX Subsection "Test Environment"
If you have an Oracle instance ready to hand, you can skip this section. For
For those who don't have a handy Oracle instance lying around, try using one
of the Oracle-XE Docker images
or the Database Virtual Box Appliance .
Details on each follow.
.PP
\fIOracle-XE Docker Configuration\fR
.IX Subsection "Oracle-XE Docker Configuration"
.PP
The simplest way to the Sqitch Oracle engine is with the
[gvenzl/oracle\-xe](https://hub.docker.com/r/gvenzl/oracle\-xe) docker image.
Essentially, start it like so:
.PP
.Vb 5
\& docker run \-d \-p 1521:1521 \e
\& \-e ORACLE_PASSWORD=oracle \e
\& \-e APP_USER=scott \e
\& \-e APP_USER_PASSWORD=tiger \e
\& gvenzl/oracle\-xe:18\-slim
.Ve
.PP
This will create a user (and schema) named \f(CW\*(C`scott\*(C'\fR in the pluggable database \f(CW\*(C`pdb1\*(C'\fR.
To create a SID named \f(CW\*(C`flipr_test\*(C'\fR pointing to the \f(CW\*(C`pdb1\*(C'\fR database, add this entry to
\&\fR\f(CI$ORACLE_HOME\fR\fI/network/admin/tnsnames.ora\fR:
.PP
.Vb 8
\& FLIPR_TEST =
\& (DESCRIPTION =
\& (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
\& (CONNECT_DATA =
\& (SERVER = DEDICATED)
\& (SERVICE_NAME = pdb1)
\& )
\& )
.Ve
.PP
\fIVirtual Box Configuration\fR
.IX Subsection "Virtual Box Configuration"
.PP
Some instructions for setting up the Database Virtual Box Appliance for
following along in this tutorial.
.IP \(bu 4
See \fIt/oracle.t\fR for instructions on downloading, installing, and configuring
the Oracle developer days VM.
.IP \(bu 4
Download and install VirtualBox .
.IP \(bu 4
Download the VM from the Database Virtual Box
Appliance
page and import it into VirtualBox.
.IP \(bu 4
Once the VM is imported into VirtualBox and started, login with the username
"oracle" and the password "oracle". Then, in VirtualBox, go to Settings \->
Network, select the NAT adapter, and add two port forwarding rules
(https://barrymcgillin.blogspot.com/2011/12/using\-oracle\-developer\-days\-virtualbox.html):
.Sp
.Vb 4
\& Host Port | Guest Port
\& \-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-
\& 1521 | 1521
\& 2222 | 22
.Ve
.Sp
Then restart the VM. You should then be able to connect from your host with:
.Sp
.Vb 1
\& sqlplus sys/oracle@localhost/ORCL as sysdba
.Ve
.Sp
If this fails with either of these errors:
.Sp
.Vb 2
\& ORA\-01017: invalid username/password; logon denied ORA\-21561: OID
\& generation failed
.Ve
.Sp
Make sure that your computer's hostname is on the localhost line of \fI/etc/hosts\fR
(reference ):
.Sp
.Vb 4
\& > hostname
\& stickywicket
\& > grep 127 /etc/hosts
\& 127.0.0.1 localhost stickywicket
.Ve
.IP \(bu 4
Give user \f(CW\*(C`scott\*(C'\fR the access it needs:
.Sp
.Vb 2
\& ALTER USER scott IDENTIFIED BY tiger;
\& GRANT ALL PRIVILEGES TO scott;
.Ve
.IP \(bu 4
To create a SID named \f(CW\*(C`flipr_test\*(C'\fR pointing to the \f(CW\*(C`pdb1\*(C'\fR database, add this entry to
\&\fR\f(CI$ORACLE_HOME\fR\fI/network/admin/tnsnames.ora\fR:
.Sp
.Vb 8
\& FLIPR_TEST =
\& (DESCRIPTION =
\& (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
\& (CONNECT_DATA =
\& (SERVER = DEDICATED)
\& (SERVICE_NAME = orcl)
\& )
\& )
.Ve
.SH "Starting a New Project"
.IX Header "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:
.PP
.Vb 10
\& > mkdir flipr
\& > cd flipr
\& > git init .
\& Initialized empty Git repository in /flipr/.git/
\& > touch README.md
\& > git add .
\& > git commit \-am \*(AqInitialize project, add README.\*(Aq
\& [main (root\-commit) 1bd134b] Initialize project, add README.
\& 1 file changed, 38 insertions(+)
\& create mode 100644 README.md
.Ve
.PP
If you're a Git user and want to follow along the history, the repository used
in these examples is on GitHub .
.PP
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:
identifiers internally, and will prevent the deployment of a different project
with the same name. So let's specify one when we initialize Sqitch:
.PP
.Vb 5
\& Created sqitch.conf
\& Created sqitch.plan
\& Created deploy/
\& Created revert/
\& Created verify/
.Ve
.PP
Let's have a look at \fIsqitch.conf\fR:
.PP
.Vb 9
\& > cat sqitch.conf
\& [core]
\& engine = oracle
\& # plan_file = sqitch.plan
\& # top_dir = .
\& # [engine "oracle"]
\& # target = db:oracle:
\& # registry =
\& # client = sqlplus
.Ve
.PP
Good, it picked up on the fact that we're creating changes for the Oracle
engine, thanks to the \f(CW\*(C`\-\-engine oracle\*(C'\fR option, and saved it to the file.
Furthermore, it wrote a commented-out \f(CW\*(C`[engine "oracle"]\*(C'\fR section with all
the available Oracle engine-specific settings commented out and ready to be
edited as appropriate. This includes the path to
SQL*Plus in my \f(CW$ORACLE_HOME\fR.
.PP
By default, Sqitch will read \fIsqitch.conf\fR in the current directory for
settings. But it will also read \fI~/.sqitch/sqitch.conf\fR for user-specific
settings. Let's tell it who we are, since this data will be used in all of our
projects:
.PP
.Vb 2
\& > sqitch config \-\-user user.name \*(AqMarge N. O’Vera\*(Aq
\& > sqitch config \-\-user user.email \*(Aqmarge@example.com\*(Aq
.Ve
.PP
Have a look at \fI~/.sqitch/sqitch.conf\fR and you'll see this:
.PP
.Vb 4
\& > cat ~/.sqitch/sqitch.conf
\& [user]
\& name = Marge N. O’Vera
\& email = marge@example.com
.Ve
.PP
Which means that Sqitch will always properly identify us when planning and
committing changes. Back to the repository. Have a look at the plan file,
\&\fIsqitch.plan\fR:
.PP
.Vb 4
\& > cat sqitch.plan
\& %syntax\-version=1.0.0
\& %project=flipr
\& %uri=https://github.com/sqitchers/sqitch\-oracle\-intro/
.Ve
.PP
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
\&\f(CW\*(C`%syntax\-version\*(C'\fR pragma is always set by Sqitch, so that it always knows how
to parse the plan, even if the format changes in the future.
.PP
Let's commit these changes and start creating the database changes.
.PP
.Vb 6
\& > git add .
\& > git commit \-am \*(AqInitialize Sqitch configuration.\*(Aq
\& [main bd82f41] Initialize Sqitch configuration.
\& 2 files changed, 19 insertions(+)
\& create mode 100644 sqitch.conf
\& create mode 100644 sqitch.plan
.Ve
.SH "Our First Change"
.IX Header "Our First Change"
First, our project will need an Oracle user and accompanying schema. This
creates a nice namespace for all of the objects that will be part of the flipr
app. Run this command:
.PP
.Vb 5
\& > sqitch add appschema \-n \*(AqApp user and schema for all flipr objects.\*(Aq
\& Created deploy/appschema.sql
\& Created revert/appschema.sql
\& Created verify/appschema.sql
\& Added "appschema" to sqitch.plan
.Ve
.PP
The \f(CW\*(C`add\*(C'\fR 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 \f(CW\*(C`deploy\*(C'\fR script's job is to create the user. So we add
this to \fIdeploy/appschema.sql\fR:
.PP
.Vb 1
\& CREATE USER flipr IDENTIFIED BY whatever;
.Ve
.PP
The \f(CW\*(C`revert\*(C'\fR script's job is to precisely revert the change to the deploy
script, so we add this to \fIrevert/appschema.sql\fR:
.PP
.Vb 1
\& DROP USER flipr;
.Ve
.PP
Now we can try deploying this change. Before going any further, unless you're
using the Docker or VM environments described above, you might need to
create the database
and configure the SID. Assuming you have an Oracle SID named \f(CW\*(C`FLIPR_TEST\*(C'\fR set
up in your \f(CW\*(C`\fR\f(CITNSNAMES.ORA\fR\f(CW|https://www.orafaq.com/wiki/Tnsnames.ora\*(C'\fR file,
tell Sqitch where to send the change via a
database URI , such as
.PP
.Vb 1
\& export SQITCH_URI=db:oracle://$username:$password@/flipr_test
.Ve
.PP
With that URI set up, we can deploy:
.PP
.Vb 4
\& > sqitch deploy db:oracle://scott:tiger@/flipr_test
\& Adding registry tables to db:oracle://scott:@/flipr_test
\& Deploying changes to db:oracle://scott:@/flipr_test
\& + appschema .. ok
.Ve
.PP
First Sqitch created the registry tables used to track database changes. The
structure and name of the registry varies between databases, but in Oracle
they are simply stored in the current schema \-\- that is, the schema with the
same name as the user you've connected as. In this example, that schema is
\&\f(CW\*(C`scott\*(C'\fR. Ideally, only Sqitch data will be stored in this schema, so it
probably makes the most sense to create a superuser named \f(CW\*(C`sqitch\*(C'\fR or
something similar and use it to deploy changes.
.PP
If you'd like it to use a different database as the registry database, use
\&\f(CW\*(C`sqitch engine add oracle $name\*(C'\fR to configure it (or via the
\&\f(CW\*(C`target\*(C'\fR command; more below). This will be
useful if you don't want to use the same registry database to manage multiple
databases on the same server.
.PP
Next, Sqitch deploys changes to the target database, which we specified on the
command-line. We only have one change so far; the \f(CW\*(C`+\*(C'\fR reinforces the idea
that the change is being \fIadded\fR to the database.
.PP
With this change deployed, if you connect to the database, you'll be able to
see the schema:
.PP
.Vb 5
\& > echo "SELECT username FROM all_users WHERE username = \*(AqFLIPR\*(Aq;" \e
\& | sqlplus \-S scott/tiger@flipr_test
\& USERNAME
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& FLIPR
.Ve
.SS "Trust, But Verify"
.IX Subsection "Trust, But Verify"
But that's too much work. Do you really want to do something like that after
every deploy?
.PP
Here's where the \f(CW\*(C`verify\*(C'\fR 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 Oracle, the simplest way to do so for schema is probably to
simply create an object in the schema. Put this SQL into
\&\fIverify/appschema.sql\fR:
.PP
.Vb 2
\& CREATE TABLE flipr.verify_\|_ (id int);
\& DROP TABLE flipr.verify_\|_;
.Ve
.PP
In truth, you can use \fIany\fR query that generates an SQL error if the schema
doesn't exist. This works because Sqitch configures SQL*Plus so that SQL
errors cause it to exit with the error code (more on that below). 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 \f(CW\*(C`flipr\*(C'\fR schema does not exist, you could
do something like this:
.PP
.Vb 1
\& SELECT 1/COUNT(*) FROM sys.all_users WHERE username = \*(AqFLIPR\*(Aq;
.Ve
.PP
Either way, run the \f(CW\*(C`verify\*(C'\fR script with the \f(CW\*(C`verify\*(C'\fR
command:
.PP
.Vb 4
\& > sqitch verify db:oracle://scott:tiger@/flipr_test
\& Verifying db:oracle://scott:@/flipr_test
\& * appschema .. ok
\& Verify successful
.Ve
.PP
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:
.PP
.Vb 1
\& CREATE TABLE nonesuch.verify_\|_ (id int);
.Ve
.PP
Then \f(CW\*(C`verify\*(C'\fR again:
.PP
.Vb 6
\& > sqitch verify db:oracle://scott:tiger@/flipr_test
\& Verifying db:oracle://scott:@/flipr_test
\& * appschema .. CREATE TABLE nonesuch.verify_\|_ (id int)
\& *
\& ERROR at line 1:
\& ORA\-01918: user \*(AqNONESUCH\*(Aq does not exist
\&
\&
\&
\& # Verify script "verify/appschema.sql" failed.
\& not ok
\&
\& Verify Summary Report
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& Changes: 1
\& Errors: 1
\& Verify failed
.Ve
.PP
It's even nice enough to tell us what the problem is. Or, for the
divide-by-zero example, change the schema name:
.PP
.Vb 1
\& SELECT 1/COUNT(*) FROM sys.all_users WHERE username = \*(AqNONESUCH\*(Aq;
.Ve
.PP
Then the verify will look something like:
.PP
.Vb 6
\& > sqitch verify db:oracle://scott:tiger@/flipr_test
\& Verifying db:oracle://scott:@/flipr_test
\& * appschema .. SELECT 1/COUNT(*) FROM sys.all_users WHERE username = \*(AqNONESUCH\*(Aq
\& *
\& ERROR at line 1:
\& ORA\-01476: divisor is equal to zero
\&
\&
\&
\& # Verify script "verify/appschema.sql" failed.
\& not ok
\&
\& Verify Summary Report
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& Changes: 1
\& Errors: 1
\& Verify failed
.Ve
.PP
Less useful error output, but enough to alert us that something has gone
wrong.
.PP
Don't forget to change the schema name back before continuing!
.SS "Status, Revert, Log, Repeat"
.IX Subsection "Status, Revert, Log, Repeat"
For purely informational purposes, we can always see how a deployment was
recorded via the \f(CW\*(C`status\*(C'\fR command, which reads the registry
tables from the database:
.PP
.Vb 9
\& > sqitch status db:oracle://scott:tiger@/flipr_test
\& # On database db:oracle://scott:@/flipr_test
\& # Project: flipr
\& # Change: c59e700589fc03568e8f35f592c0d9b7c638cbdd
\& # Name: appschema
\& # Deployed: 2013\-12\-31 15:25:23 \-0800
\& # By: Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.PP
Let's make sure that we can revert the change:
.PP
.Vb 3
\& > sqitch revert db:oracle://scott:tiger@/flipr_test
\& Revert all changes from db:oracle://scott:@/flipr_test? [Yes]
\& \- appschema .. ok
.Ve
.PP
The \f(CW\*(C`revert\*(C'\fR command first prompts to make sure that we
really do want to revert. This is to prevent unnecessary accidents. You can
pass the \f(CW\*(C`\-y\*(C'\fR option to disable the prompt. Also, notice the \f(CW\*(C`\-\*(C'\fR before the
change name in the output, which reinforces that the change is being
\&\fIremoved\fR from the database. And now the schema should be gone:
.PP
.Vb 3
\& > echo "SELECT username FROM all_users WHERE username = \*(AqFLIPR\*(Aq;" \e
\& | sqlplus \-S scott/tiger@flipr_test
\& no rows selected
.Ve
.PP
And the status message should reflect as much:
.PP
.Vb 3
\& > sqitch status db:oracle://scott:tiger@/flipr_test
\& # On database db:oracle://scott:@/flipr_test
\& No changes deployed
.Ve
.PP
Of course, since nothing is deployed, the \f(CW\*(C`verify\*(C'\fR command
has nothing to verify:
.PP
.Vb 3
\& > sqitch verify db:oracle://scott:tiger@/flipr_test
\& Verifying db:oracle://scott:@/flipr_test
\& No changes deployed
.Ve
.PP
However, we still have a record that the change happened, visible via the
\&\f(CW\*(C`log\*(C'\fR command:
.PP
.Vb 6
\& > sqitch log db:oracle://scott:tiger@/flipr_test
\& On database db:oracle://scott:@/flipr_test
\& Revert c59e700589fc03568e8f35f592c0d9b7c638cbdd
\& Name: appschema
\& Committer: Marge N. O’Vera
\& Date: 2013\-12\-31 16:19:38 \-0800
\&
\& App user and schema for all flipr objects.
\&
\& Deploy c59e700589fc03568e8f35f592c0d9b7c638cbdd
\& Name: appschema
\& Committer: Marge N. O’Vera
\& Date: 2013\-12\-31 15:25:23 \-0800
\&
\& App user and schema for all flipr objects.
.Ve
.PP
Note that the actions we took are shown in reverse chronological order, with
the revert first and then the deploy.
.PP
Cool. Now let's commit it.
.PP
.Vb 7
\& > git add .
\& > git commit \-m \*(AqAdd flipr schema.\*(Aq
\& [main e0e0b11] Add flipr schema.
\& 4 files changed, 11 insertions(+)
\& create mode 100644 deploy/appschema.sql
\& create mode 100644 revert/appschema.sql
\& create mode 100644 verify/appschema.sql
.Ve
.PP
And then deploy again. This time, let's use the \f(CW\*(C`\-\-verify\*(C'\fR option, so that
the \f(CW\*(C`verify\*(C'\fR script is applied when the change is deployed:
.PP
.Vb 3
\& > sqitch deploy \-\-verify db:oracle://scott:tiger@/flipr_test
\& Deploying changes to db:oracle://scott:@/flipr_test
\& + appschema .. ok
.Ve
.PP
And now the schema should be back:
.PP
.Vb 5
\& > echo "SELECT username FROM all_users WHERE username = \*(AqFLIPR\*(Aq;" \e
\& | sqlplus \-S scott/tiger@flipr_test
\& USERNAME
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& FLIPR
.Ve
.PP
When we look at the status, the deployment will be there:
.PP
.Vb 9
\& > sqitch status db:oracle://scott:tiger@/flipr_test
\& # On database db:oracle://scott:@/flipr_test
\& # Project: flipr
\& # Change: c59e700589fc03568e8f35f592c0d9b7c638cbdd
\& # Name: appschema
\& # Deployed: 2013\-12\-31 16:22:01 \-0800
\& # By: Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.SH "On Target"
.IX Header "On Target"
I'm getting a little tired of always having to type
\&\f(CW\*(C`db:oracle://scott:tiger@/flipr_test\*(C'\fR, 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:
.PP
.Vb 1
\& > sqitch target add flipr_test db:oracle://scott:tiger@/flipr_test
.Ve
.PP
The \f(CW\*(C`target\*(C'\fR command, inspired by
\&\f(CW\*(C`git\-remote\*(C'\fR , allows management of one
or more named deployment targets. We've just added a target named
\&\f(CW\*(C`flipr_test\*(C'\fR, which means we can use the string \f(CW\*(C`flipr_test\*(C'\fR for the target,
rather than the URI. But since we're doing so much testing, we can also tell
Sqitch to deploy to the \f(CW\*(C`flipr_test\*(C'\fR target by default:
.PP
.Vb 1
\& > sqitch engine add oracle flipr_test
.Ve
.PP
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.:
.PP
.Vb 9
\& > sqitch status
\& # On database flipr_test
\& # Project: flipr
\& # Change: c59e700589fc03568e8f35f592c0d9b7c638cbdd
\& # Name: appschema
\& # Deployed: 2013\-12\-31 16:22:01 \-0800
\& # By: Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.PP
Yay, that allows things to be a little more concise. Let's also make sure that
changes are verified after deploying them:
.PP
.Vb 2
\& > sqitch config \-\-bool deploy.verify true
\& > sqitch config \-\-bool rebase.verify true
.Ve
.PP
We'll see the \f(CW\*(C`rebase\*(C'\fR command a bit later. In the meantime,
let's commit the new configuration and make some more changes!
.PP
.Vb 3
\& > git commit \-am \*(AqSet default target and always verify.\*(Aq
\& [main c4a308a] Set default target and always verify.
\& 1 file changed, 8 insertions(+)
.Ve
.SH "Deploy with Dependency"
.IX Header "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:
.PP
.Vb 5
\& > sqitch add users \-\-requires appschema \-n \*(AqCreates table to track our users.\*(Aq
\& Created deploy/users.sql
\& Created revert/users.sql
\& Created verify/users.sql
\& Added "users [appschema]" to sqitch.plan
.Ve
.PP
Note that we're requiring the \f(CW\*(C`appschema\*(C'\fR change as a dependency of the new
\&\f(CW\*(C`users\*(C'\fR change. Although that change has already been added to the plan and
therefore should always be applied before the \f(CW\*(C`users\*(C'\fR change, it's a good
idea to be explicit about dependencies.
.PP
Now edit the scripts. When you're done, \fIdeploy/users.sql\fR should look like
this:
.PP
.Vb 2
\& \-\- Deploy flipr:users to oracle
\& \-\- requires: appschema
\&
\& CREATE TABLE flipr.users (
\& nickname VARCHAR2(512 CHAR) PRIMARY KEY,
\& password VARCHAR2(512 CHAR) NOT NULL,
\& timestamp TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL
\& );
.Ve
.PP
A few things to notice here. On the second line, the dependence on the
\&\f(CW\*(C`appschema\*(C'\fR change has been listed in a comment. This doesn't do anything,
but the default Oracle \f(CW\*(C`deploy\*(C'\fR template lists it here for your reference
while editing the file. Useful, right?
.PP
The table itself will been created in the \f(CW\*(C`flipr\*(C'\fR schema. This is why we need
to require the \f(CW\*(C`appschema\*(C'\fR change.
.PP
Notice that we've done nothing about error handling. Sqitch needs SQL*Plus
to return failure when a script experiences an error, so one might expect that
each script would need to start with lines like these:
.PP
.Vb 2
\& WHENEVER OSERROR EXIT 9
\& WHENEVER SQLERROR EXIT SQL.SQLCODE
.Ve
.PP
However, Sqitch always sets these error handling parameters before it executes
your scripts, so you don't have to.
.PP
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 \f(CW\*(C`WHERE\*(C'\fR clause. Add this to \fIverify/users.sql\fR:
.PP
.Vb 3
\& SELECT nickname, password, timestamp
\& FROM flipr.users
\& WHERE 0 = 1;
.Ve
.PP
Now for the revert script: all we have to do is drop the table. Add this to
\&\fIrevert/users.sql\fR:
.PP
.Vb 1
\& DROP TABLE flipr.users;
.Ve
.PP
Couldn't be much simpler, right? Let's deploy this bad boy:
.PP
.Vb 3
\& > sqitch deploy
\& Deploying changes to flipr_test
\& + users .. ok
.Ve
.PP
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:
.PP
.Vb 1
\& > echo "DESCRIBE flipr.users;" | sqlplus \-S scott/tiger@flipr_test
\&
\& Name Null? Type
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& NICKNAME NOT NULL VARCHAR2(512 CHAR)
\& PASSWORD NOT NULL VARCHAR2(512 CHAR)
\& TIMESTAMP NOT NULL TIMESTAMP(6) WITH TIME ZONE
.Ve
.PP
We can also verify all currently deployed changes with the
\&\f(CW\*(C`verify\*(C'\fR command:
.PP
.Vb 5
\& > sqitch verify
\& Verifying flipr_test
\& * appschema .. ok
\& * users ...... ok
\& Verify successful
.Ve
.PP
Now have a look at the status:
.PP
.Vb 9
\& > sqitch status
\& # On database flipr_test
\& # Project: flipr
\& # Change: 6840dc13beb0cd716b8bd3979b03a259c1e94405
\& # Name: users
\& # Deployed: 2013\-12\-31 16:32:31 \-0800
\& # By: Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.PP
Success! Let's make sure we can revert the change, as well:
.PP
.Vb 3
\& > sqitch revert \-\-to @HEAD^ \-y
\& Reverting changes to appschema from flipr_test
\& \- users .. ok
.Ve
.PP
Note that we've used the \f(CW\*(C`\-\-to\*(C'\fR option to specify the change to revert to.
And what do we revert to? The symbolic tag \f(CW@HEAD\fR, when passed to
\&\f(CW\*(C`revert\*(C'\fR, 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 (\f(CW\*(C`^\*(C'\fR) tells Sqitch to select the change \fIprior\fR to the
last deployed change. So we revert to \f(CW\*(C`appschema\*(C'\fR, the penultimate change.
The other potentially useful symbolic tag is \f(CW@ROOT\fR, which refers to the
first change deployed to the database (or in the plan, depending on the
command).
.PP
Back to the database. The \f(CW\*(C`users\*(C'\fR table should be gone but the \f(CW\*(C`flipr\*(C'\fR schema
should still be around:
.PP
.Vb 1
\& > echo "DESCRIBE flipr.users;" | sqlplus \-S scott/tiger@flipr_test
\&
\& ERROR:
\& ORA\-04043: object flipr.users does not exist
.Ve
.PP
The \f(CW\*(C`status\*(C'\fR command politely informs us that we have
undeployed changes:
.PP
.Vb 10
\& > sqitch status
\& # On database flipr_test
\& # Project: flipr
\& # Change: c59e700589fc03568e8f35f592c0d9b7c638cbdd
\& # Name: appschema
\& # Deployed: 2013\-12\-31 16:22:01 \-0800
\& # By: Marge N. O’Vera
\& #
\& Undeployed change:
\& * users
.Ve
.PP
As does the \f(CW\*(C`verify\*(C'\fR command:
.PP
.Vb 6
\& > sqitch verify
\& Verifying flipr_test
\& * appschema .. ok
\& Undeployed change:
\& * users
\& Verify successful
.Ve
.PP
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.
.PP
Okay, let's commit and deploy again:
.PP
.Vb 10
\& > git add .
\& > git commit \-am \*(AqAdd users table.\*(Aq
\& [main 2506312] Add users table.
\& 4 files changed, 17 insertions(+)
\& create mode 100644 deploy/users.sql
\& create mode 100644 revert/users.sql
\& create mode 100644 verify/users.sql
\& > sqitch deploy
\& Deploying changes to flipr_test
\& + users .. ok
.Ve
.PP
Looks good. Check the status:
.PP
.Vb 9
\& > sqitch status
\& # On database flipr_test
\& # Project: flipr
\& # Change: 6840dc13beb0cd716b8bd3979b03a259c1e94405
\& # Name: users
\& # Deployed: 2013\-12\-31 16:34:28 \-0800
\& # By: Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.PP
Excellent. Let's do some more!
.SH "Add Two at Once"
.IX Header "Add Two at Once"
Let's add a couple more changes to add functions for managing users.
.PP
.Vb 6
\& > sqitch add insert_user \-\-requires users \-\-requires appschema \e
\& \-n \*(AqCreates a function to insert a user.\*(Aq
\& Created deploy/insert_user.sql
\& Created revert/insert_user.sql
\& Created verify/insert_user.sql
\& Added "insert_user [users appschema]" to sqitch.plan
\&
\& > sqitch add change_pass \-\-requires users \-\-requires appschema \e
\& \-n \*(AqCreates a function to change a user password.\*(Aq
\& Created deploy/change_pass.sql
\& Created revert/change_pass.sql
\& Created verify/change_pass.sql
\& Added "change_pass [users appschema]" to sqitch.plan
.Ve
.PP
Now might be a good time to have a look at the deployment plan:
.PP
.Vb 4
\& > cat sqitch.plan
\& %syntax\-version=1.0.0
\& %project=flipr
\& %uri=https://github.com/sqitchers/sqitch\-oracle\-intro/
\&
\& appschema 2013\-12\-31T22:34:42Z Marge N. O’Vera # App user and schema for all flipr objects.
\& users [appschema] 2014\-01\-01T00:31:20Z Marge N. O’Vera # Creates table to track our users.
\& insert_user [users appschema] 2014\-01\-01T00:35:21Z Marge N. O’Vera # Creates a function to insert a user.
\& change_pass [users appschema] 2014\-01\-01T00:35:28Z Marge N. O’Vera # Creates a function to change a user password.
.Ve
.PP
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.
.PP
Let's write the code for the new changes. Here's what
\&\fIdeploy/insert_user.sql\fR should look like:
.PP
.Vb 3
\& \-\- Deploy flipr:insert_user to oracle
\& \-\- requires: users
\& \-\- requires: appschema
\&
\& CREATE OR REPLACE PROCEDURE flipr.insert_user(
\& nickname VARCHAR2,
\& password VARCHAR2
\& ) AS
\& BEGIN
\& INSERT INTO flipr.users VALUES(
\& nickname,
\& LOWER( RAWTOHEX( UTL_RAW.CAST_TO_RAW(
\& sys.dbms_obfuscation_toolkit.md5(input_string => password)
\& ) ) ),
\& DEFAULT
\& );
\& END;
\& /
\&
\& SHOW ERRORS;
\&
\& \-\- Drop and die on error.
\& DECLARE
\& l_err_count INTEGER;
\& BEGIN
\& SELECT COUNT(*)
\& INTO l_err_count
\& FROM all_errors
\& WHERE owner = \*(AqFLIPR\*(Aq
\& AND name = \*(AqINSERT_USER\*(Aq;
\&
\& IF l_err_count > 0 THEN
\& EXECUTE IMMEDIATE \*(AqDROP PROCEDURE flipr.insert_user\*(Aq;
\& raise_application_error(\-20001, \*(AqErrors in FLIPR.INSERT_USER\*(Aq);
\& END IF;
\& END;
\& /
.Ve
.PP
The \f(CW\*(C`DECLARE\*(C'\fR PL/SQL block is to catch compilation warnings, which are not
normally fatal. It's admittedly
a bit convoluted , but ensures that
errors propagate and a broken function get dropped.
.PP
Here's what \fIverify/insert_user.sql\fR might look like:
.PP
.Vb 2
\& \-\- Verify flipr:insert_user on oracle
\& DESCRIBE flipr.insert_user;
.Ve
.PP
We simply take advantage of the fact that \f(CW\*(C`DESCRIBE\*(C'\fR throws an exception if
the specified function does not exist.
.PP
And \fIrevert/insert_user.sql\fR should look something like this:
.PP
.Vb 2
\& \-\- Revert flipr:insert_user from oracle
\& DROP PROCEDURE flipr.insert_user;
.Ve
.PP
Now for \f(CW\*(C`change_pass\*(C'\fR; \fIdeploy/change_pass.sql\fR might look like this:
.PP
.Vb 3
\& \-\- Deploy flipr:change_pass to oracle
\& \-\- requires: users
\& \-\- requires: appschema
\&
\& CREATE OR REPLACE PROCEDURE flipr.change_pass(
\& nick VARCHAR2,
\& oldpass VARCHAR2,
\& newpass VARCHAR2
\& ) IS
\& flipr_auth_failed EXCEPTION;
\& BEGIN
\& UPDATE flipr.users
\& SET password = LOWER( RAWTOHEX( UTL_RAW.CAST_TO_RAW(
\& sys.dbms_obfuscation_toolkit.md5(input_string => newpass)
\& ) ) )
\& WHERE nickname = nick
\& AND password = LOWER( RAWTOHEX( UTL_RAW.CAST_TO_RAW(
\& sys.dbms_obfuscation_toolkit.md5(input_string => oldpass)
\& ) ) );
\& IF SQL%ROWCOUNT = 0 THEN RAISE flipr_auth_failed; END IF;
\& END;
\& /
\&
\& SHOW ERRORS;
\&
\& \-\- Drop and die on error.
\& DECLARE
\& l_err_count INTEGER;
\& BEGIN
\& SELECT COUNT(*)
\& INTO l_err_count
\& FROM all_errors
\& WHERE owner = \*(AqFLIPR\*(Aq
\& AND name = \*(AqCHANGE_PASS\*(Aq;
\&
\& IF l_err_count > 0 THEN
\& EXECUTE IMMEDIATE \*(AqDROP PROCEDURE flipr.CHANGE_PASS\*(Aq;
\& raise_application_error(\-20001, \*(AqErrors in FLIPR.CHANGE_PASS\*(Aq);
\& END IF;
\& END;
\& /
.Ve
.PP
We again need the \f(CW\*(C`DECLARE\*(C'\fR PL/SQL block to detect compilation warnings and
make the script die. Use \f(CW\*(C`DESCRIBE\*(C'\fR in \fIverify/change_pass.sql\fR again:
.PP
.Vb 2
\& \-\- Verify flipr:change_pass on oracle
\& DESCRIBE flipr.change_pass;
.Ve
.PP
And of course, its \f(CW\*(C`revert\*(C'\fR script, \fIrevert/change_pass.sql\fR, should look
something like:
.PP
.Vb 2
\& \-\- Revert flipr:change_pass from oracle
\& DROP PROCEDURE flipr.change_pass;
.Ve
.PP
Try em out!
.PP
.Vb 6
\& > sqitch deploy
\& Deploying changes to flipr_test
\& + insert_user .. No errors.
\& ok
\& + change_pass .. No errors.
\& ok
.Ve
.PP
Looks good. The "No errors" notices come from the \f(CW\*(C`SHOW ERRORS\*(C'\fR SQL*Plus
command. It's not very useful here, but very useful if there are compilation
errors. If it bothers you, you can drop the \f(CW\*(C`SHOW ERRORS\*(C'\fR line and select the
error for display in the \f(CW\*(C`DECLARE\*(C'\fR block, instead.
.PP
Now, do we have the functions? Of course we do, they were verified. Still,
have a look:
.PP
.Vb 2
\& > echo "DESCRIBE flipr.insert_user;\enDESCRIBE flipr.change_pass;" \e
\& | sqlplus \-S scott/tiger@flipr_test
\&
\& PROCEDURE flipr.insert_user
\& Argument Name Type In/Out Default?
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\- \-\-\-\-\-\-\-\-
\& NICKNAME VARCHAR2 IN
\& PASSWORD VARCHAR2 IN
\&
\& PROCEDURE flipr.change_pass
\& Argument Name Type In/Out Default?
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\- \-\-\-\-\-\-\-\-
\& NICK VARCHAR2 IN
\& OLDPASS VARCHAR2 IN
\& NEWPASS VARCHAR2 IN
.Ve
.PP
And what's the status?
.PP
.Vb 9
\& > sqitch status
\& # On database flipr_test
\& # Project: flipr
\& # Change: e1c9df6a95da835769eb560790588c16174f78df
\& # Name: change_pass
\& # Deployed: 2013\-12\-31 16:37:22 \-0800
\& # By: Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.PP
Looks good. Let's make sure revert works:
.PP
.Vb 8
\& > sqitch revert \-y \-\-to @HEAD^^
\& Reverting changes to users from flipr_test
\& \- change_pass .. ok
\& \- insert_user .. ok
\& > echo "DESCRIBE flipr.insert_user;\enDESCRIBE flipr.change_pass;" \e
\& | sqlplus \-S dwheeler/dwheeler@flipr_test
\& ERROR:
\& ORA\-04043: object flipr.insert_user does not exist
\&
\& ERROR:
\& ORA\-04043: object flipr.change_pass does not exist
.Ve
.PP
Note the use of \f(CW\*(C`@HEAD^^\*(C'\fR 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:
.PP
.Vb 10
\& > git add .
\& > git commit \-m \*(AqAdd \`insert_user()\` and \`change_pass()\`.\*(Aq
\& [main 6b6797e] Add \`insert_user()\` and \`change_pass()\`.
\& 7 files changed, 92 insertions(+)
\& create mode 100644 deploy/change_pass.sql
\& create mode 100644 deploy/insert_user.sql
\& create mode 100644 revert/change_pass.sql
\& create mode 100644 revert/insert_user.sql
\& create mode 100644 verify/change_pass.sql
\& create mode 100644 verify/insert_user.sql
\&
\& > sqitch deploy
\& Deploying changes to flipr_test
\& + insert_user .. No errors.
\& ok
\& + change_pass .. No errors.
\& ok
\&
\& > sqitch status
\& # On database flipr_test
\& # Project: flipr
\& # Change: e1c9df6a95da835769eb560790588c16174f78df
\& # Name: change_pass
\& # Deployed: 2013\-12\-31 16:38:46 \-0800
\& # By: Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
\&
\& > sqitch verify
\& Verifying flipr_test
\& * appschema .... ok
\& * users ........ ok
\& * insert_user .. ok
\& * change_pass .. ok
\& Verify successful
.Ve
.PP
Great, we're fully up-to-date!
.SH "Ship It!"
.IX Header "Ship It!"
Let's do a first release of our app. Let's call it \f(CW\*(C`1.0.0\-dev1\*(C'\fR Since we want
to have it go out with deployments tied to the release, let's tag it:
.PP
.Vb 6
\& > sqitch tag v1.0.0\-dev1 \-n \*(AqTag v1.0.0\-dev1.\*(Aq
\& Tagged "change_pass" with @v1.0.0\-dev1
\& > git commit \-am \*(AqTag the database with v1.0.0\-dev1.\*(Aq
\& [main eae5f71] Tag the database with v1.0.0\-dev1.
\& 1 file changed, 1 insertion(+)
\& > git tag v1.0.0\-dev1 \-am \*(AqTag v1.0.0\-dev1\*(Aq
.Ve
.PP
We can try deploying to make sure the tag gets picked up by deploying to a new
database, like so (assuming you have an Oracle SID named \f(CW\*(C`flipr_dev\*(C'\fR that
points to a different database):
.PP
.Vb 9
\& > sqitch deploy db:oracle://scott:tiger@/flipr_dev
\& Adding registry tables to db:oracle://scott:@/flipr_dev
\& Deploying changes to db:oracle://scott:@/flipr_dev
\& + appschema ................. ok
\& + users ..................... ok
\& + insert_user ............... No errors.
\& ok
\& + change_pass @v1.0.0\-dev1 .. No errors.
\& ok
.Ve
.PP
Great, all four changes were deployed and \f(CW\*(C`change_pass\*(C'\fR was tagged with
\&\f(CW\*(C`@v1.0.0\-dev1\*(C'\fR. Let's have a look at the status:
.PP
.Vb 10
\& > sqitch status db:oracle://scott:tiger@/flipr_dev
\& # On database db:oracle://scott:tiger@/flipr_dev
\& # Project: flipr
\& # Change: e1c9df6a95da835769eb560790588c16174f78df
\& # Name: change_pass
\& # Tag: @v1.0.0\-dev1
\& # Deployed: 2013\-12\-31 16:40:02 \-0800
\& # By: Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.PP
Note the listing of the tag as part of the status message. Now let's bundle
everything up for release:
.PP
.Vb 9
\& > sqitch bundle
\& Bundling into bundle/
\& Writing config
\& Writing plan
\& Writing scripts
\& + appschema
\& + users
\& + insert_user
\& + change_pass @v1.0.0\-dev1
.Ve
.PP
Now we can package the \fIbundle\fR directory and distribute it. When it gets
installed somewhere, users can use Sqitch to deploy to the database. Let's try
deploying it to yet another database (again, assuming you have a SID named
\&\f(CW\*(C`flipr_prod\*(C'\fR:
.PP
.Vb 8
\& > cd bundle
\& > sqitch deploy db:oracle://scott:tiger@/flipr_prod
\& Adding registry tables to db:oracle://scott:@/flipr_prod
\& Deploying changes to flipr_prod
\& + appschema ................. ok
\& + users ..................... ok
\& + insert_user ............... ok
\& + change_pass @v1.0.0\-dev1 .. ok
.Ve
.PP
Looks much the same as before, eh? Package it up and ship it!
.PP
.Vb 3
\& > cd ..
\& > mv bundle flipr\-v1.0.0\-dev1
\& > tar \-czf flipr\-v1.0.0\-dev1.tgz flipr\-v1.0.0\-dev1
.Ve
.SH "Flip Out"
.IX Header "Flip Out"
Now that we've got the basics of user management done, let's get to work on
the core of our product, the "flip." 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:
.PP
.Vb 2
\& > git checkout \-b flips
\& Switched to a new branch \*(Aqflips\*(Aq
.Ve
.PP
Now we can add a new change to create a table for our flips.
.PP
.Vb 5
\& > sqitch add flips \-r appschema \-r users \-n \*(AqAdds table for storing flips.\*(Aq
\& Created deploy/flips.sql
\& Created revert/flips.sql
\& Created verify/flips.sql
\& Added "flips [appschema users]" to sqitch.plan
.Ve
.PP
You know the drill by now. Edit \fIdeploy/flips.sql\fR:
.PP
.Vb 3
\& \-\- Deploy flipr:flips to oracle
\& \-\- requires: appschema
\& \-\- requires: users
\&
\& CREATE TABLE flipr.flips (
\& id INTEGER PRIMARY KEY,
\& nickname VARCHAR2(512 CHAR) NOT NULL REFERENCES flipr.users(nickname),
\& body VARCHAR2(180 CHAR) NOT NULL,
\& timestamp TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL
\& );
\&
\& CREATE SEQUENCE flipr.flip_id_seq START WITH 1 INCREMENT BY 1 NOCACHE;
\&
\& CREATE OR REPLACE TRIGGER flipr.flip_pk BEFORE INSERT ON flipr.flips
\& FOR EACH ROW WHEN (NEW.id IS NULL)
\& DECLARE
\& v_id flipr.flips.id%TYPE;
\& BEGIN
\& SELECT flipr.flip_id_seq.nextval INTO v_id FROM DUAL;
\& :new.id := v_id;
\& END;
\& /
.Ve
.PP
Edit \fIverify/flips.sql\fR:
.PP
.Vb 2
\& \-\- Verify flipr:flips on oracle
\& DESCRIBE flipr.flips;
.Ve
.PP
And edit \fIrevert/flips.sql\fR:
.PP
.Vb 4
\& \-\- Revert flipr:flips from oracle
\& DROP TRIGGER flipr.flip_pk;
\& DROP SEQUENCE flipr.flip_id_seq;
\& DROP TABLE flipr.flips;
.Ve
.PP
And give it a whirl:
.PP
.Vb 3
\& > sqitch deploy
\& Deploying changes to flipr_test
\& + flips .. ok
.Ve
.PP
Look good?
.PP
.Vb 12
\& > sqitch status \-\-show\-tags
\& # On database flipr_test
\& # Project: flipr
\& # Change: 8e1573bb5ce5dfc239d5370c33d6e10820234aad
\& # Name: flips
\& # Deployed: 2013\-12\-31 16:51:54 \-0800
\& # By: Marge N. O’Vera
\& #
\& # Tag:
\& # @v1.0.0\-dev1 \- 2013\-12\-31 16:44:00 \-0800 \- Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.PP
Note the use of \f(CW\*(C`\-\-show\-tags\*(C'\fR to show all the deployed tags. Now make it so:
.PP
.Vb 7
\& > git add .
\& > git commit \-am \*(AqAdd flips table.\*(Aq
\& [flips bbea131] Add flips table.
\& 4 files changed, 32 insertions(+)
\& create mode 100644 deploy/flips.sql
\& create mode 100644 revert/flips.sql
\& create mode 100644 verify/flips.sql
.Ve
.SH "Wash, Rinse, Repeat"
.IX Header "Wash, Rinse, Repeat"
Now comes the time to add functions to manage flips. I'm sure you have things
nailed down now. Go ahead and add \f(CW\*(C`insert_flip\*(C'\fR and \f(CW\*(C`delete_flip\*(C'\fR changes
and commit them. The \f(CW\*(C`insert_flip\*(C'\fR deploy script might look something like:
.PP
.Vb 3
\& \-\- Deploy flipr:insert_flip to oracle
\& \-\- requires: flips
\& \-\- requires: appschema
\&
\& CREATE OR REPLACE PROCEDURE flipr.insert_flip(
\& nickname VARCHAR2,
\& body VARCHAR2
\& ) AS
\& BEGIN
\& INSERT INTO flipr.flips (nickname, body)
\& VALUES (nickname, body);
\& END;
\& /
\&
\& SHOW ERRORS;
\&
\& \-\- Drop and die on error.
\& DECLARE
\& l_err_count INTEGER;
\& BEGIN
\& SELECT COUNT(*)
\& INTO l_err_count
\& FROM all_errors
\& WHERE owner = \*(AqFLIPR\*(Aq
\& AND name = \*(AqINSERT_FLIP\*(Aq;
\&
\& IF l_err_count > 0 THEN
\& EXECUTE IMMEDIATE \*(AqDROP PROCEDURE flipr.insert_flip\*(Aq;
\& raise_application_error(\-20001, \*(AqErrors in FLIPR.INSERT_FLIP\*(Aq);
\& END IF;
\& END;
\& /
.Ve
.PP
And the \f(CW\*(C`delete_flip\*(C'\fR deploy script might look something like:
.PP
.Vb 3
\& \-\- Deploy flipr:delete_flip to oracle
\& \-\- requires: flips
\& \-\- requires: appschema
\&
\& CREATE OR REPLACE PROCEDURE flipr.delete_flip(
\& flip_id INTEGER
\& ) IS
\& flipr_flip_delete_failed EXCEPTION;
\& BEGIN
\& DELETE FROM flipr.flips WHERE id = flip_id;
\& IF SQL%ROWCOUNT = 0 THEN RAISE flipr_flip_delete_failed; END IF;
\& END;
\& /
\&
\& SHOW ERRORS;
\&
\& \-\- Drop and die on error.
\& DECLARE
\& l_err_count INTEGER;
\& BEGIN
\& SELECT COUNT(*)
\& INTO l_err_count
\& FROM all_errors
\& WHERE owner = \*(AqFLIPR\*(Aq
\& AND name = \*(AqDELETE_FLIP\*(Aq;
\&
\& IF l_err_count > 0 THEN
\& EXECUTE IMMEDIATE \*(AqDROP PROCEDURE flipr.delete_flip\*(Aq;
\& raise_application_error(\-20001, \*(AqErrors in FLIPR.DELETE_FLIP\*(Aq);
\& END IF;
\& END;
\& /
.Ve
.PP
The \f(CW\*(C`verify\*(C'\fR scripts are:
.PP
.Vb 2
\& \-\- Verify flipr:insert_flip on oracle
\& DESCRIBE flipr.insert_flip;
.Ve
.PP
And:
.PP
.Vb 2
\& \-\- Verify flipr:delete_flip on oracle
\& DESCRIBE flipr.delete_flip;
.Ve
.PP
The \f(CW\*(C`revert\*(C'\fR scripts are:
.PP
.Vb 2
\& \-\- Revert flipr:insert_flip from oracle
\& DROP PROCEDURE flipr.insert_flip;
.Ve
.PP
And:
.PP
.Vb 2
\& \-\- Revert flipr:delete_flip from oracle
\& DROP PROCEDURE flipr.delete_flip;
.Ve
.PP
Check the example git repository for
the complete details. Test \f(CW\*(C`deploy\*(C'\fR and
\&\f(CW\*(C`revert\*(C'\fR, then commit it to the repository. The status
should end up looking something like this:
.PP
.Vb 12
\& > sqitch status \-\-show\-tags
\& # On database flipr_test
\& # Project: flipr
\& # Change: a47be5a474eaad1a28546666eadeb0eba3ac12dc
\& # Name: delete_flip
\& # Deployed: 2013\-12\-31 16:54:31 \-0800
\& # By: Marge N. O’Vera
\& #
\& # Tag:
\& # @v1.0.0\-dev1 \- 2013\-12\-31 16:44:00 \-0800 \- Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.PP
Good, we've finished this feature. Time to merge back into \f(CW\*(C`main\*(C'\fR.
.SS Emergency
.IX Subsection "Emergency"
Let's do it:
.PP
.Vb 10
\& > git checkout main
\& Switched to branch \*(Aqmain\*(Aq
\& > git pull
\& Updating eae5f71..a16f97c
\& Fast\-forward
\& deploy/delete_list.sql | 35 +++++++++++++++++++++++++++++++++++
\& deploy/insert_list.sql | 33 +++++++++++++++++++++++++++++++++
\& deploy/lists.sql | 10 ++++++++++
\& revert/delete_list.sql | 3 +++
\& revert/insert_list.sql | 3 +++
\& revert/lists.sql | 3 +++
\& sqitch.plan | 4 ++++
\& verify/delete_list.sql | 3 +++
\& verify/insert_list.sql | 3 +++
\& verify/lists.sql | 5 +++++
\& 10 files changed, 102 insertions(+)
\& create mode 100644 deploy/delete_list.sql
\& create mode 100644 deploy/insert_list.sql
\& create mode 100644 deploy/lists.sql
\& create mode 100644 revert/delete_list.sql
\& create mode 100644 revert/insert_list.sql
\& create mode 100644 revert/lists.sql
\& create mode 100644 verify/delete_list.sql
\& create mode 100644 verify/insert_list.sql
\& create mode 100644 verify/lists.sql
.Ve
.PP
Hrm, that's interesting. Looks like someone made some changes to \f(CW\*(C`main\*(C'\fR.
They added list support. Well, let's see what happens when we merge our
changes.
.PP
.Vb 4
\& > git merge \-\-no\-ff flips
\& Auto\-merging sqitch.plan
\& CONFLICT (content): Merge conflict in sqitch.plan
\& Automatic merge failed; fix conflicts and then commit the result.
.Ve
.PP
Oh, a conflict in \fIsqitch.plan\fR. Not too surprising, since both the merged
\&\f(CW\*(C`lists\*(C'\fR branch and our \f(CW\*(C`flips\*(C'\fR branch added changes to the plan. Let's try a
different approach.
.PP
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 \f(CW\*(C`main\*(C'\fR before merging in a branch. What one \fIshould\fR do
is either:
.IP \(bu 4
Rebase the \fIflips\fR branch from main before merging. This "rewinds" the
branch changes, pulls from \f(CW\*(C`main\*(C'\fR, and then replays the changes back on top
of the pulled changes.
.IP \(bu 4
Create a patch and apply \fIthat\fR 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.
.PP
So let's restore things to how they were at main:
.PP
.Vb 2
\& > git reset \-\-hard HEAD
\& HEAD is now at a16f97c Merge branch \*(Aqlists\*(Aq
.Ve
.PP
That throws out our botched merge. Now let's go back to our branch and rebase
it on \f(CW\*(C`main\*(C'\fR:
.PP
.Vb 10
\& > git checkout flips
\& Switched to branch \*(Aqflips\*(Aq
\& > git rebase main
\& First, rewinding head to replay your work on top of it...
\& Applying: Add flips 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 flips 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".
.Ve
.PP
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 \fIsqitch.plan\fR differently. Because we only ever
append lines to the file, we can have it use the "union" merge driver, which,
according to its
docs :
.Sp
.RS 4
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.
.RE
.PP
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:
.PP
.Vb 1
\& > git rebase \-\-abort
.Ve
.PP
Now add the union merge driver to \fI.gitattributes\fR for \fIsqitch.plan\fR
and rebase again:
.PP
.Vb 10
\& > echo sqitch.plan merge=union > .gitattributes
\& > git rebase main
\& First, rewinding head to replay your work on top of it...
\& Applying: Add flips 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
\& Applying: Add functions to insert and delete flips.
\& Using index info to reconstruct a base tree...
\& M sqitch.plan
\& Falling back to patching base and 3\-way merge...
\& Auto\-merging sqitch.plan
.Ve
.PP
Ah, that looks a bit better. Let's have a look at the plan:
.PP
.Vb 4
\& > cat sqitch.plan
\& %syntax\-version=1.0.0
\& %project=flipr
\& %uri=https://github.com/sqitchers/sqitch\-oracle\-intro/
\&
\& appschema 2013\-12\-31T22:34:42Z Marge N. O’Vera # App user and schema for all flipr objects.
\& users [appschema] 2014\-01\-01T00:31:20Z Marge N. O’Vera # Creates table to track our users.
\& insert_user [users appschema] 2014\-01\-01T00:35:21Z Marge N. O’Vera # Creates a function to insert a user.
\& change_pass [users appschema] 2014\-01\-01T00:35:28Z Marge N. O’Vera # Creates a function to change a user password.
\& @v1.0.0\-dev1 2014\-01\-01T00:39:35Z Marge N. O’Vera # Tag v1.0.0\-dev1.
\&
\& lists [appschema users] 2014\-01\-01T00:43:46Z Marge N. O’Vera # Adds table for storing lists.
\& insert_list [lists appschema] 2014\-01\-01T00:45:24Z Marge N. O’Vera # Creates a function to insert a list.
\& delete_list [lists appschema] 2014\-01\-01T00:45:43Z Marge N. O’Vera # Creates a function to delete a list.
\& flips [appschema users] 2014\-01\-01T00:51:15Z Marge N. O’Vera # Adds table for storing flips.
\& insert_flip [flips appschema] 2014\-01\-01T00:53:00Z Marge N. O’Vera # Creates a function to insert a flip.
\& delete_flip [flips appschema] 2014\-01\-01T00:53:16Z Marge N. O’Vera # Creates a function to delete a flip.
.Ve
.PP
Note that it has appended the changes from the merged "lists" branch, and then
merged the changes from our "flips" branch. Test it to make sure it works as
expected:
.PP
.Vb 10
\& > sqitch rebase \-y
\& Reverting all changes from flipr_test
\& \- delete_flip ............... ok
\& \- insert_flip ............... ok
\& \- flips ..................... ok
\& \- change_pass @v1.0.0\-dev1 .. ok
\& \- insert_user ............... ok
\& \- users ..................... ok
\& \- appschema ................. ok
\& Deploying changes to flipr_test
\& + appschema ................. ok
\& + users ..................... ok
\& + insert_user ............... No errors.
\& ok
\& + change_pass @v1.0.0\-dev1 .. No errors.
\& ok
\& + lists ..................... ok
\& + insert_list ............... No errors.
\& ok
\& + delete_list ............... No errors.
\& ok
\& + flips ..................... ok
\& + insert_flip ............... No errors.
\& ok
\& + delete_flip ............... No errors.
\& ok
.Ve
.PP
Note the use of \f(CW\*(C`rebase\*(C'\fR, which combines a
\&\f(CW\*(C`revert\*(C'\fR and a \f(CW\*(C`deploy\*(C'\fR 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 \fI.gitattributes\fR; seems
worthwhile to keep that change:
.PP
.Vb 5
\& > git add .
\& > git commit \-m \*(AqAdd \`.gitattributes\` with union merge for \`sqitch.plan\`.\*(Aq
\& [flips 383691f] Add \`.gitattributes\` with union merge for \`sqitch.plan\`.
\& 1 file changed, 1 insertion(+)
\& create mode 100644 .gitattributes
.Ve
.SS "Merges Mastered"
.IX Subsection "Merges Mastered"
And now, finally, we can merge into \f(CW\*(C`main\*(C'\fR:
.PP
.Vb 10
\& > git checkout main
\& Switched to branch \*(Aqmain\*(Aq
\& > git merge \-\-no\-ff flips \-m "Merge branch \*(Aqflips\*(Aq"
\& Merge made by the \*(Aqrecursive\*(Aq strategy.
\& .gitattributes | 1 +
\& deploy/delete_flip.sql | 32 ++++++++++++++++++++++++++++++++
\& deploy/flips.sql | 22 ++++++++++++++++++++++
\& deploy/insert_flip.sql | 32 ++++++++++++++++++++++++++++++++
\& revert/delete_flip.sql | 3 +++
\& revert/flips.sql | 5 +++++
\& revert/insert_flip.sql | 3 +++
\& sqitch.plan | 3 +++
\& verify/delete_flip.sql | 3 +++
\& verify/flips.sql | 3 +++
\& verify/insert_flip.sql | 3 +++
\& 11 files changed, 110 insertions(+)
\& create mode 100644 .gitattributes
\& create mode 100644 deploy/delete_flip.sql
\& create mode 100644 deploy/flips.sql
\& create mode 100644 deploy/insert_flip.sql
\& create mode 100644 revert/delete_flip.sql
\& create mode 100644 revert/flips.sql
\& create mode 100644 revert/insert_flip.sql
\& create mode 100644 verify/delete_flip.sql
\& create mode 100644 verify/flips.sql
\& create mode 100644 verify/insert_flip.sql
.Ve
.PP
And double-check our work:
.PP
.Vb 4
\& > cat sqitch.plan
\& %syntax\-version=1.0.0
\& %project=flipr
\& %uri=https://github.com/sqitchers/sqitch\-oracle\-intro/
\&
\& appschema 2013\-12\-31T22:34:42Z Marge N. O’Vera # App user and schema for all flipr objects.
\& users [appschema] 2014\-01\-01T00:31:20Z Marge N. O’Vera # Creates table to track our users.
\& insert_user [users appschema] 2014\-01\-01T00:35:21Z Marge N. O’Vera # Creates a function to insert a user.
\& change_pass [users appschema] 2014\-01\-01T00:35:28Z Marge N. O’Vera # Creates a function to change a user password.
\& @v1.0.0\-dev1 2014\-01\-01T00:39:35Z Marge N. O’Vera # Tag v1.0.0\-dev1.
\&
\& lists [appschema users] 2014\-01\-01T00:43:46Z Marge N. O’Vera # Adds table for storing lists.
\& insert_list [lists appschema] 2014\-01\-01T00:45:24Z Marge N. O’Vera # Creates a function to insert a list.
\& delete_list [lists appschema] 2014\-01\-01T00:45:43Z Marge N. O’Vera # Creates a function to delete a list.
\& flips [appschema users] 2014\-01\-01T00:51:15Z Marge N. O’Vera # Adds table for storing flips.
\& insert_flip [flips appschema] 2014\-01\-01T00:53:00Z Marge N. O’Vera # Creates a function to insert a flip.
\& delete_flip [flips appschema] 2014\-01\-01T00:53:16Z Marge N. O’Vera # Creates a function to delete a flip.
.Ve
.PP
Much much better, a nice clean main now. And because it is now identical to
the "flips" branch, we can just carry on. Go ahead and tag it, bundle, and
release:
.PP
.Vb 10
\& > sqitch tag v1.0.0\-dev2 \-n \*(AqTag v1.0.0\-dev2.\*(Aq
\& Tagged "delete_flip" with @v1.0.0\-dev2
\& > git commit \-am \*(AqTag the database with v1.0.0\-dev2.\*(Aq
\& [main 5427456] Tag the database with v1.0.0\-dev2.
\& 1 file changed, 1 insertion(+)
\& > git tag v1.0.0\-dev2 \-am \*(AqTag v1.0.0\-dev2\*(Aq
\& > sqitch bundle \-\-dest\-dir flipr\-1.0.0\-dev2
\& Bundling into flipr\-1.0.0\-dev2
\& Writing config
\& Writing plan
\& Writing scripts
\& + appschema
\& + users
\& + insert_user
\& + change_pass @v1.0.0\-dev1
\& + lists
\& + insert_list
\& + delete_list
\& + flips
\& + insert_flip
\& + delete_flip @v1.0.0\-dev2
.Ve
.PP
Note the use of the \f(CW\*(C`\-\-dest\-dir\*(C'\fR option to \f(CW\*(C`sqitch bundle\*(C'\fR. Just a nicer way
to create the top-level directory name so we don't have to rename it from
\&\fIbundle\fR.
.SH "In Place Changes"
.IX Header "In Place Changes"
Uh-oh, someone just noticed that MD5 hashing is not particularly secure. Why?
Have a look at this:
.PP
.Vb 6
\& > echo "
\& DELETE FROM flipr.users;
\& EXECUTE flipr.insert_user(\*(Aqfoo\*(Aq, \*(Aqs3cr3t\*(Aq);
\& EXECUTE flipr.insert_user(\*(Aqbar\*(Aq, \*(Aqs3cr3t\*(Aq);
\& SELECT nickname, password FROM flipr.users;
\& " | sqlplus \-S scott/tiger@flipr_test
\&
\& PL/SQL procedure successfully completed.
\&
\&
\& PL/SQL procedure successfully completed.
\&
\&
\& NICKNAME
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& PASSWORD
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& foo
\& a4d80eac9ab26a4a2da04125bc2c096a
\&
\& bar
\& a4d80eac9ab26a4a2da04125bc2c096a
.Ve
.PP
If user "foo" ever got access to the database, she could quickly discover that
user "bar" has the same password and thus be able to exploit the account. Not
a great idea. So we need to modify the \f(CWinsert_user()\fR and \f(CWchange_pass()\fR
functions to fix that. How?
.PP
We'll create a function that encrypts passwords using a
cryptographic salt . This
will allow the password hashes to be stored with random hashing. So we'll need
to add the function. The deploy script should be:
.PP
.Vb 2
\& \-\- Deploy flipr:crypt to oracle
\& \-\- requires: appschema
\&
\& CREATE OR REPLACE FUNCTION flipr.crypt(
\& password VARCHAR2,
\& salt VARCHAR2
\& ) RETURN VARCHAR2 IS
\& salted CHAR(10) := SUBSTR(salt, 0, 10);
\& BEGIN
\& RETURN salted || LOWER( RAWTOHEX( UTL_RAW.CAST_TO_RAW(
\& sys.dbms_obfuscation_toolkit.md5(input_string => password || salted)
\& ) ) );
\& END;
\& /
\&
\& SHOW ERRORS;
\&
\& \-\- Drop and die on error.
\& DECLARE
\& l_err_count INTEGER;
\& BEGIN
\& SELECT COUNT(*)
\& INTO l_err_count
\& FROM all_errors
\& WHERE owner = \*(AqFLIPR\*(Aq
\& AND name = \*(AqCRYPT\*(Aq;
\&
\& IF l_err_count > 0 THEN
\& EXECUTE IMMEDIATE \*(AqDROP PROCEDURE flipr.crypt\*(Aq;
\& raise_application_error(\-20001, \*(AqErrors in FLIPR.CRYPT\*(Aq);
\& END IF;
\& END;
\& /
.Ve
.PP
And the revert script should be:
.PP
.Vb 2
\& \-\- Revert flipr:crypt. from oracle
\& DROP FUNCTION flipr.crypt;
.Ve
.PP
And, as usual, the verify script should just use \f(CW\*(C`DESCRIBE\*(C'\fR:
.PP
.Vb 2
\& \-\- Verify flipr:crypt on oracle
\& DESCRIBE flipr.crypt;
.Ve
.PP
With that change in place and committed, we're ready to make use of the
improved encryption. But how to deploy the changes to \f(CWinsert_user()\fR and
\&\f(CWchange_pass()\fR?
.PP
Normally, modifying functions in database changes is a
PITA . You have to make
changes like these:
.IP 1. 4
Copy \fIdeploy/insert_user.sql\fR to \fIdeploy/insert_user_crypt.sql\fR.
.IP 2. 4
Edit \fIdeploy/insert_user_crypt.sql\fR to switch from
\&\f(CW\*(C`sys.dbms_obfuscation_toolkit.md5()\*(C'\fR to \f(CW\*(C`flipr.crypt()\*(C'\fR and to add a
dependency on the \f(CW\*(C`crypt\*(C'\fR change.
.IP 3. 4
Copy \fIdeploy/insert_user.sql\fR to \fIrevert/insert_user_crypt.sql\fR.
Yes, copy the original change script to the new revert change.
.IP 4. 4
Copy \fIverify/insert_user.sql\fR to \fIverify/insert_user_crypt.sql\fR.
.IP 5. 4
Edit \fIverify/insert_user_crypt.sql\fR to test that the function now properly
uses \f(CW\*(C`flipr.crypt()\*(C'\fR.
.IP 6. 4
Test the changes to make sure you can deploy and revert the
\&\f(CW\*(C`insert_user_crypt\*(C'\fR change.
.IP 7. 4
Now do the same for the \f(CW\*(C`change_pass\*(C'\fR scripts.
.PP
But you can have Sqitch do it 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 \f(CW\*(C`@v1.0.0\-dev2\*(C'\fR added in the previous
section. With that, we can let Sqitch do most of the hard work for us, thanks
to the \f(CW\*(C`rework\*(C'\fR command, which is similar to
\&\f(CW\*(C`add\*(C'\fR, including support for the \f(CW\*(C`\-\-requires\*(C'\fR option:
.PP
.Vb 6
\& > sqitch rework insert_user \-\-requires crypt \-n \*(AqChange insert_user to use crypt.\*(Aq
\& Added "insert_user [insert_user@v1.0.0\-dev2 crypt]" to sqitch.plan.
\& Modify these files as appropriate:
\& * deploy/insert_user.sql
\& * revert/insert_user.sql
\& * verify/insert_user.sql
.Ve
.PP
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 \f(CW\*(C`insert_user\*(C'\fR change, which we can see via \f(CW\*(C`git status\*(C'\fR:
.PP
.Vb 10
\& > git status
\& # On branch main
\& # Your branch is ahead of \*(Aqorigin/main\*(Aq by 2 commits.
\& # (use "git push" to publish your local commits)
\& #
\& # Changes not staged for commit:
\& # (use "git add ..." to update what will be committed)
\& # (use "git checkout \-\- ..." to discard changes in working directory)
\& #
\& # modified: revert/insert_user.sql
\& # modified: sqitch.plan
\& #
\& # Untracked files:
\& # (use "git add ..." to include in what will be committed)
\& #
\& # deploy/insert_user@v1.0.0\-dev2.sql
\& # revert/insert_user@v1.0.0\-dev2.sql
\& # verify/insert_user@v1.0.0\-dev2.sql
\& no changes added to commit (use "git add" and/or "git commit \-a")
.Ve
.PP
The "untracked files" part of the output is the first thing to notice. They
are all named \f(CW\*(C`insert_user@v1.0.0\-dev2.sql\*(C'\fR. What that means is: "the
\&\f(CW\*(C`insert_user\*(C'\fR change as it was implemented as of the \f(CW\*(C`@v1.0.0\-dev2\*(C'\fR 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 \f(CW\*(C`insert_user\*(C'\fR
change. As such, it's important not to change them again. But hey, if you're
reworking the change, you shouldn't need to.
.PP
The other thing to notice is that \fIrevert/insert_user.sql\fR has changed.
Sqitch replaced it with the original deploy script. As of now,
\&\fIdeploy/insert_user.sql\fR and \fIrevert/insert_user.sql\fR 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 \fIdeploy/insert_user@v1.0.0\-dev2.sql\fR.
.PP
Fortunately, our function deploy scripts are already idempotent, thanks to the
use of the \f(CW\*(C`OR REPLACE\*(C'\fR expression. No matter how many times a deployment
script is run, the end result will be the same instance of the function, with
no duplicates or errors.
.PP
As a result, there is no need to explicitly add changes. So go ahead. Modify the
script to switch to \f(CWcrypt()\fR. Make this change to
\&\fIdeploy/insert_user.sql\fR:
.PP
.Vb 5
\& @@ \-1,6 +1,7 @@
\& \-\- Deploy flipr:insert_user to oracle
\& \-\- requires: users
\& \-\- requires: appschema
\& +\-\- requires: crypt
\&
\& CREATE OR REPLACE PROCEDURE flipr.insert_user(
\& nickname VARCHAR2,
\& @@ \-9,9 +10,7 @@ CREATE OR REPLACE PROCEDURE flipr.insert_user(
\& BEGIN
\& INSERT INTO flipr.users VALUES(
\& nickname,
\& \- LOWER( RAWTOHEX( UTL_RAW.CAST_TO_RAW(
\& \- sys.dbms_obfuscation_toolkit.md5(input_string => password)
\& \- ) ) ),
\& + flipr.crypt(password, DBMS_RANDOM.STRING(\*(Aqp\*(Aq, 10)),
\& DEFAULT
\& );
\& END;
.Ve
.PP
Go ahead and rework the \f(CW\*(C`change_pass\*(C'\fR change, too:
.PP
.Vb 6
\& > sqitch rework change_pass \-\-requires crypt \-n \*(AqChange change_pass to use crypt.\*(Aq
\& Added "change_pass [change_pass@v1.0.0\-dev2 crypt]" to sqitch.plan.
\& Modify these files as appropriate:
\& * deploy/change_pass.sql
\& * revert/change_pass.sql
\& * verify/change_pass.sql
.Ve
.PP
And make this change to \fIdeploy/change_pass.sql\fR:
.PP
.Vb 5
\& @@ \-1,6 +1,7 @@
\& \-\- Deploy flipr:change_pass to oracle
\& \-\- requires: users
\& \-\- requires: appschema
\& +\-\- requires: crypt
\&
\& CREATE OR REPLACE PROCEDURE flipr.change_pass(
\& nick VARCHAR2,
\& @@ \-10,13 +11,9 @@ CREATE OR REPLACE PROCEDURE flipr.change_pass(
\& flipr_auth_failed EXCEPTION;
\& BEGIN
\& UPDATE flipr.users
\& \- SET password = LOWER( RAWTOHEX( UTL_RAW.CAST_TO_RAW(
\& \- sys.dbms_obfuscation_toolkit.md5(input_string => newpass)
\& \- ) ) )
\& + SET password = flipr.crypt(newpass, DBMS_RANDOM.STRING(\*(Aqp\*(Aq, 10))
\& WHERE nickname = nick
\& \- AND password = LOWER( RAWTOHEX( UTL_RAW.CAST_TO_RAW(
\& \- sys.dbms_obfuscation_toolkit.md5(input_string => oldpass)
\& \- ) ) );
\& + AND password = flipr.crypt(oldpass, password);
\& IF SQL%ROWCOUNT = 0 THEN RAISE flipr_auth_failed; END IF;
\& END;
\& /
.Ve
.PP
And then try a deployment:
.PP
.Vb 6
\& > sqitch deploy
\& Deploying changes to flipr_test
\& + insert_user .. No errors.
\& ok
\& + change_pass .. No errors.
\& ok
.Ve
.PP
So, are the changes deployed?
.PP
.Vb 6
\& > echo "
\& DELETE FROM flipr.users;
\& EXECUTE flipr.insert_user(\*(Aqfoo\*(Aq, \*(Aqs3cr3t\*(Aq);
\& EXECUTE flipr.insert_user(\*(Aqbar\*(Aq, \*(Aqs3cr3t\*(Aq);
\& SELECT nickname, password FROM flipr.users;
\& " | sqlplus \-S scott/tiger@flipr_test
\&
\& PL/SQL procedure successfully completed.
\&
\&
\& PL/SQL procedure successfully completed.
\&
\&
\& NICKNAME
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& PASSWORD
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& foo
\& cP?.eR!V[pf3d91ce9b7dcfe9260c6f4bb94ed0b22
\&
\& bar
\& Z+l"_W_JiSefb62b789c0ff114cddcccc69c422e78
.Ve
.PP
Awesome, the stored passwords are different now. But can we revert, even
though we haven't written any reversion scripts?
.PP
.Vb 6
\& > sqitch revert \-\-to @HEAD^^ \-y
\& Reverting changes to crypt from flipr_test
\& \- change_pass .. No errors.
\& ok
\& \- insert_user .. No errors.
\& ok
.Ve
.PP
Did that work, are the MD5 passwords back?
.PP
.Vb 6
\& > echo "
\& DELETE FROM flipr.users;
\& EXECUTE flipr.insert_user(\*(Aqfoo\*(Aq, \*(Aqs3cr3t\*(Aq);
\& EXECUTE flipr.insert_user(\*(Aqbar\*(Aq, \*(Aqs3cr3t\*(Aq);
\& SELECT nickname, password FROM flipr.users;
\& " | sqlplus \-S scott/tiger@flipr_test
\&
\& PL/SQL procedure successfully completed.
\&
\&
\& PL/SQL procedure successfully completed.
\&
\&
\& NICKNAME
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& PASSWORD
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& foo
\& a4d80eac9ab26a4a2da04125bc2c096a
\&
\& bar
\& a4d80eac9ab26a4a2da04125bc2c096a
.Ve
.PP
Yes, it works! Sqitch properly finds the original instances of these changes
in the new script files that include tags.
.PP
But what about the verify script? How can we verify that the functions have
been modified to use \f(CWcrypt()\fR? I think the simplest thing to do is to
examine the body of the function by querying the
\&\f(CW\*(C`all_source\*(C'\fR
view. So the \f(CW\*(C`insert_user\*(C'\fR verify script looks like this:
.PP
.Vb 1
\& \-\- Verify flipr:insert_user on oracle
\&
\& DESCRIBE flipr.insert_user;
\&
\& SELECT 1/COUNT(*)
\& FROM all_source
\& WHERE type = \*(AqPROCEDURE\*(Aq
\& AND name = \*(AqINSERT_USER\*(Aq
\& AND text LIKE \*(Aq%flipr.crypt(password, DBMS_RANDOM.STRING(\*(Aq\*(Aqp\*(Aq\*(Aq, 10))%\*(Aq;
.Ve
.PP
And the \f(CW\*(C`change_pass\*(C'\fR verify script looks like this:
.PP
.Vb 1
\& \-\- Verify flipr:change_pass on oracle
\&
\& DESCRIBE flipr.change_pass;
\&
\& SELECT 1/COUNT(*)
\& FROM all_source
\& WHERE type = \*(AqPROCEDURE\*(Aq
\& AND name = \*(AqCHANGE_PASS\*(Aq
\& AND text LIKE \*(Aq%password = flipr.crypt(newpass, DBMS_RANDOM.STRING(\*(Aq\*(Aqp\*(Aq\*(Aq, 10))%\*(Aq;
.Ve
.PP
Make sure these pass by re-deploying:
.PP
.Vb 6
\& > sqitch deploy
\& Deploying changes to flipr_test
\& + insert_user .. No errors.
\& ok
\& + change_pass .. No errors.
\& ok
.Ve
.PP
Excellent. Let's go ahead and commit these changes:
.PP
.Vb 12
\& > git add .
\& > git commit \-m \*(AqUse crypt to encrypt passwords.\*(Aq
\& [main be46175] Use crypt to encrypt passwords.
\& 13 files changed, 181 insertions(+), 15 deletions(\-)
\& create mode 100644 deploy/change_pass@v1.0.0\-dev2.sql
\& create mode 100644 deploy/insert_user@v1.0.0\-dev2.sql
\& rewrite revert/change_pass.sql (98%)
\& rename revert/{change_pass.sql => change_pass@v1.0.0\-dev2.sql} (100%)
\& rewrite revert/insert_user.sql (98%)
\& rename revert/{insert_user.sql => insert_user@v1.0.0\-dev2.sql} (100%)
\& create mode 100644 verify/change_pass@v1.0.0\-dev2.sql
\& create mode 100644 verify/insert_user@v1.0.0\-dev2.sql
\&
\& > sqitch status
\& # On database flipr_test
\& # Project: flipr
\& # Change: 8367dc3bff7a563ec27f145421a1ffdf724cb6de
\& # Name: change_pass
\& # Deployed: 2013\-12\-31 17:18:28 \-0800
\& # By: Marge N. O’Vera
\& #
\& Nothing to deploy (up\-to\-date)
.Ve
.SH "More to Come"
.IX Header "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.
.SH Author
.IX Header "Author"
David E. Wheeler
.SH License
.IX Header "License"
Copyright (c) 2012\-2024 iovation Inc., David E. Wheeler
.PP
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:
.PP
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
.PP
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.