summaryrefslogtreecommitdiff
path: root/lib/sqitchtutorial-sqlite.pod
blob: e59fa713082cc1de14077805ae9c1d3feb0d95ab (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
=encoding UTF-8

=head1 Name

sqitchtutorial-sqlite - A tutorial introduction to Sqitch change management on SQLite

=head1 Synopsis

  sqitch *

=head1 Description

This tutorial explains how to create a sqitch-enabled SQLite 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 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<SQLite|https://www.sqlite.org/> as the storage engine.

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

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 -m 'Initialize project, add README.'
  [main (root-commit) 253542e] Initialize project, add README.
   1 file changed, 37 insertions(+)
   create mode 100644 README.md

If you're a Git user and want to follow along the history, the repository used
in these examples is L<on GitHub|https://github.com/sqitchers/sqitch-sqlite-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-sqlite-intro/ --engine sqlite
  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 = sqlite
  	# plan_file = sqitch.plan
  	# top_dir = .
  # [engine "sqlite"]
  	# target = db:sqlite:
  	# registry = sqitch
  	# client = sqlite3

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

By default, Sqitch will read F<sqitch.conf> in the current directory for
settings. But it will also read F<~/.sqitch/sqitch.conf> for user-specific
settings. Since SQLite's C<sqlite3> client is not in the path on my system,
let's go ahead an tell it where to find the client on our computer (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.sqlite.client /opt/local/bin/sqlite3

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

  > 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 "sqlite"]
	client = /opt/local/bin/sqlite3
  [user]
	name = Marge N. O’Vera
	email = marge@example.com

Which means that Sqitch should be able to find C<sqlite3> 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-sqlite-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 -m 'Initialize Sqitch configuration.'
  [main 91e2f0d] Initialize Sqitch configuration.
   2 files changed, 19 insertions(+)
   create mode 100644 sqitch.conf
   create mode 100644 sqitch.plan

=head1 Our First Change

Let's create a table. Our app will need users, of course, so we'll create a
table for them. Run this command:

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

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 table. By default,
the F<deploy/users.sql> file looks like this:

  -- Deploy flipr:users to sqlite

  BEGIN;

  -- XXX Add DDLs here.

  COMMIT;

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

  -- Deploy flipr:users to sqlite

  BEGIN;

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

  COMMIT;

The C<revert> script's job is to precisely revert the change to the deploy
script, so we edit this to F<revert/users.sql> to look like this:

  -- Revert flipr:users from sqlite

  BEGIN;

  DROP TABLE users;

  COMMIT;

Now we can try deploying this change. We tell Sqitch where to send the change
via a L<database URI|https://github.com/libwww-perl/uri-db/>. Here we've
specified a database file, F<flipr_test.db>:

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

First Sqitch created the registry database and tables used to track database
changes. The registry is separate from the database to which the C<users>
change was deployed; by default, its name is C<sqitch.$suffix>, where
C<$suffix> is the same as the suffix on the target database, if any. It lives
in the same directory as the target database. This will be useful if you use
the SQLite L<C<ATTACHDATABASE>|https://www.sqlite.org/lang_attach.html>
command to manage multiple database files in a single project. In that case,
you will want to use the same file for all the databases. Keep them all in the
same directory with the same suffix and you get just that with the default
sqitch database. In this case, we should end up with two databases:

=over

=item * F<sqitch.db>

The Sqitch registry database.

=item * F<flipr_test.db>

The database Sqitch manages.

=back

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

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

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

	> sqlite3 flipr_test.db '.tables'
	users

=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 was it was supposed to. It should do so without regard to any data that
might be in the database, and should throw an error if the deploy was not
successful. The easiest way to do that with a table is to simply C<SELECT>
from it. Put this query into F<verify/users.sql>:

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

Now you can run the C<verify> script with the L<C<verify>|sqitch-verify>
command:

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

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

  SELECT nickname, password, timestamp
	FROM users_nonesuch
   WHERE 0;

Then L<C<verify>|sqitch-verify> again:

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

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

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

=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 tables
from the registry database:

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

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

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

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

	> sqlite3 flipr_test.db '.tables'

And the status message should reflect as much:

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

Of course, since nothing is deployed, the L<C<verify>|sqitch-verify> command
has nothing to verify:

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

      Creates table to track our users.

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

      Creates table to track our users.

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

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

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

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

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

