]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/make-db-patch.pl
Stamping upgrade script for revive-qp-fts (LP 1161601).
[working/Evergreen.git] / Open-ILS / src / sql / Pg / make-db-patch.pl
1 #!/usr/bin/perl
2
3 # Copyright (C) 2011 Equinox Software, Inc.
4 # Galen Charlton <gmc@esilibrary.com>
5 #
6 # Make template for a new DB patch SQL file.
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 use strict;
19 use warnings;
20
21 use Getopt::Long;
22
23 my $db_patch_num;
24 my $db_patch_nonum;
25 my $patch_name;
26 my $patch_from;
27 my $patch_wrap;
28 my @deprecates;
29 my @supersedes;
30
31 exit_usage() if $#ARGV == -1;
32 GetOptions( 
33     'num=i' => \$db_patch_num,
34     'nonum' => \$db_patch_nonum,
35     'name=s' => \$patch_name,
36     'from=s' => \$patch_from,
37     'wrap=s' => \$patch_wrap,
38     'deprecates=i' => \@deprecates,
39     'supersedes=i' => \@supersedes,
40 ) or exit_usage();
41
42 exit_usage('--num cannot be used with --nonum') if ($db_patch_nonum && defined $db_patch_num);
43 $db_patch_num = 'XXXX' if ($db_patch_nonum);
44 exit_usage('--num or --nonum required') unless defined $db_patch_num;
45 exit_usage('--name required') unless defined $patch_name;
46
47 $patch_from = 'HEAD' unless defined $patch_from;
48
49 # pad to four digits
50 $db_patch_num = sprintf('%-04.4d', $db_patch_num) unless $db_patch_nonum ;
51 $_ = sprintf('%-04.4d', $_) foreach @deprecates;
52 $_ = sprintf('%-04.4d', $_) foreach @supersedes;
53
54 if($db_patch_num ne 'XXXX') {
55     # basic sanity checks
56     my @existing = glob("upgrade/$db_patch_num.*");
57     if (@existing) {    
58         print "Error: $db_patch_num is already used by $existing[0]\n";
59         exit(1);
60     }
61     foreach my $dep (@deprecates) {
62         if ($dep gt $db_patch_num) {
63             print "Error: deprecated patch $dep has a higher patch number than $db_patch_num\n";
64             exit(1);
65         }
66     }
67     foreach my $sup (@supersedes) {
68         if ($sup gt $db_patch_num) {
69             print "Error: superseded patch $sup has a higher patch number than $db_patch_num\n";
70             exit(1);
71         }
72     }
73 }
74 else {
75     if ( -e "upgrade/XXXX.$patch_name.sql" ) {
76         print "Error: upgrade/XXXX.$patch_name.sql already exists\n";
77         print "Either remove the existing file or pick a new --name\n";
78         exit(1);
79     }
80 }
81
82 my $patch_file_name = "upgrade/$db_patch_num.$patch_name.sql";
83 open OUT, '>', $patch_file_name or die "$): cannot open output file $patch_file_name: $!\n";
84
85 print OUT <<_HEADER_;
86 -- Evergreen DB patch $db_patch_num.$patch_name.sql
87 --
88 -- FIXME: insert description of change, if needed
89 --
90 BEGIN;
91
92 _HEADER_
93
94 if (@deprecates or @supersedes) {
95     my @ins_cols = ('db_patch');
96     my @ins_vals = ("'$db_patch_num'");
97     if (@deprecates) {
98         print OUT "-- Deprecates patch(es): " . join(', ', @deprecates) . "\n"; 
99         push @ins_cols, 'deprecates';
100         push @ins_vals, "ARRAY[" . join(', ', map { "'$_'" } @deprecates) . "]";
101     }
102     if (@supersedes) {
103         print OUT "-- Supersedes patch(es): " . join(', ', @supersedes) . "\n";
104         push @ins_cols, 'supersedes';
105         push @ins_vals, "ARRAY[" . join(', ', map { "'$_'" } @supersedes) . "]";
106     }
107     print OUT "INSERT INTO config.db_patch_dependencies (" .
108               join(', ', @ins_cols) .
109               ")\nVALUES (" .
110               join(', ', @ins_vals) .
111               ");\n";
112 }
113
114 my $patch_init_contents;
115 $patch_init_contents = `git diff $patch_from -- ./[0-9][0-9][0-9].*.sql | sed -e '/^[^+\@-]/d' -e '/^\\(--- a\\|+++ b\\)/d' -e 's/^+//'` if ($patch_from ne '' && ! defined $patch_wrap);
116 $patch_init_contents = `cat $patch_wrap` if (defined $patch_wrap && $patch_wrap ne '');
117
118 print OUT <<_FOOTER_;
119
120 -- check whether patch can be applied
121 SELECT evergreen.upgrade_deps_block_check('$db_patch_num', :eg_version);
122
123 -- FIXME: add/check SQL statements to perform the upgrade
124 $patch_init_contents
125
126 COMMIT;
127 _FOOTER_
128
129 close OUT;
130 print "Created new patch script $patch_file_name -- please go forth and edit.\n";
131
132 sub exit_usage {
133     my $msg = shift;
134     print "$msg\n\n" if defined($msg);
135     print <<_HELP_;
136 usage: $0 --num <patch_num> --name <patch_name> [--deprecates <num1>] [--supersedes <num2>]
137
138 Make template for a DB patch SQL file.
139
140     --num          DB patch number
141     --nonum        Versionless
142     --name         descriptive part of patch filename 
143     --deprecates   patch(es) deprecated by this update
144     --supersedes   patch(es) superseded by this update
145     --from         git refspec to compare against
146     --wrap         existing file to wrap (overrides --from)
147 _HELP_
148     exit 0;
149 }