/
sqitchtutorial.pod
1700 lines (1299 loc) · 56.5 KB
/
sqitchtutorial.pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=encoding UTF-8
=head1 Name
sqitchtutorial - A tutorial introduction to Sqitch change management on PostgreSQL
=head1 Synopsis
sqitch *
=head1 Description
This tutorial explains how to create a sqitch-enabled PostgreSQL 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 L<Git|https://git-scm.com/> as
the VCS and L<PostgreSQL|https://www.postgresql.org/> as the storage engine,
though L<YugabyteDB|https://www.yugabyte.com/yugabytedb/> and
L<CockroachDB|https://www.cockroachlabs.com/product/> should work just as well.
For the most part you can substitute other VCSes and database engines in
the examples as appropriate.
If you'd like to manage an SQLite database, see L<sqitchtutorial-sqlite>.
If you'd like to manage an Oracle database, see L<sqitchtutorial-oracle>.
If you'd like to manage a MySQL database, see L<sqitchtutorial-mysql>.
If you'd like to manage a Firebird database, see L<sqitchtutorial-firebird>.
If you'd like to manage a Vertica database, see L<sqitchtutorial-vertica>.
If you'd like to manage an Exasol database, see L<sqitchtutorial-exasol>.
If you'd like to manage a Snowflake database, see L<sqitchtutorial-snowflake>.
=head1 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 L<on GitHub|https://github.com/sqitchers/sqitch-intro>.
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-intro/ --engine pg
Created sqitch.conf
Created sqitch.plan
Created deploy/
Created revert/
Created verify/
Let's have a look at F<sqitch.conf>:
> cat sqitch.conf
[core]
engine = pg
# plan_file = sqitch.plan
# top_dir = .
# [engine "pg"]
# target = db:pg:
# registry = sqitch
# client = psql
Good, it picked up on the fact that we're creating changes for the PostgreSQL
engine, thanks to the C<--engine pg> option, and saved it to the file.
Furthermore, it wrote a commented-out C<[engine "pg"]> section with all the
available PostgreSQL engine-specific settings commented out and ready to be
edited as appropriate. This configuration will also fork for
L<YugabyteDB|https://www.yugabyte.com/yugabytedb/> databases, too. For
L<CockroachDB|https://www.cockroachlabs.com/product/>, use
C<--engine cockroach>, instead, and replace any instance of C<pg> with
C<cockroach> in the rest of this document.
By default, Sqitch will read F<sqitch.conf> in the current directory for
settings. But it will also read F<~/.sqitch/sqitch.conf> for user-specific
settings. Since PostgreSQL's C<psql> client is not in the path on my system,
let's go ahead and tell it where to find the client on our computer (don't
bother if you're using the
L<Docker image|https://hub.docker.com/r/sqitch/sqitch/> because it uses the
client inside the container, not on your host machine):
> sqitch config --user engine.pg.client /opt/local/pgsql/bin/psql
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 'marge@example.com'
Have a look at F<~/.sqitch/sqitch.conf> and you'll see this:
> cat ~/.sqitch/sqitch.conf
[engine "pg"]
client = /opt/local/pgsql/bin/psql
[user]
name = Marge N. O’Vera
email = marge@example.com
Which means that Sqitch should be able to find C<psql> 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, F<sqitch.plan>:
> cat sqitch.plan
%syntax-version=1.0.0
%project=flipr
%uri=https://github.com/sqitchers/sqitch-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
C<%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 85e8d7c] Initialize Sqitch configuration.
2 files changed, 19 insertions(+)
create mode 100644 sqitch.conf
create mode 100644 sqitch.plan
=head1 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 L<C<add>|sqitch-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 C<deploy> script's job is to create the schema. So we add
this to F<deploy/appschema.sql>:
CREATE SCHEMA flipr;
The C<revert> script's job is to precisely revert the change to the deploy
script, so we add this to F<revert/appschema.sql>:
DROP SCHEMA flipr;
Now we can try deploying this change. First, we need to create a database
to deploy to:
> createdb flipr_test
Now we tell Sqitch where to send the change via a
L<database URI|https://github.com/libwww-perl/uri-db/>. The examples here
use C<db:pg:flipr_test>, assuming a passwordless connection on a local
socket. If you need to specify a username and hostname, it would be more
like C<db:pg://postgres@localhost/flipr_test>. For YugabyteDB, you might
also need the port, as in C<db:pg://postgres@localhost:5433/flipr_test>.
For CockroachDB, use C<db:cockroach:>, as in
C<db:cockroach://root@localhost:26257/flipr_test>.
Sticking with simply C<db:pg:flipr_test> for these examples, we use the
C<deploy> command to deploy the change:
> sqitch deploy db:pg:flipr_test
Adding registry tables to db:pg:flipr_test
Deploying to db:pg:flipr_test
+ appschema .. ok
First Sqitch created registry tables used to track database changes. The
structure and name of the registry varies between databases (PostgreSQL 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 C<+>
reinforces the idea that the change is being C<added> to the database.
With this change deployed, if you connect to the database, you'll be able to
see the schema:
> psql -d flipr_test -c '\dn flipr'
List of schemas
Name | Owner
-------+-------
flipr | marge
=head2 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 C<verify> script comes in. Its job is to test that the deploy
did what 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 PostgreSQL, the simplest way to do so for non-queryable objects
such as schemas is to take advantage the
L<access privilege inquiry functions|https://www.postgresql.org/docs/current/static/functions-info.html#FUNCTIONS-INFO-ACCESS-TABLE>.
These functions conveniently throw exceptions if the object being inquired
does not exist. For our new schema, C<has_schema_privilege()> will do very
nicely. Put this query into F<verify/appschema.sql>:
SELECT pg_catalog.has_schema_privilege('flipr', 'usage');
B<Important!> This query isn't verifying that the user has C<usage> privilege
on schema C<flipr>. The verification will pass even if the current user
has no usage rights.
B<Important!> Both C<SELECT false;> and C<SELECT true;> queries will successfully
pass C<verify> step. Only queries that raise an exception will fail.
Such functionality may not be available to other databases, but you can use
I<any> query that will throw an exception if the schema doesn't exist. One
handy way to do that is to divide by zero if an object doesn't exist. So for
other databases, assuming division by zero is fatal, you could do something
like this:
SELECT 1/COUNT(*) FROM information_schema.schemata WHERE schema_name = 'flipr';
In Postgres 9.5+ you can use C<PL/pgSQL> anonymous functions with
C<ASSERT> / C<RAISE> statements.
DO $$
BEGIN
ASSERT (SELECT has_schema_privilege('flipr', 'usage'));
END $$;
You can use variables to perform more complex checks:
DO $$
DECLARE
result varchar;
BEGIN
result := (SELECT name FROM flipr.pipelines WHERE id = 1);
ASSERT result = 'Example';
END $$;
This example ensures the record with C<id=1> in C<pipelines> table
has C<name> field equals C<'Example'>.
Either way, run the C<verify> script with the L<C<verify>|sqitch-verify>
command:
> sqitch verify db:pg:flipr_test
Verifying db:pg:flipr_test
* 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:
SELECT pg_catalog.has_schema_privilege('nonesuch', 'usage');
Then L<C<verify>|sqitch-verify> again:
> sqitch verify db:pg:flipr_test
Verifying db:pg:flipr_test
* appschema .. psql:verify/appschema.sql:5: ERROR: schema "nonesuch" does not exist
# 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 information_schema.schemata WHERE schema_name = 'nonesuch';
Then the verify will look something like:
> sqitch verify db:pg:flipr_test
Verifying db:pg:flipr_test
* appschema .. psql:verify/appschema.sql:5: ERROR: division by zero
# 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!
=head2 Status, Revert, Log, Repeat
For purely informational purposes, we can always see how a deployment was
recorded via the L<C<status>|sqitch-status> command, which reads the registry
tables from the database:
> sqitch status db:pg:flipr_test
# On database db:pg:flipr_test
# Project: flipr
# Change: c7981df861183412b01be706889e508a63d445ca
# Name: appschema
# Deployed: 2013-12-30 15:27:15 -0800
# By: Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)
Let's make sure that we can revert the change:
> sqitch revert db:pg:flipr_test
Revert all changes from db:pg:flipr_test? [Yes]
- appschema .. ok
The L<C<revert>|sqitch-revert> command first prompts to make sure that we
really do want to revert. This is to prevent unnecessary accidents. You can
pass the C<-y> option to disable the prompt. Also, notice the C<-> before the
change name in the output, which reinforces that the change is being
I<removed> from the database. And now the schema should be gone:
> psql -d flipr_test -c '\dn flipr'
List of schemas
Name | Owner
------+-------
And the status message should reflect as much:
> sqitch status db:pg:flipr_test
# On database db:pg:flipr_test
No changes deployed
Of course, since nothing is deployed, the L<C<verify>|sqitch-verify> command
has nothing to verify:
> sqitch verify db:pg:flipr_test
Verifying db:pg:flipr_test
No changes deployed
However, we still have a record that the change happened, visible via the
L<C<log>|sqitch-log> command:
> sqitch log db:pg:flipr_test
On database db:pg:flipr_test
Revert c7981df861183412b01be706889e508a63d445ca
Name: appschema
Committer: Marge N. O’Vera <marge@example.com>
Date: 2013-12-30 15:38:17 -0800
Add schema for all flipr objects.
Deploy c7981df861183412b01be706889e508a63d445ca
Name: appschema
Committer: Marge N. O’Vera <marge@example.com>
Date: 2013-12-30 15:27:15 -0800
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 d812132] Add flipr schema.
4 files changed, 22 insertions(+)
create mode 100644 deploy/appschema.sql
create mode 100644 revert/appschema.sql
create mode 100644 verify/appschema.sql
And then deploy again. This time, let's use the C<--verify> option, so that
the C<verify> script is applied when the change is deployed:
> sqitch deploy --verify db:pg:flipr_test
Deploying changes to db:pg:flipr_test
+ appschema .. ok
And now the schema should be back:
> psql -d flipr_test -c '\dn flipr'
List of schemas
Name | Owner
-------+-------
flipr | marge
When we look at the status, the deployment will be there:
> sqitch status db:pg:flipr_test
# On database db:pg:flipr_test
# Project: flipr
# Change: c7981df861183412b01be706889e508a63d445ca
# Name: appschema
# Deployed: 2013-12-30 15:40:53 -0800
# By: Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)
=head1 On Target
I'm getting a little tired of always having to type C<db:pg:flipr_test>,
aren't you? This L<database connection URI|https://github.com/libwww-perl/uri-db/>
tells Sqitch how to connect to the deployment target, but we don't have
to keep using the URI. We can name the target:
> sqitch target add flipr_test db:pg:flipr_test
The L<C<target>|sqitch-target> command, inspired by
L<C<git-remote>|https://git-scm.com/docs/git-remote>, allows management of one
or more named deployment targets. We've just added a target named
C<flipr_test>, which means we can use the string C<flipr_test> for the target,
rather than the URI. But since we're doing so much testing, we can also use
the L<C<engine>|sqitch-engine> command to tell Sqitch to deploy to the
C<flipr_test> target by default:
> sqitch engine add pg 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: c7981df861183412b01be706889e508a63d445ca
# Name: appschema
# Deployed: 2013-12-30 15:40:53 -0800
# By: Marge N. O’Vera <marge@example.com>
#
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 L<C<rebase>|sqitch-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 a6267d3] Set default deployment target and always verify.
1 file changed, 8 insertions(+)
=head1 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 C<appschema> change as a dependency of the new
C<users> change. Although that change has already been added to the plan and
therefore should always be applied before the C<users> change, it's a good
idea to be explicit about dependencies.
Now edit the scripts. When you're done, F<deploy/users.sql> should look like
this:
-- Deploy flipr:users to pg
-- requires: appschema
BEGIN;
SET client_min_messages = 'warning';
CREATE TABLE flipr.users (
nickname TEXT PRIMARY KEY,
password TEXT NOT NULL,
timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
COMMIT;
A few things to notice here. On the second line, the dependence on the
C<appschema> change has been listed. This doesn't do anything, but the default
C<deploy> PostgreSQL template lists it here for your reference while editing
the file. Useful, right?
Notice that all of the SQL code is wrapped in a transaction. This is handy for
PostgreSQL deployments, because PostgreSQL DDLs are transactional. The upshot
is that if any part of this deploy script fails, the whole change fails. Such
may work less-well for database engines that don't support transactional DDLs.
The table itself will be created in the C<flipr> schema. This is why we need
to require the C<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 C<WHERE> clause. Add this to F<verify/users.sql>:
SELECT nickname, password, timestamp
FROM flipr.users
WHERE FALSE;
Now for the revert script: all we have to do is drop the table. Add this to
F<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:
> psql -d flipr_test -c '\d flipr.users'
Table "flipr.users"
Column | Type | Modifiers
-----------+--------------------------+------------------------
nickname | text | not null
password | text | not null
timestamp | timestamp with time zone | not null default now()
Indexes:
"users_pkey" PRIMARY KEY, btree (nickname)
We can also verify all currently deployed changes with the
L<C<verify>|sqitch-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: 77398e1dbc5fbce58b05eb67d201f15774718727
# Name: users
# Deployed: 2013-12-30 15:51:09 -0800
# By: Marge N. O’Vera <marge@example.com>
#
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 C<--to> option to specify the change to revert to.
And what do we revert to? The symbolic tag C<@HEAD>, when passed to
L<C<revert>|sqitch-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 (C<^>) tells Sqitch to select the change I<prior> to the
last deployed change. So we revert to C<appschema>, the penultimate change.
The other potentially useful symbolic tag is C<@ROOT>, which refers to the
first change deployed to the database (or in the plan, depending on the
command).
Back to the database. The C<users> table should be gone but the C<flipr> schema
should still be around:
> psql -d flipr_test -c '\d flipr.users'
Did not find any relation named "flipr.users".
The L<C<status>|sqitch-status> command politely informs us that we have
undeployed changes:
> sqitch status
# On database flipr_test
# Project: flipr
# Change: c7981df861183412b01be706889e508a63d445ca
# Name: appschema
# Deployed: 2013-12-30 15:40:53 -0800
# By: Marge N. O’Vera <marge@example.com>
#
Undeployed change:
* users
As does the L<C<verify>|sqitch-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 d58ea2f] Add users table.
4 files changed, 31 insertions(+)
create mode 100644 deploy/users.sql
create mode 100644 revert/users.sql
create mode 100644 verify/users.sql
> sqitch deploy
Deploying changes to flipr_test
+ users .. ok
Looks good. Check the status:
> sqitch status
# On database flipr_test
# Project: flipr
# Change: 77398e1dbc5fbce58b05eb67d201f15774718727
# Name: users
# Deployed: 2013-12-30 15:57:14 -0800
# By: Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)
Excellent. Let's do some more!
=head1 Add Two at Once
Let's add a couple more changes to add functions for managing users.
> sqitch add insert_user --requires users --requires appschema \
-n 'Creates a function to insert a user.'
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 \
-n 'Creates a function to change a user password.'
Created deploy/change_pass.sql
Created revert/change_pass.sql
Created verify/change_pass.sql
Added "change_pass [users appschema]" 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-intro/
appschema 2013-12-30T23:19:45Z Marge N. O’Vera <marge@example.com> # Add schema for all flipr objects.
users [appschema] 2013-12-30T23:49:00Z Marge N. O’Vera <marge@example.com> # Creates table to track our users.
insert_user [users appschema] 2013-12-30T23:57:36Z Marge N. O’Vera <marge@example.com> # Creates a function to insert a user.
change_pass [users appschema] 2013-12-30T23:57:45Z Marge N. O’Vera <marge@example.com> # Creates a function to change a user password.
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
F<deploy/insert_user.sql> should look like:
-- Deploy flipr:insert_user to pg
-- requires: users
-- requires: appschema
BEGIN;
CREATE OR REPLACE FUNCTION flipr.insert_user(
nickname TEXT,
password TEXT
) RETURNS VOID LANGUAGE SQL SECURITY DEFINER AS $$
INSERT INTO flipr.users VALUES($1, md5($2));
$$;
COMMIT;
Here's what F<verify/insert_user.sql> might look like:
BEGIN;
SELECT has_function_privilege('flipr.insert_user(text, text)', 'execute');
ROLLBACK;
We simply take advantage of the fact that C<has_function_privilege()> throws
an exception if the specified function does not exist.
And F<revert/insert_user.sql> should look something like this:
-- Revert flipr:insert_user from pg
BEGIN;
DROP FUNCTION flipr.insert_user(TEXT, TEXT);
COMMIT;
Now for C<change_pass>; F<deploy/change_pass.sql> might look like this:
-- Deploy flipr:change_pass to pg
-- requires: users
-- requires: appschema
BEGIN;
CREATE OR REPLACE FUNCTION flipr.change_pass(
nick TEXT,
oldpass TEXT,
newpass TEXT
) RETURNS BOOLEAN LANGUAGE plpgsql SECURITY DEFINER AS $$
BEGIN
UPDATE flipr.users
SET password = md5($3)
WHERE nickname = $1
AND password = md5($2);
RETURN FOUND;
END;
$$;
COMMIT;
Use C<has_function_privilege()> in F<verify/change_pass.sql> again:
BEGIN;
SELECT has_function_privilege('flipr.change_pass(text, text, text)', 'execute');
ROLLBACK;
And of course, its C<revert> script, F<revert/change_pass.sql>, should look
something like:
-- Revert flipr:change_pass from pg
BEGIN;
DROP FUNCTION flipr.change_pass(TEXT, TEXT, TEXT);
COMMIT;
Try em out!
> sqitch deploy
Deploying changes to flipr_test
+ insert_user .. ok
+ change_pass .. ok
Do we have the functions? Of course we do, they were verified. Still, have a
look:
> psql -d flipr_test -c '\df flipr.*'
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+-------------+------------------+---------------------------------------+--------
flipr | change_pass | boolean | nick text, oldpass text, newpass text | normal
flipr | insert_user | void | nickname text, password text | normal
And what's the status?
> sqitch status
# On database flipr_test
# Project: flipr
# Change: 01a4f6964b89284525cb5877d222df8be70d1647
# Name: change_pass
# Deployed: 2013-12-30 15:59:44 -0800
# By: Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)
Looks good. Let's make sure revert works:
> sqitch revert -y --to @HEAD^^
Reverting changes to users from flipr_test
- change_pass .. ok
- insert_user .. ok
> psql -d flipr_test -c '\df flipr.*'
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+------+------------------+---------------------+------
Note the use of C<@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 `insert_user()` and `change_pass()`.'
[main c9b4d68] Add `insert_user()` and `change_pass()`.
7 files changed, 65 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 .. ok
+ change_pass .. ok
> sqitch status
# On database flipr_test
# Project: flipr
# Change: 01a4f6964b89284525cb5877d222df8be70d1647
# Name: change_pass
# Deployed: 2013-12-30 16:00:50 -0800
# By: Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)
> sqitch verify
Verifying flipr_test
* appschema .... ok
* users ........ ok
* insert_user .. ok
* change_pass .. ok
Verify successful
Great, we're fully up-to-date!
=head1 Ship It!
Let's do a first release of our app. Let's call it C<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 "change_pass" with @v1.0.0-dev1
> git commit -am 'Tag the database with v1.0.0-dev1.'
[main 0acef3e] Tag the database with v1.0.0-dev1.
1 file changed, 1 insertion(+)
> git tag v1.0.0-dev1 -am 'Tag v1.0.0-dev1'
We can try deploying to make sure the tag gets picked up like so:
> createdb flipr_dev
> sqitch deploy db:pg:flipr_dev
Adding registry tables to db:pg:flipr_dev
Deploying changes to db:pg:flipr_dev
+ appschema ................. ok
+ users ..................... ok
+ insert_user ............... ok
+ change_pass @v1.0.0-dev1 .. ok
Great, all four changes were deployed and C<change_pass> was tagged with
C<@v1.0.0-dev1>. Let's have a look at the status:
> sqitch status db:pg:flipr_dev
# On database db:pg:flipr_dev
# Project: flipr
# Change: 01a4f6964b89284525cb5877d222df8be70d1647
# Name: change_pass
# Tag: @v1.0.0-dev1
# Deployed: 2013-12-30 16:02:19 -0800
# By: Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)
Note the listing of the tag as part of the status message. Now let's bundle
everything up for release:
> sqitch bundle
Bundling into bundle/
Writing config
Writing plan
Writing scripts
+ appschema
+ users
+ insert_user
+ change_pass @v1.0.0-dev1
Now we can package the F<bundle> directory and distribute it. When it gets
installed somewhere, users can use Sqitch to deploy to the database. Let's try
deploying it:
> cd bundle
> createdb flipr_prod
> sqitch deploy db:pg:flipr_prod
Adding registry tables to db:pg:flipr_prod
Deploying changes to db:pg:flipr_prod
+ appschema ................. ok
+ users ..................... ok
+ insert_user ............... ok
+ change_pass @v1.0.0-dev1 .. ok
Looks much the same as before, eh? 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
=head1 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:
> git checkout -b flips
Switched to a new branch 'flips'
Now we can add a new change to create a table for our flips.
> 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
You know the drill by now. Edit F<deploy/flips.sql>:
-- Deploy flipr:flips to pg
-- requires: appschema
-- requires: users
BEGIN;
SET client_min_messages = 'warning';
CREATE TABLE flipr.flips (
id BIGSERIAL PRIMARY KEY,
nickname TEXT NOT NULL REFERENCES flipr.users(nickname),
body TEXT NOT NULL DEFAULT '' CHECK ( length(body) <= 180 ),
timestamp TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
);
COMMIT;
Edit F<verify/flips.sql>:
-- Verify flipr:flips on pg
BEGIN;
SELECT id
, nickname
, body
, timestamp
FROM flipr.flips
WHERE FALSE;
ROLLBACK;
And edit F<revert/flips.sql>:
-- Revert flipr:flips from pg
BEGIN;
DROP TABLE flipr.flips;
COMMIT;
And give it a whirl:
> sqitch deploy
Deploying changes to flipr_test
+ flips .. ok
Look good?
> sqitch status --show-tags
# On database flipr_test
# Project: flipr
# Change: 4d164ef5986450f00a565735518b1d126f8ee69d
# Name: flips
# Deployed: 2013-12-30 16:34:38 -0800
# By: Marge N. O’Vera <marge@example.com>
#
# Tag:
# @v1.0.0-dev1 - 2013-12-30 16:34:38 -0800 - Marge N. O’Vera <marge@example.com>
#
Nothing to deploy (up-to-date)
Note the use of C<--show-tags> to show all the deployed tags. Now make it so:
> git add .
[flips e8f4655] Add flips table.
> git commit -am 'Add flips table.'
4 files changed, 37 insertions(+)
create mode 100644 deploy/flips.sql
create mode 100644 revert/flips.sql
create mode 100644 verify/flips.sql
=head1 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 C<insert_flip> and C<delete_flip> changes
and commit them. The C<insert_flip> deploy script might look something like:
-- Deploy flipr:insert_flip to pg
-- requires: flips
-- requires: appschema
-- requires: users
BEGIN;
CREATE OR REPLACE FUNCTION flipr.insert_flip(
nickname TEXT,
body TEXT
) RETURNS BIGINT LANGUAGE sql SECURITY DEFINER AS $$
INSERT INTO flipr.flips (nickname, body)
VALUES ($1, $2)
RETURNING id;