Debian Codegraph @

src/bin/pg_upgrade/t/006_transfer_modes.pl

raw
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 =
17 PostgreSQL::Test::Cluster->new('old', install_path => $ENV{oldinstall});
18 my $new = PostgreSQL::Test::Cluster->new('new');
19
20 # --swap can't be used to upgrade from versions older than 10, so just skip
21 # the test if the old cluster version is too old.
22 if ($old->pg_version < 10 && $mode eq "--swap")
23 {
24 $old->clean_node();
25 $new->clean_node();
26 return;
27 }
28
29 if (defined($ENV{oldinstall}))
30 {
31 # Checksums are now enabled by default, but weren't before 18, so pass
32 # '-k' to initdb on older versions so that upgrades work.
33 $old->init(extra => ['-k']);
34 }
35 else
36 {
37 $old->init();
38 }
39 $new->init();
40
41 # Create a small variety of simple test objects on the old cluster. We'll
42 # check that these reach the new version after upgrading.
43 $old->start;
44 $old->safe_psql('postgres',
45 "CREATE TABLE test1 AS SELECT generate_series(1, 100)");
46 $old->safe_psql('postgres', "CREATE DATABASE testdb1");
47 $old->safe_psql('testdb1',
48 "CREATE TABLE test2 AS SELECT generate_series(200, 300)");
49 $old->safe_psql('testdb1', "VACUUM FULL test2");
50 $old->safe_psql('testdb1', "CREATE SEQUENCE testseq START 5432");
51
52 # For cross-version tests, we can also check that pg_upgrade handles
53 # tablespaces.
54 if (defined($ENV{oldinstall}))
55 {
56 my $tblspc = PostgreSQL::Test::Utils::tempdir_short();
57 $old->safe_psql('postgres',
58 "CREATE TABLESPACE test_tblspc LOCATION '$tblspc'");
59 $old->safe_psql('postgres',
60 "CREATE DATABASE testdb2 TABLESPACE test_tblspc");
61 $old->safe_psql('postgres',
62 "CREATE TABLE test3 TABLESPACE test_tblspc AS SELECT generate_series(300, 401)"
63 );
64 $old->safe_psql('testdb2',
65 "CREATE TABLE test4 AS SELECT generate_series(400, 502)");
66 }
67 $old->stop;
68
69 my $result = command_ok_or_fails_like(
70 [
71 'pg_upgrade', '--no-sync',
72 '--old-datadir' => $old->data_dir,
73 '--new-datadir' => $new->data_dir,
74 '--old-bindir' => $old->config_data('--bindir'),
75 '--new-bindir' => $new->config_data('--bindir'),
76 '--socketdir' => $new->host,
77 '--old-port' => $old->port,
78 '--new-port' => $new->port,
79 $mode
80 ],
81 qr/.* not supported on this platform|could not .* between old and new data directories: .*/,
82 qr/^$/,
83 "pg_upgrade with transfer mode $mode");
84
85 # If pg_upgrade was successful, check that all of our test objects reached
86 # the new version.
87 if ($result)
88 {
89 $new->start;
90 $result = $new->safe_psql('postgres', "SELECT COUNT(*) FROM test1");
91 is($result, '100', "test1 data after pg_upgrade $mode");
92 $result = $new->safe_psql('testdb1', "SELECT COUNT(*) FROM test2");
93 is($result, '101', "test2 data after pg_upgrade $mode");
94 $result = $new->safe_psql('testdb1', "SELECT nextval('testseq')");
95 is($result, '5432', "sequence data after pg_upgrade $mode");
96
97 # For cross-version tests, we should have some objects in a non-default
98 # tablespace.
99 if (defined($ENV{oldinstall}))
100 {
101 $result =
102 $new->safe_psql('postgres', "SELECT COUNT(*) FROM test3");
103 is($result, '102', "test3 data after pg_upgrade $mode");
104 $result =
105 $new->safe_psql('testdb2', "SELECT COUNT(*) FROM test4");
106 is($result, '103', "test4 data after pg_upgrade $mode");
107 }
108 $new->stop;
109 }
110
111 $old->clean_node();
112 $new->clean_node();
113 }
114
115 # Run pg_upgrade in tmp_check to avoid leaving files like
116 # delete_old_cluster.{sh,bat} in the source directory for VPATH and meson
117 # builds.
118 chdir ${PostgreSQL::Test::Utils::tmp_check};
119
120 test_mode('--clone');
121 test_mode('--copy');
122 test_mode('--copy-file-range');
123 test_mode('--link');
124 test_mode('--swap');
125
126 done_testing();