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