And now the C<users> table should be back:

  > sqlite3 flipr_test.db '.tables'
  users

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

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

=head1 On Target

I'm getting a little tired of always having to type
C<db:sqlite:flipr_test.db>, 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:sqlite:flipr_test.db

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 tell
Sqitch to deploy to the C<flipr_test> target by default:

  > sqitch engine add sqlite flipr_test

Now we can omit the target argument altogether, unless we need to deploy to
another database. Which we will, eventually, but at least our examples will be
simpler from here on in, e.g.:

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

Yay, 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 target and always verify.'
  [main 5fb57ec] Set default target and always verify.
   1 file changed, 8 insertions(+)

=head1 Deploy with Dependency

Let's add another change. Our app will need to store status messages from
users. Let's call them -- and the table to store them -- "flips". First, add
the new change:

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

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

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

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

  BEGIN;

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

  COMMIT;

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

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

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

  -- Verify flipr:flips on sqlite

  BEGIN;

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

  ROLLBACK;

Now for the revert script: all we have to do is drop the table. Add this to
F<revert/flips.sql>:

  -- Revert flipr:flips from sqlite

  BEGIN;

  DROP TABLE flips;

  COMMIT;

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

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

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:

  > sqlite3 flipr_test.db '.tables'
  flips  users

We can also verify all currently deployed changes with the
L<C<verify>|sqitch-verify> command:

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

Now have a look at the status:

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

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

  > sqitch revert --to @HEAD^ -y
  Reverting changes to users from flipr_test
    - flips .. 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<users>, 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<flips> table should be gone but the C<users> table
should still be around:

  > sqlite3 flipr_test.db '.tables'
  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:   f30fe47f5f99501fb8d481e910d9112c5ac0a676
  # Name:     users
  # Deployed: 2013-12-31 10:57:55 -0800
  # By:       Marge N. O’Vera <marge@example.com>
  # 
  Undeployed change:
    * flips

As does the L<C<verify>|sqitch-verify> command:

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

Note that the verify is successful, because all currently-deployed changes are
verified. The list of undeployed changes (just "flips" here) reminds us about
the current state.

Okay, let's commit and deploy again:

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

Looks good. Check the status:

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

=head1 View to a Thrill

One more thing to add before we are ready to ship a first beta release. Let's
create a view that lists user names with their flips.

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

Now add this SQL to F<deploy/userflips.sql>:

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

Add this SQL to F<verify/userflips.sql>

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

And add the C<DROP VIEW> statement to F<revert/userflips.sql>:

  DROP VIEW userflips;

Now Try it out!

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

Looks good! Commit it.

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

=head1 Ship It!

Now we're ready for the first development 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 "userflips" with @v1.0.0-dev1
  > git commit -am 'Tag the database with v1.0.0-dev1.'
  [main 7a479fd] Tag the database with v1.0.0-dev1.
   1 file changed, 1 insertion(+)
  > git tag v1.0.0-dev1 -am 'Tag v1.0.0-dev1'

We can try deploying to make sure the tag gets picked up like so:

  > mkdir dev
  > sqitch deploy db:sqlite:dev/flipr.db
  Adding registry tables to db:sqlite:dev/sqitch.db
  Deploying changes to db:sqlite:dev/flipr.db
    + users ................... ok
    + flips ................... ok

Great, both changes were deployed and C<userflips> was tagged with
C<@v1.0.0-dev1>. Let's have a look at the status:

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

Note the listing of the tag as part of the status message. Now let's bundle
everything up for release:

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

Now we can package 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
  > sqitch deploy db:sqlite:flipr_prod.db
  Adding registry tables to db:sqlite:sqitch.db
  Deploying changes to db:sqlite:flipr_prod.db
    + users ................... ok
    + flips ................... ok
    + userflips @v1.0.0-dev1 .. ok

Looks much the same as before, eh? Package it up and ship it!

  > rm *.db
  > cd ..
  > mv bundle flipr-v1.0.0-dev1
  > tar -czf flipr-v1.0.0-dev1.tgz flipr-v1.0.0-dev1

=head1 Making a Hash of Things

