run-upgrade-scripts 3.62 KB
Newer Older
1
#!/usr/bin/perl -w
2 3
# Run all upgrade scripts.
#
4 5 6 7 8 9 10 11 12 13 14
# (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.
15 16 17 18 19 20 21 22 23 24 25

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;
26 27 28 29
    unless (defined $dbs[0]) {
        print ' Error: cluster is not running';
        return ();
    }
Martin Pitt's avatar
Martin Pitt committed
30 31

    # filter out the postgres database
32
    @dbs = grep { $_ ne 'postgres' } @dbs;
Martin Pitt's avatar
Martin Pitt committed
33

34 35 36 37 38 39 40 41
    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 {
42 43 44 45 46 47
    if (my $child = fork) { # parent
        waitpid $child, 0;
        return;
    }

    # child code
48 49 50
    my ($script, $scope, $version, $cluster, $db) = @_;
    $cluster =~ s/'/''/g; # escape ' in cluster name
    my $orig_euid = $>;
51
    $< = $> = (stat (PgCommon::cluster_data_directory $version, $cluster))[4];
Martin Pitt's avatar
Martin Pitt committed
52 53

    # temporarily enable connections
54
    my $conallow = `psql --cluster '$version/$cluster' template1 -AXtqc "select datallowconn from pg_database where datname='$db'"`;
Martin Pitt's avatar
Martin Pitt committed
55 56
    chomp $conallow;
    if ($conallow eq 'f') {
57
        system "psql --cluster '$version/$cluster' template1 -AXtqc \"update pg_database set datallowconn = 't' where datname='$db'\"";
Martin Pitt's avatar
Martin Pitt committed
58 59
    }

60
    my $out = `ON_ERROR_STOP=1 psql --cluster '$version/$cluster' -f '$script' '$db' 2>&1`;
Martin Pitt's avatar
Martin Pitt committed
61 62 63

    # reset allowconn
    if ($conallow eq 'f') {
64
        system "psql --cluster '$version/$cluster' template1 -AXtqc \"update pg_database set datallowconn = 'f' where datname='$db'\"";
Martin Pitt's avatar
Martin Pitt committed
65
    }
66
    print "[FAIL]\n$out" if $?;
67 68

    exit 0;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
}

# 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'";
Martin Pitt's avatar
Martin Pitt committed
89
for my $script (sort readdir S) {
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
    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;