|
| 1 | + |
| 2 | +# Copyright (c) 2023, PostgreSQL Global Development Group |
| 3 | + |
| 4 | +=pod |
| 5 | +
|
| 6 | +=head1 NAME |
| 7 | +
|
| 8 | +PostgreSQL::Test::AdjustUpgrade - helper module for cross-version upgrade tests |
| 9 | +
|
| 10 | +=head1 SYNOPSIS |
| 11 | +
|
| 12 | + use PostgreSQL::Test::AdjustUpgrade; |
| 13 | +
|
| 14 | + # Build commands to adjust contents of old-version database before dumping |
| 15 | + $statements = adjust_database_contents($old_version, %dbnames); |
| 16 | +
|
| 17 | + # Adjust contents of old pg_dumpall output file to match newer version |
| 18 | + $dump = adjust_old_dumpfile($old_version, $dump); |
| 19 | +
|
| 20 | + # Adjust contents of new pg_dumpall output file to match older version |
| 21 | + $dump = adjust_new_dumpfile($old_version, $dump); |
| 22 | +
|
| 23 | +=head1 DESCRIPTION |
| 24 | +
|
| 25 | +C<PostgreSQL::Test::AdjustUpgrade> encapsulates various hacks needed to |
| 26 | +compare the results of cross-version upgrade tests. |
| 27 | +
|
| 28 | +=cut |
| 29 | + |
| 30 | +package PostgreSQL::Test::AdjustUpgrade; |
| 31 | + |
| 32 | +use strict; |
| 33 | +use warnings; |
| 34 | + |
| 35 | +use Exporter 'import'; |
| 36 | +use PostgreSQL::Version; |
| 37 | + |
| 38 | +our @EXPORT = qw( |
| 39 | + adjust_database_contents |
| 40 | + adjust_old_dumpfile |
| 41 | + adjust_new_dumpfile |
| 42 | +); |
| 43 | + |
| 44 | +=pod |
| 45 | +
|
| 46 | +=head1 ROUTINES |
| 47 | +
|
| 48 | +=over |
| 49 | +
|
| 50 | +=item $statements = adjust_database_contents($old_version, %dbnames) |
| 51 | +
|
| 52 | +Generate SQL commands to perform any changes to an old-version installation |
| 53 | +that are needed before we can pg_upgrade it into the current PostgreSQL |
| 54 | +version. |
| 55 | +
|
| 56 | +Typically this involves dropping or adjusting no-longer-supported objects. |
| 57 | +
|
| 58 | +Arguments: |
| 59 | +
|
| 60 | +=over |
| 61 | +
|
| 62 | +=item C<old_version>: Branch we are upgrading from, represented as a |
| 63 | +PostgreSQL::Version object. |
| 64 | +
|
| 65 | +=item C<dbnames>: Hash of database names present in the old installation. |
| 66 | +
|
| 67 | +=back |
| 68 | +
|
| 69 | +Returns a reference to a hash, wherein the keys are database names and the |
| 70 | +values are arrayrefs to lists of statements to be run in those databases. |
| 71 | +
|
| 72 | +=cut |
| 73 | + |
| 74 | +sub adjust_database_contents |
| 75 | +{ |
| 76 | + my ($old_version, %dbnames) = @_; |
| 77 | + my $result = {}; |
| 78 | + |
| 79 | + # remove dbs of modules known to cause pg_upgrade to fail |
| 80 | + # anything not builtin and incompatible should clean up its own db |
| 81 | + foreach my $bad_module ('test_ddl_deparse', 'tsearch2') |
| 82 | + { |
| 83 | + if ($dbnames{"contrib_regression_$bad_module"}) |
| 84 | + { |
| 85 | + _add_st($result, 'postgres', |
| 86 | + "drop database contrib_regression_$bad_module"); |
| 87 | + delete($dbnames{"contrib_regression_$bad_module"}); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + # avoid version number issues with test_ext7 |
| 92 | + if ($dbnames{contrib_regression_test_extensions}) |
| 93 | + { |
| 94 | + _add_st( |
| 95 | + $result, |
| 96 | + 'contrib_regression_test_extensions', |
| 97 | + 'drop extension if exists test_ext7'); |
| 98 | + } |
| 99 | + |
| 100 | + # get rid of dblink's dependencies on regress.so |
| 101 | + my $regrdb = |
| 102 | + $old_version le '9.4' |
| 103 | + ? 'contrib_regression' |
| 104 | + : 'contrib_regression_dblink'; |
| 105 | + |
| 106 | + if ($dbnames{$regrdb}) |
| 107 | + { |
| 108 | + _add_st( |
| 109 | + $result, $regrdb, |
| 110 | + 'drop function if exists public.putenv(text)', |
| 111 | + 'drop function if exists public.wait_pid(integer)'); |
| 112 | + } |
| 113 | + |
| 114 | + return $result; |
| 115 | +} |
| 116 | + |
| 117 | +# Internal subroutine to add statement(s) to the list for the given db. |
| 118 | +sub _add_st |
| 119 | +{ |
| 120 | + my ($result, $db, @st) = @_; |
| 121 | + |
| 122 | + $result->{$db} ||= []; |
| 123 | + push(@{ $result->{$db} }, @st); |
| 124 | +} |
| 125 | + |
| 126 | +=pod |
| 127 | +
|
| 128 | +=item adjust_old_dumpfile($old_version, $dump) |
| 129 | +
|
| 130 | +Edit a dump output file, taken from the adjusted old-version installation |
| 131 | +by current-version C<pg_dumpall -s>, so that it will match the results of |
| 132 | +C<pg_dumpall -s> on the pg_upgrade'd installation. |
| 133 | +
|
| 134 | +Typically this involves coping with cosmetic differences in the output |
| 135 | +of backend subroutines used by pg_dump. |
| 136 | +
|
| 137 | +Arguments: |
| 138 | +
|
| 139 | +=over |
| 140 | +
|
| 141 | +=item C<old_version>: Branch we are upgrading from, represented as a |
| 142 | +PostgreSQL::Version object. |
| 143 | +
|
| 144 | +=item C<dump>: Contents of dump file |
| 145 | +
|
| 146 | +=back |
| 147 | +
|
| 148 | +Returns the modified dump text. |
| 149 | +
|
| 150 | +=cut |
| 151 | + |
| 152 | +sub adjust_old_dumpfile |
| 153 | +{ |
| 154 | + my ($old_version, $dump) = @_; |
| 155 | + |
| 156 | + # use Unix newlines |
| 157 | + $dump =~ s/\r\n/\n/g; |
| 158 | + |
| 159 | + # Version comments will certainly not match. |
| 160 | + $dump =~ s/^-- Dumped from database version.*\n//mg; |
| 161 | + |
| 162 | + # Suppress blank lines, as some places in pg_dump emit more or fewer. |
| 163 | + $dump =~ s/\n\n+/\n/g; |
| 164 | + |
| 165 | + return $dump; |
| 166 | +} |
| 167 | + |
| 168 | +=pod |
| 169 | +
|
| 170 | +=item adjust_new_dumpfile($old_version, $dump) |
| 171 | +
|
| 172 | +Edit a dump output file, taken from the pg_upgrade'd installation |
| 173 | +by current-version C<pg_dumpall -s>, so that it will match the old |
| 174 | +dump output file as adjusted by C<adjust_old_dumpfile>. |
| 175 | +
|
| 176 | +Typically this involves deleting data not present in the old installation. |
| 177 | +
|
| 178 | +Arguments: |
| 179 | +
|
| 180 | +=over |
| 181 | +
|
| 182 | +=item C<old_version>: Branch we are upgrading from, represented as a |
| 183 | +PostgreSQL::Version object. |
| 184 | +
|
| 185 | +=item C<dump>: Contents of dump file |
| 186 | +
|
| 187 | +=back |
| 188 | +
|
| 189 | +Returns the modified dump text. |
| 190 | +
|
| 191 | +=cut |
| 192 | + |
| 193 | +sub adjust_new_dumpfile |
| 194 | +{ |
| 195 | + my ($old_version, $dump) = @_; |
| 196 | + |
| 197 | + # use Unix newlines |
| 198 | + $dump =~ s/\r\n/\n/g; |
| 199 | + |
| 200 | + # Version comments will certainly not match. |
| 201 | + $dump =~ s/^-- Dumped from database version.*\n//mg; |
| 202 | + |
| 203 | + # Suppress blank lines, as some places in pg_dump emit more or fewer. |
| 204 | + $dump =~ s/\n\n+/\n/g; |
| 205 | + |
| 206 | + return $dump; |
| 207 | +} |
| 208 | + |
| 209 | +=pod |
| 210 | +
|
| 211 | +=back |
| 212 | +
|
| 213 | +=cut |
| 214 | + |
| 215 | +1; |
0 commit comments