remove alerts.
[sgn.git] / db / run_all_patches.pl
blob6e41013af77b9c08e0c32b64dc9d3214970e8cc8
1 #!/usr/bin/env perl
3 =head1 Usage
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`
17 =cut
19 use strict;
20 use warnings;
22 use Getopt::Long;
23 use Data::Dumper;
24 use File::Basename qw(dirname);
25 use Cwd qw(abs_path);
27 my $dbuser;
28 my $dbpass;
29 my $host;
30 my $db;
31 my $editinguser;
32 my $startfrom = 0;
33 my $test;
35 GetOptions(
36 "user=s" => \$dbuser,
37 "pass=s" => \$dbpass,
38 "host=s" => \$host,
39 "db=s" => \$db,
40 "editinguser=s" => \$editinguser,
41 "startfrom:i" => \$startfrom,
42 "test" => \$test
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 = "echo -ne \"$dbpass\n\" | 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);
55 chdir($folders[$i]);
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";
62 next;
67 my $cmd = "echo -ne \"$dbuser\\n$dbpass\" | mx-run $patch -H $host -D $db -u $editinguser".($test?' -t':'');
68 print STDERR $cmd."\n";
69 system("bash -c '$cmd'");
70 print STDERR "\n\n\n";