Now that we've got the basics of the app done, let's add a feature. Gotta
track the hashtags associated with flips, right? Let's add a table for them.
But since other folks are working on other tasks in the repository, we'll work
on a branch, so we can all stay out of each other's way. So let's branch:

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

Now we can add a new change to create a table for hashtags.

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

You know the drill by now. Add this to F<deploy/hashtags.sql>

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

Again, select from the table in F<verify/hashtags.sql>:

  SELECT flip_id, hashtag FROM hashtags WHERE 0;

And drop it in F<revert/hashtags.sql>

  DROP TABLE hashtags;

And give it a whirl:

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

Look good?

  > sqitch status --show-tags
  # On database flipr_test
  # Project:  flipr
  # Change:   1352464e8b5f3d5eeac76a1986379f07de43bffd
  # Name:     hashtags
  # Deployed: 2013-12-31 11:30:53 -0800
  # By:       Marge N. O’Vera <marge@example.com>
  # 
  # Tag:
  #   @v1.0.0-dev1 - 2013-12-31 11:13:49 -0800 - Marge N. O’Vera <marge@example.com>
  # 
  Nothing to deploy (up-to-date)

Note the use of C<--show-tags> to show all the deployed tags. Make sure we can
revert, too:

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

Great! Now make it so:

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

Good, we've finished this feature. Time to merge back into C<main>.

=head2 Emergency

Let's do it:

  > git checkout main
  Switched to branch 'main'
  > git pull
  Updating 7a479fd..47a4107
  Fast-forward
   deploy/lists.sql | 13 +++++++++++++
   revert/lists.sql |  7 +++++++
   sqitch.plan      |  2 ++
   verify/lists.sql |  9 +++++++++
   4 files changed, 31 insertions(+)
   create mode 100644 deploy/lists.sql
   create mode 100644 revert/lists.sql
   create mode 100644 verify/lists.sql

Hrm, that's interesting. Looks like someone made some changes to C<main>.
They added list support. Well, let's see what happens when we merge our
changes.

  > git merge --no-ff hashtags
  Auto-merging sqitch.plan
  CONFLICT (content): Merge conflict in sqitch.plan
  Automatic merge failed; fix conflicts and then commit the result.

Oh, a conflict in F<sqitch.plan>. Not too surprising, since both the merged
C<lists> branch and our C<hashtags> branch added changes to the plan. Let's
try a different approach.

The truth is, we got lazy. Those changes when we pulled main from the origin
should have raised a red flag. It's considered a bad practice not to look at
what's changed in C<main> before merging in a branch. What one I<should> do
is either:

=over

=item *

Rebase the F<hashtags> branch from main before merging. This "rewinds" the
branch changes, pulls from C<main>, and then replays the changes back on top
of the pulled changes.

=item *

Create a patch and apply I<that> to main. This is the sort of thing you
might have to do if you're sending changes to another user, especially if the
VCS is not Git.

=back

So let's restore things to how they were at main:

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

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

  > git checkout hashtags
  Switched to branch 'hashtags'
  > git rebase main   
  First, rewinding head to replay your work on top of it...
  Applying: Add hashtags table.
  Using index info to reconstruct a base tree...
  M	sqitch.plan
  Falling back to patching base and 3-way merge...
  Auto-merging sqitch.plan
  CONFLICT (content): Merge conflict in sqitch.plan
  Failed to merge in the changes.
  Patch failed at 0001 Add hashtags table.
  The copy of the patch that failed is found in:
     .git/rebase-apply/patch

  When you have resolved this problem, run "git rebase --continue".
  If you prefer to skip this patch, run "git rebase --skip" instead.
  To check out the original branch and stop rebasing, run "git rebase --abort".

Oy, that's kind of a pain. It seems like no matter what we do, we'll need to
resolve conflicts in that file. Except in Git. Fortunately for us, we can tell
Git to resolve conflicts in F<sqitch.plan> differently. Because we only ever
append lines to the file, we can have it use the "union" merge driver, which,
according to L<its
docs|https://git-scm.com/docs/gitattributes#_built-in_merge_drivers>:

=over

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.

=back

