|
| 1 | +# Copyright (c) 2025, PostgreSQL Global Development Group |
| 2 | + |
| 3 | +# Tests for file transfer modes |
| 4 | + |
| 5 | +use strict; |
| 6 | +use warnings FATAL => 'all'; |
| 7 | + |
| 8 | +use PostgreSQL::Test::Cluster; |
| 9 | +use PostgreSQL::Test::Utils; |
| 10 | +use Test::More; |
| 11 | + |
| 12 | +sub test_mode |
| 13 | +{ |
| 14 | + my ($mode) = @_; |
| 15 | + |
| 16 | + my $old = PostgreSQL::Test::Cluster->new('old', install_path => $ENV{oldinstall}); |
| 17 | + my $new = PostgreSQL::Test::Cluster->new('new'); |
| 18 | + |
| 19 | + if (defined($ENV{oldinstall})) |
| 20 | + { |
| 21 | + # Checksums are now enabled by default, but weren't before 18, so pass |
| 22 | + # '-k' to initdb on older versions so that upgrades work. |
| 23 | + $old->init(extra => ['-k']); |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + $old->init(); |
| 28 | + } |
| 29 | + $new->init(); |
| 30 | + |
| 31 | + # Create a small variety of simple test objects on the old cluster. We'll |
| 32 | + # check that these reach the new version after upgrading. |
| 33 | + $old->start; |
| 34 | + $old->safe_psql('postgres', "CREATE TABLE test1 AS SELECT generate_series(1, 100)"); |
| 35 | + $old->safe_psql('postgres', "CREATE DATABASE testdb1"); |
| 36 | + $old->safe_psql('testdb1', "CREATE TABLE test2 AS SELECT generate_series(200, 300)"); |
| 37 | + $old->safe_psql('testdb1', "VACUUM FULL test2"); |
| 38 | + $old->safe_psql('testdb1', "CREATE SEQUENCE testseq START 5432"); |
| 39 | + |
| 40 | + # For cross-version tests, we can also check that pg_upgrade handles |
| 41 | + # tablespaces. |
| 42 | + if (defined($ENV{oldinstall})) |
| 43 | + { |
| 44 | + my $tblspc = PostgreSQL::Test::Utils::tempdir_short(); |
| 45 | + $old->safe_psql('postgres', "CREATE TABLESPACE test_tblspc LOCATION '$tblspc'"); |
| 46 | + $old->safe_psql('postgres', "CREATE DATABASE testdb2 TABLESPACE test_tblspc"); |
| 47 | + $old->safe_psql('postgres', "CREATE TABLE test3 TABLESPACE test_tblspc AS SELECT generate_series(300, 401)"); |
| 48 | + $old->safe_psql('testdb2', "CREATE TABLE test4 AS SELECT generate_series(400, 502)"); |
| 49 | + } |
| 50 | + $old->stop; |
| 51 | + |
| 52 | + my $result = command_ok_or_fails_like( |
| 53 | + [ |
| 54 | + 'pg_upgrade', '--no-sync', |
| 55 | + '--old-datadir' => $old->data_dir, |
| 56 | + '--new-datadir' => $new->data_dir, |
| 57 | + '--old-bindir' => $old->config_data('--bindir'), |
| 58 | + '--new-bindir' => $new->config_data('--bindir'), |
| 59 | + '--socketdir' => $new->host, |
| 60 | + '--old-port' => $old->port, |
| 61 | + '--new-port' => $new->port, |
| 62 | + $mode |
| 63 | + ], |
| 64 | + qr/.* not supported on this platform|could not .* between old and new data directories: .*/, |
| 65 | + qr/^$/, |
| 66 | + "pg_upgrade with transfer mode $mode"); |
| 67 | + |
| 68 | + # If pg_upgrade was successful, check that all of our test objects reached |
| 69 | + # the new version. |
| 70 | + if ($result) |
| 71 | + { |
| 72 | + $new->start; |
| 73 | + $result = $new->safe_psql('postgres', "SELECT COUNT(*) FROM test1"); |
| 74 | + is($result, '100', "test1 data after pg_upgrade $mode"); |
| 75 | + $result = $new->safe_psql('testdb1', "SELECT COUNT(*) FROM test2"); |
| 76 | + is($result, '101', "test2 data after pg_upgrade $mode"); |
| 77 | + $result = $new->safe_psql('testdb1', "SELECT nextval('testseq')"); |
| 78 | + is($result, '5432', "sequence data after pg_upgrade $mode"); |
| 79 | + |
| 80 | + # For cross-version tests, we should have some objects in a non-default |
| 81 | + # tablespace. |
| 82 | + if (defined($ENV{oldinstall})) |
| 83 | + { |
| 84 | + $result = $new->safe_psql('postgres', "SELECT COUNT(*) FROM test3"); |
| 85 | + is($result, '102', "test3 data after pg_upgrade $mode"); |
| 86 | + $result = $new->safe_psql('testdb2', "SELECT COUNT(*) FROM test4"); |
| 87 | + is($result, '103', "test4 data after pg_upgrade $mode"); |
| 88 | + } |
| 89 | + $new->stop; |
| 90 | + } |
| 91 | + |
| 92 | + $old->clean_node(); |
| 93 | + $new->clean_node(); |
| 94 | +} |
| 95 | + |
| 96 | +test_mode('--clone'); |
| 97 | +test_mode('--copy'); |
| 98 | +test_mode('--copy-file-range'); |
| 99 | +test_mode('--link'); |
| 100 | + |
| 101 | +done_testing(); |
0 commit comments