add patch for updating materialized views to include timestamp, operator, improved...
[sgn.git] / db / run_all_patches.pl
blobfe81a264d23a3ba0426709bb4fa6920c1d5fb17e
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];
59 my $cmd = "echo -ne \"$dbuser\\n$dbpass\" | mx-run $patch -H $host -D $db -u $editinguser".($test?' -t':'');
60 print STDERR $cmd."\n";
61 system("bash -c '$cmd'");
62 print STDERR "\n\n\n";