This has the effect of appending lines from all the merging files, which is
exactly what we need. So let's give it a try. First, back out the botched
rebase:

  > git rebase --abort

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

  > echo sqitch.plan merge=union > .gitattributes
  > git rebase main
  First, rewinding head to replay your work on top of it...
  Applying: Add hashtags table.
  Using index info to reconstruct a base tree...
  M	sqitch.plan
  Falling back to patching base and 3-way merge...
  Auto-merging sqitch.plan

Ah, that looks a bit better. Let's have a look at the plan:

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

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

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

Note that it has appended the changes from the merged "lists" branch, and then
merged the changes from our "hashtags" branch. Test it to make sure it works
as expected:

  > sqitch rebase -y
  Reverting all changes from flipr_test
    - hashtags ................ ok
    - userflips @v1.0.0-dev1 .. ok
    - flips ................... ok
    - users ................... ok
  Deploying changes to flipr_test
    + users ................... ok
    + flips ................... ok
    + userflips @v1.0.0-dev1 .. ok
    + lists ................... ok
    + hashtags ................ ok

Note the use of L<C<rebase>|sqitch-rebase>, which combines a
L<C<revert>|sqitch-revert> and a L<C<deploy>|sqitch-deploy> into a single
command. Handy, right? It correctly reverted our changes, and then deployed
them all again in the proper order. So let's commit F<.gitattributes>; seems
worthwhile to keep that change:

  > git add .
  > git commit -m 'Add `.gitattributes` with union merge for `sqitch.plan`.'
  [hashtags 4f93ac4] Add `.gitattributes` with union merge for `sqitch.plan`.
   1 file changed, 1 insertion(+)
   create mode 100644 .gitattributes

=head2 Merges Mastered

And now, finally, we can merge into C<main>:

  > git checkout main
  Switched to branch 'main'
  > git merge --no-ff hashtags -m "Merge branch 'hashtags'"
  Merge made by the 'recursive' strategy.
   .gitattributes      |  1 +
   deploy/hashtags.sql | 12 ++++++++++++
   revert/hashtags.sql |  7 +++++++
   sqitch.plan         |  1 +
   verify/hashtags.sql |  7 +++++++
   5 files changed, 28 insertions(+)
   create mode 100644 .gitattributes
   create mode 100644 deploy/hashtags.sql
   create mode 100644 revert/hashtags.sql
   create mode 100644 verify/hashtags.sql

And double-check our work:

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

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

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

Much much better, a nice clean main now. And because it is now identical to
the "hashtags" branch, we can just carry on. Go ahead and tag it, bundle, and
release:

  > sqitch tag v1.0.0-dev2 -n 'Tag v1.0.0-dev2.'
  Tagged "hashtags" with @v1.0.0-dev2
  > git commit -am 'Tag the database with v1.0.0-dev2.'
  [main 7abfd9b] Tag the database with v1.0.0-dev2.
   1 file changed, 1 insertion(+)
  > git tag v1.0.0-dev2 -am 'Tag v1.0.0-dev2'
  > sqitch bundle --dest-dir flipr-1.0.0-dev2
  Bundling into flipr-1.0.0-dev2
  Writing config
  Writing plan
  Writing scripts
    + users
    + flips
    + userflips @v1.0.0-dev1
    + lists
    + hashtags @v1.0.0-dev2

Note the use of the C<--dest-dir> option to C<sqitch bundle>. Just a nicer way
to create the top-level directory name so we don't have to rename it from
F<bundle>.

=head1 In Place Changes

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

Normally, modifying views in database changes is a
L<PITA|https://www.urbandictionary.com/define.php?term=pita>. You have to make
changes like these:

=over

=item 1.

Copy F<deploy/userflips.sql> to F<deploy/userflips_twitter.sql>.

=item 2.

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

=item 3.

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

=item 4.

Add a C<DROP VIEW> statement to F<revert/userflips_twitter.sql>.

=item 5.

Copy F<verify/userflips.sql> to F<verify/userflips_twitter.sql>.

=item 6.

Modify F<verify/userflips_twitter.sql> to include a check for the C<twiter>
column.

=item 7.

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

=back

