summaryrefslogtreecommitdiffstats
path: root/run-upgrade-scripts
blob: aae3e90d8715c06c620be4349ca4b3eea9496544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/perl -w
# Run all upgrade scripts.
#
# (C) 2005-2009 Martin Pitt <mpitt@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

use strict;
use PgCommon;

error "Usage: $0 <version upgraded from>" if $#ARGV != 0;

# Return the cluster's databases that match the given scope.
# Arguments: <version> <cluster> <scope>
sub dbs_from_scope {
    my ($v, $c, $scope) = @_;
    my @dbs = get_cluster_databases $v, $c;
    unless (defined $dbs[0]) {
        print ' Error: cluster is not running';
        return ();
    }

    # filter out the postgres database
    @dbs = grep { $_ ne 'postgres' } @dbs;

    return @dbs if $scope eq 't0';
    return grep { $_ ne 'template0' } @dbs if $scope eq 't1';
    return grep { $_ ne 'template0' && $_ ne 'template1' } @dbs if $scope eq 'db';
    return grep { $_ eq 'template1' } @dbs if $scope eq 'cluster';
}

# Arguments: <script> <scope> <version> <cluster> <database>
sub call_sql {
    if (my $child = fork) { # parent
        waitpid $child, 0;
        return;
    }

    # child code
    my ($script, $scope, $version, $cluster, $db) = @_;
    $cluster =~ s/'/''/g; # escape ' in cluster name
    my $orig_euid = $>;
    $< = $> = (stat (PgCommon::cluster_data_directory $version, $cluster))[4];

    # temporarily enable connections
    my $conallow = `psql --cluster '$version/$cluster' template1 -AXtqc "select datallowconn from pg_database where datname='$db'"`;
    chomp $conallow;
    if ($conallow eq 'f') {
        system "psql --cluster '$version/$cluster' template1 -AXtqc \"update pg_database set datallowconn = 't' where datname='$db'\"";
    }

    my $out = `ON_ERROR_STOP=1 psql --cluster '$version/$cluster' -f '$script' '$db' 2>&1`;

    # reset allowconn
    if ($conallow eq 'f') {
        system "psql --cluster '$version/$cluster' template1 -AXtqc \"update pg_database set datallowconn = 'f' where datname='$db'\"";
    }
    print "[FAIL]\n$out" if $?;

    exit 0;
}

# Arguments: <script> <scope> <version> <cluster> <database>
sub call_script {
    my ($script, $scope, $version, $cluster, $db) = @_;
    $cluster =~ s/'/\\'/g; # escape ' in cluster name
    my $out = `$script '$version' '$cluster' '$db' 2>&1`;
    print "[FAIL]\n$out" if $?;
}

my $upgraded_version  = $ARGV[0];

# determine path of upgrade scripts
my @f = split(/\//, $0);
pop @f;
@f = ('.') if $#f == -1;
push @f, 'upgrade-scripts';
my $scriptpath = join ('/', @f);

opendir S, $scriptpath or error "Could not open script path '$scriptpath'";
for my $script (sort readdir S) {
    my ($fname, $ext) = split /\./, $script;
    my ($version, $name, $scope) = split /_/, $fname;
    my $is_sql = (defined $ext && $ext eq 'sql');
    next unless defined ($scope) && ($is_sql || -x "$scriptpath/$script");

    next unless ($version eq 'all' || $upgraded_version <= $version);
    print "Executing upgrade script $name...\n";

    for my $v (get_versions) {
	for my $c (get_version_clusters $v) {
	    print "  cluster $v/$c:";
	    for my $db (dbs_from_scope $v, $c, $scope) {
		print " $db";
		if ($is_sql) {
		    call_sql "$scriptpath/$script", $scope, $v, $c, $db;
		} else {
		    call_script "$scriptpath/$script", $scope, $v, $c, $db;
		}
	    }
            print "\n";
	}
    }

}
closedir S;