Debian Codegraph @

src/test/perl/PostgreSQL/Test/AdjustDump.pm

raw
1 # Copyright (c) 2024-2025, PostgreSQL Global Development Group
2
3 =pod
4
5 =head1 NAME
6
7 PostgreSQL::Test::AdjustDump - helper module for dump/restore tests
8
9 =head1 SYNOPSIS
10
11 use PostgreSQL::Test::AdjustDump;
12
13 # Adjust contents of dump output file so that dump output from original
14 # regression database and that from the restored regression database match
15 $dump = adjust_regress_dumpfile($dump, $adjust_child_columns);
16
17 =head1 DESCRIPTION
18
19 C<PostgreSQL::Test::AdjustDump> encapsulates various hacks needed to
20 compare the results of dump/restore tests.
21
22 =cut
23
24 package PostgreSQL::Test::AdjustDump;
25
26 use strict;
27 use warnings FATAL => 'all';
28
29 use Exporter 'import';
30 use Test::More;
31
32 our @EXPORT = qw(
33 adjust_regress_dumpfile
34 );
35
36 =pod
37
38 =head1 ROUTINES
39
40 =over
41
42 =item $dump = adjust_regress_dumpfile($dump, $adjust_child_columns)
43
44 Edit a dump output file, taken from the source regression database,
45 to remove the known differences to a dump taken after restoring the
46 same database.
47
48 Arguments:
49
50 =over
51
52 =item C<dump>: Contents of dump file
53
54 =item C<adjust_child_columns>: 1 indicates that the given dump file requires
55 adjusting columns in the child tables; usually when the dump is from original
56 database. 0 indicates no such adjustment is needed; usually when the dump is
57 from restored database.
58
59 =back
60
61 Returns the adjusted dump text.
62
63 Adjustments Applied:
64
65 =over
66
67 =item Column reordering on child table creation
68
69 This rearranges the column declarations in the C<CREATE TABLE... INHERITS>
70 statements in the dump file from original database so that they match those
71 from the restored database.
72
73 Only executed if C<adjust_child_columns> is true.
74
75 Context: some regression tests purposefully create child tables in such a way
76 that the order of their inherited columns differ from column orders of their
77 respective parents. In the restored database, however, the order of their
78 inherited columns are same as that of their respective parents. Thus the column
79 orders of these child tables in the original database and those in the restored
80 database differ, causing difference in the dump outputs. See
81 C<MergeAttributes()> and C<dumpTableSchema()> for details.
82
83 =item Removal of problematic C<COPY> statements
84
85 Remove COPY statements to abnormal children tables.
86
87 Context: This difference is caused because of columns that are added to parent
88 tables that already have children; because recreating the children tables puts
89 the columns from the parent ahead of columns declared locally in children,
90 these extra columns are in earlier position compared to the original database.
91 Reordering columns on the entire C<COPY> data is impractical, so we just remove
92 them.
93
94 =item Newline adjustment
95
96 Windows-style newlines are changed to Unix-style. Empty lines are trimmed.
97
98 =back
99
100 =cut
101
102 sub adjust_regress_dumpfile
103 {
104 my ($dump, $adjust_child_columns) = @_;
105
106 # use Unix newlines
107 $dump =~ s/\r\n/\n/g;
108
109 # Adjust the CREATE TABLE ... INHERITS statements.
110 if ($adjust_child_columns)
111 {
112 $dump =~ s/(^CREATE\sTABLE\sgenerated_stored_tests\.gtestxx_4\s\()
113 (\n\s+b\sinteger),
114 (\n\s+a\sinteger\sNOT\sNULL)/$1$3,$2/mgx;
115
116 $dump =~ s/(^CREATE\sTABLE\sgenerated_virtual_tests\.gtestxx_4\s\()
117 (\n\s+b\sinteger),
118 (\n\s+a\sinteger\sNOT\sNULL)/$1$3,$2/mgx;
119
120 $dump =~ s/(^CREATE\sTABLE\spublic\.test_type_diff2_c1\s\()
121 (\n\s+int_four\sbigint),
122 (\n\s+int_eight\sbigint),
123 (\n\s+int_two\ssmallint)/$1$4,$2,$3/mgx;
124
125 $dump =~ s/(^CREATE\sTABLE\spublic\.test_type_diff2_c2\s\()
126 (\n\s+int_eight\sbigint),
127 (\n\s+int_two\ssmallint),
128 (\n\s+int_four\sbigint)/$1$3,$4,$2/mgx;
129 }
130
131 # Remove COPY statements with differing column order
132 for my $table (
133 'public\.b_star', 'public\.c_star',
134 'public\.cc2', 'public\.d_star',
135 'public\.e_star', 'public\.f_star',
136 'public\.renamecolumnanother', 'public\.renamecolumnchild',
137 'public\.test_type_diff2_c1', 'public\.test_type_diff2_c2',
138 'public\.test_type_diff_c')
139 {
140 # This multiline pattern matches the whole COPY, up to the
141 # terminating "\."
142 $dump =~ s/^COPY $table \(.+?^\\\.$//sm;
143 }
144
145 # Suppress blank lines, as some places in pg_dump emit more or fewer.
146 $dump =~ s/\n\n+/\n/g;
147
148 return $dump;
149 }
150
151 =pod
152
153 =back
154
155 =cut
156
157 1;