3 # Copyright (C) 2011 Equinox Software, Inc.
4 # Galen Charlton <gmc@esilibrary.com>
6 # Make template for a new DB patch SQL file.
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.
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.
28 exit_usage() if $#ARGV == -1;
30 'num=i' => \$db_patch_num,
31 'name=s' => \$patch_name,
32 'deprecates=i' => \@deprecates,
33 'supersedes=i' => \@supersedes,
36 exit_usage('--num required') unless defined $db_patch_num;
37 exit_usage('--name required') unless defined $patch_name;
40 $db_patch_num = sprintf('%-04.4d', $db_patch_num);
41 $_ = sprintf('%-04.4d', $_) foreach @deprecates;
42 $_ = sprintf('%-04.4d', $_) foreach @supersedes;
45 my @existing = glob("upgrade/$db_patch_num.*");
47 print "Error: $db_patch_num is already used by $existing[0]\n";
50 foreach my $dep (@deprecates) {
51 if ($dep gt $db_patch_num) {
52 print "Error: deprecated patch $dep has a higher patch number than $db_patch_num\n";
56 foreach my $sup (@supersedes) {
57 if ($sup gt $db_patch_num) {
58 print "Error: superseded patch $sup has a higher patch number than $db_patch_num\n";
63 my $patch_file_name = "upgrade/$db_patch_num.$patch_name.sql";
64 open OUT, '>', $patch_file_name or die "$): cannot open output file $patch_file_name: $!\n";
67 -- Evergreen DB patch $db_patch_num.$patch_name.sql
69 -- FIXME: insert description of change, if needed
75 if (@deprecates or @supersedes) {
76 my @ins_cols = ('db_patch');
77 my @ins_vals = ("'$db_patch_num'");
79 print OUT "-- Deprecates patch(es): " . join(', ', @deprecates) . "\n";
80 push @ins_cols, 'deprecates';
81 push @ins_vals, "ARRAY[" . join(', ', map { "'$_'" } @deprecates) . "]";
84 print OUT "-- Supersedes patch(es): " . join(', ', @supersedes) . "\n";
85 push @ins_cols, 'supersedes';
86 push @ins_vals, "ARRAY[" . join(', ', map { "'$_'" } @supersedes) . "]";
88 print OUT "INSERT INTO config.db_patch_dependencies (" .
89 join(', ', @ins_cols) .
91 join(', ', @ins_vals) .
97 -- check whether patch can be applied
98 SELECT evergreen.update_deps_block_check('$db_patch_num', :eg_version);
100 -- FIXME: add SQL statements to perform the upgrade
106 print "Created new patch script $patch_file_name -- please go forth and edit.\n";
110 print "$msg\n\n" if defined($msg);
112 usage: $0 --num <patch_num> --name <patch_name> [--deprecates <num1>] [--supersedes <num2>]
114 Make template for a DB patch SQL file.
116 --num DB patch number
117 --name descriptive part of patch filename
118 --deprecates patch(es) deprecated by this update
119 --supersedes patch(es) superseded by this update