5 Usage: ./run_all_patches.pl -u <dbuser> -p <dbpassword> -h <dbhost> -d <dbname> -e <editinguser> [-s <startfrom>] [--test]
7 -u, --user= database login username
8 -p, --pass= database login pasword
9 -h, --host= database host
10 -d, --db= database name
11 -e, --editinguser= user to write as patch executor
12 -s, --startfrom=0 start patches from folder # (Default: 0)
13 -t, --test Do not make permanent changes.
15 e.g. `./run_all_patches.pl -u postgres -p postgres -h localhost -d fixture -e janedoe -s 00085 -t`
24 use File
::Basename
qw(dirname);
40 "editinguser=s" => \
$editinguser,
41 "startfrom:i" => \
$startfrom,
45 my $db_patch_path = dirname
(abs_path
($0));
46 chdir($db_patch_path);
48 my @folders = grep /[0-9]{5}/, (split "\n", `ls -d */`);
49 my $cmd = "PGPASSWORD=$dbpass psql -h $host -U $dbuser -t -c \"select patch_name from Metadata.md_dbversion\" -d $db";
50 my @installed = grep {!/^$/} map {s/^\s+|\s+$//gr} `$cmd`;
52 for (my $i = 0; $i < (scalar @folders); $i++) {
53 if (($folders[$i] =~ s/\/$//r
) >= $startfrom) {
54 chdir($db_patch_path);
56 my @patches = grep {!($_ ~~ @installed)} map {s/.pm//r} (split "\n", `ls`);
57 for (my $j = 0; $j < (scalar @patches); $j++) {
58 my $patch = $patches[$j];
60 if ($patch =~ /\~$/) {
61 print STDERR
"Ignoring $patch...\n";
65 my $cmd = "echo -ne \"$dbuser\\n$dbpass\" | mx-run $patch -H $host -D $db -u $editinguser" . ($test ?
' -t' : '');
66 print STDERR
$cmd . "\n";
67 system("bash -c '$cmd'");
69 if (($?
>> 8) == 255) { #execution error
70 die "Failed executing patch: $patch";
73 print STDERR
"\n\n\n";
78 print STDERR
"DB patching complete, database is up to date\n";