But you can have Sqitch do most of the work for you. The only requirement is
that a tag appear between the two instances of a change we want to modify. In
general, you're going to make a change like this after a release, which you've
tagged anyway, right? Well we have, with C<@v1.0.0-dev2> added in the previous
section. With that, we can let Sqitch do most of the hard work for us, thanks
to the L<C<rework>|sqitch-rework> command, which is similar to
L<C<add>|sqitch-add>:

  > sqitch rework userflips -n 'Adds userflips.twitter.'
  Added "userflips [userflips@v1.0.0-dev2]" to sqitch.plan.
  Modify these files as appropriate:
	* deploy/userflips.sql
	* revert/userflips.sql
	* verify/userflips.sql

Oh, so we can edit those files in place. Nice! How does Sqitch do it? Well, in
point of fact, it has copied the files to stand in for the previous instance
of the C<userflips> change, which we can see via C<git status>:

  > git status
  # On branch main
  # Your branch is ahead of 'origin/main' by 4 commits.
  #   (use "git push" to publish your local commits)
  #
  # Changes not staged for commit:
  #   (use "git add <file>..." to update what will be committed)
  #   (use "git checkout -- <file>..." to discard changes in working directory)
  #
  #	modified:   revert/userflips.sql
  #	modified:   sqitch.plan
  #
  # Untracked files:
  #   (use "git add <file>..." to include in what will be committed)
  #
  #	deploy/userflips@v1.0.0-dev2.sql
  #	revert/userflips@v1.0.0-dev2.sql
  #	verify/userflips@v1.0.0-dev2.sql
  no changes added to commit (use "git add" and/or "git commit -a")

The "untracked files" part of the output is the first thing to notice. They
are all named C<userflips@v1.0.0-dev2.sql>. What that means is: "the
C<userflips> change as it was implemented as of the C<@v1.0.0-dev2> tag."
These are copies of the original scripts, and thereafter Sqitch will find them
when it needs to run scripts for the first instance of the C<userflips>
change. As such, it's important not to change them again. But hey, if you're
reworking the change, you shouldn't need to.

The other thing to notice is that F<revert/userflips.sql> has changed. Sqitch
replaced it with the original deploy script. As of now,
F<deploy/userflips.sql> and F<revert/userflips.sql> are identical. This is on
the assumption that the deploy script will be changed (we're reworking it,
remember?), and that the revert script should actually change things back to
how they were before. Of course, the original deploy script won't be
L<idempotent|https://en.wikipedia.org/wiki/Idempotence> -- that is, able to be
applied multiple times without changing the result beyond the initial
application. It could be if SQLite supported C<CREATE OR REPLACE VIEW>, but
since it doesn't, we will have to edit the script to drop the view before
creating it. Or, more simply, it needs to be updated to revert changes back to
how they were as-of the deployment of F<deploy/userflips@v1.0.0-dev2.sql>.

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

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

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

  @@ -2,7 +2,7 @@
 
   BEGIN;
 
  -SELECT id, nickname, fullname, body, timestamp
  +SELECT id, nickname, fullname, twitter, body, timestamp
     FROM userflips
    WHERE 0;
 
And finally, modify F<revert/userflips.sql> to drop the view
before creating it:

  @@ -4,6 +4,7 @@
 
   BEGIN;
 
  +DROP VIEW IF EXISTS userflips;
   CREATE VIEW userflips AS
   SELECT f.id, u.nickname, u.fullname, f.body, f.timestamp
     FROM users u

Note that if we had included that statement when we originally created the
C<userflips> change, we wouldn't have to change this file at all.

Now try a deployment:

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

So, are the changes deployed?

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

Awesome, the view now includes the C<twitter> column. But can we revert?

  > sqitch revert --to @HEAD^ -y
  Reverting changes to hashtags @v1.0.0-dev2 from flipr_test
	- userflips .. ok

Did that work, is the C<twitter> column gone?

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

Yes, it works! Sqitch properly finds the original instances of these changes
in the new script files that include tags.

Excellent. Let's go ahead and commit these changes:

  > git add .
  > git commit -m 'Add the twitter column to the userflips view.'
  [main 3eb96d9] Add the twitter column to the userflips view.
   7 files changed, 40 insertions(+), 4 deletions(-)
   create mode 100644 deploy/userflips@v1.0.0-dev2.sql
   create mode 100644 revert/userflips@v1.0.0-dev2.sql
   create mode 100644 verify/userflips@v1.0.0-dev2.sql

=head1 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.

=head1 Author

David E. Wheeler <david@justatheory.com>

=head1 License

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

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=cut