]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/make-db-patch.pl
More make-db-patch goodness: wrap existing
[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 $patch_name;
25 my $patch_from;
26 my $patch_wrap;
27 my @deprecates;
28 my @supersedes;
29
30 exit_usage() if $#ARGV == -1;
31 GetOptions( 
32     'num=i' => \$db_patch_num,
33     'name=s' => \$patch_name,
34     'from=s' => \$patch_from,
35     'wrap=s' => \$patch_wrap,
36     'deprecates=i' => \@deprecates,
37     'supersedes=i' => \@supersedes,
38 ) or exit_usage();
39
40 exit_usage('--num required') unless defined $db_patch_num;
41 exit_usage('--name required') unless defined $patch_name;
42
43 $patch_from = 'HEAD' unless defined $patch_from;
44
45 # pad to four digits
46 $db_patch_num = sprintf('%-04.4d', $db_patch_num);
47 $_ = sprintf('%-04.4d', $_) foreach @deprecates;
48 $_ = sprintf('%-04.4d', $_) foreach @supersedes;
49
50 # basic sanity checks
51 my @existing = glob("upgrade/$db_patch_num.*");
52 if (@existing) {    
53     print "Error: $db_patch_num is already used by $existing[0]\n";
54     exit(1);
55 }
56 foreach my $dep (@deprecates) {
57     if ($dep gt $db_patch_num) {
58         print "Error: deprecated patch $dep has a higher patch number than $db_patch_num\n";
59         exit(1);
60     }
61 }
62 foreach my $sup (@supersedes) {
63     if ($sup gt $db_patch_num) {
64         print "Error: superseded patch $sup has a higher patch number than $db_patch_num\n";
65         exit(1);
66     }
67 }
68
69 my $patch_file_name = "upgrade/$db_patch_num.$patch_name.sql";
70 open OUT, '>', $patch_file_name or die "$): cannot open output file $patch_file_name: $!\n";
71
72 print OUT <<_HEADER_;
73 -- Evergreen DB patch $db_patch_num.$patch_name.sql
74 --
75 -- FIXME: insert description of change, if needed
76 --
77 BEGIN;
78
79 _HEADER_
80
81 if (@deprecates or @supersedes) {
82     my @ins_cols = ('db_patch');
83     my @ins_vals = ("'$db_patch_num'");
84     if (@deprecates) {
85         print OUT "-- Deprecates patch(es): " . join(', ', @deprecates) . "\n"; 
86         push @ins_cols, 'deprecates';
87         push @ins_vals, "ARRAY[" . join(', ', map { "'$_'" } @deprecates) . "]";
88     }
89     if (@supersedes) {
90         print OUT "-- Supersedes patch(es): " . join(', ', @supersedes) . "\n";
91         push @ins_cols, 'supersedes';
92         push @ins_vals, "ARRAY[" . join(', ', map { "'$_'" } @supersedes) . "]";
93     }
94     print OUT "INSERT INTO config.db_patch_dependencies (" .
95               join(', ', @ins_cols) .
96               ")\nVALUES (" .
97               join(', ', @ins_vals) .
98               ");\n";
99 }
100
101 my $patch_init_contents;
102 $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);
103 $patch_init_contents = `cat $patch_wrap` if (defined $patch_wrap && $patch_wrap ne '');
104
105 print OUT <<_FOOTER_;
106
107 -- check whether patch can be applied
108 SELECT evergreen.update_deps_block_check('$db_patch_num', :eg_version);
109
110 -- FIXME: add/check SQL statements to perform the upgrade
111 $patch_init_contents
112
113 COMMIT;
114 _FOOTER_
115
116 close OUT;
117 print "Created new patch script $patch_file_name -- please go forth and edit.\n";
118
119 sub exit_usage {
120     my $msg = shift;
121     print "$msg\n\n" if defined($msg);
122     print <<_HELP_;
123 usage: $0 --num <patch_num> --name <patch_name> [--deprecates <num1>] [--supersedes <num2>]
124
125 Make template for a DB patch SQL file.
126
127     --num          DB patch number
128     --name         descriptive part of patch filename 
129     --deprecates   patch(es) deprecated by this update
130     --supersedes   patch(es) superseded by this update
131     --from         git refspec to compare against
132     --wrap         existing file to wrap (overrides --from)
133 _HELP_
134     exit 0;
135 }