From dea7d55a8b938c1b670eebe7662a0dace5077a0d Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Thu, 27 Jun 2024 15:17:29 +0530
Subject: [PATCH 3/3] Test dump and restore in all formats

Expanding on the previous commit, this commit modifies the test to dump
and restore regression database using all dump formats one by one.

But this changes increases the time to run test from 51s to 78s on my laptop.
If that's acceptable this commit should be squashed into the previous commit
before committing upstream.

Ashutosh Bapat
---
 src/bin/pg_upgrade/t/002_pg_upgrade.pl | 84 +++++++++++++++++---------
 1 file changed, 54 insertions(+), 30 deletions(-)

diff --git a/src/bin/pg_upgrade/t/002_pg_upgrade.pl b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
index 0e181b294d..5093b2bcaa 100644
--- a/src/bin/pg_upgrade/t/002_pg_upgrade.pl
+++ b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
@@ -576,41 +576,65 @@ sub test_regression_dump_restore
 {
 	my ($src_node, %node_params) = @_;
 	my $dst_node = PostgreSQL::Test::Cluster->new('dst_node');
-	my $dump3_file = "$tempdir/dump3.custom";
-	my $dump4_file = "$tempdir/dump4.sql";
-	my $dump5_file = "$tempdir/dump5.sql";
-
-
-	# Dump to be restored
-	command_ok(
-		[
-			'pg_dump', '-Fc', '--no-sync',
-			'-d', $src_node->connstr('regression'),
-			'-f', $dump3_file
-		],
-		'pg_dump on source instance');
 
 	$dst_node->init(%node_params);
 	$dst_node->start;
-	$dst_node->command_ok([ 'createdb', 'regression' ],
-		"created destination database");
 
-	# Restore into destination database
-	command_ok(
-		[ 'pg_restore', '-d', $dst_node->connstr('regression'), $dump3_file ],
-		'pg_restore on destination instance');
-
-	# Dump original and restored databases for comparison
+	# Dump original database for comparison
+	my $src_dump_file = "$tempdir/src_dump.sql";
 	take_dump_for_comparison($src_node->connstr('regression'),
-		$dump4_file, 'original');
-	take_dump_for_comparison($dst_node->connstr('regression'),
-		$dump5_file, 'restored');
-	my $dump4_adjusted = adjust_dump_for_restore($dump4_file, 1);
-	my $dump5_adjusted = adjust_dump_for_restore($dump5_file, 0);
-
-	# Compare adjusted dumps, there should be no differences.
-	compare_dumps($dump4_adjusted, $dump5_adjusted,
-		'dump outputs of original and restored regression database match');
+		$src_dump_file, 'original');
+	my $src_adjusted_dump = adjust_dump_for_restore($src_dump_file, 1);
+
+	# Test dump and restore in all formats one by one
+	for my $format ('tar', 'directory', 'custom', 'plain')
+	{
+		my $dump_file = "$tempdir/regression_dump.$format";
+		my $dst_dump_file = "$tempdir/dest_dump.$format";
+		my $format_spec = substr($format, 0, 1);
+		my $restored_db = 'regression_' . $format;
+
+		command_ok(
+			[
+				'pg_dump', "-F$format_spec", '--no-sync',
+				'-d', $src_node->connstr('regression'),
+				'-f', $dump_file
+			],
+			"pg_dump on source instance in '$format' format");
+
+		$dst_node->command_ok([ 'createdb', $restored_db ],
+			"created destination database '$restored_db'");
+
+		# Restore into destination database.
+		my @restore_command;
+		if ($format eq 'plain')
+		{
+			# Restore dump in "plain" format using `psql`.
+			@restore_command = [
+				'psql', '-d', $dst_node->connstr($restored_db),
+				'-f', $dump_file
+			];
+		}
+		else
+		{
+			@restore_command = [
+				'pg_restore', '-d',
+				$dst_node->connstr($restored_db), $dump_file
+			];
+		}
+		command_ok(@restore_command,
+			"pg_restore on destination instance in '$format' format");
+
+		# Dump restored database for comparison
+		take_dump_for_comparison($dst_node->connstr($restored_db),
+			$dst_dump_file, 'restored');
+		my $dst_adjusted_dump = adjust_dump_for_restore($dst_dump_file, 0);
+
+		# Compare adjusted dumps, there should be no differences.
+		compare_dumps($src_adjusted_dump, $dst_adjusted_dump,
+			"dump outputs of original and restored regression database, using format '$format' match"
+		);
+	}
 }
 
 done_testing();
-- 
2.34.1

