From 2a5efca7adb5f91f51cb63cde13e845b454c9f34 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Wed, 12 Mar 2025 20:53:45 +0900
Subject: [PATCH v12 2/2] Add testcase

---
 src/test/subscription/t/007_ddl.pl | 81 ++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)

diff --git a/src/test/subscription/t/007_ddl.pl b/src/test/subscription/t/007_ddl.pl
index 4d3b917ac0..e663e9f515 100644
--- a/src/test/subscription/t/007_ddl.pl
+++ b/src/test/subscription/t/007_ddl.pl
@@ -69,6 +69,87 @@ ok( $stderr =~
 	"Alter subscription set publication throws warning for non-existent publication"
 );
 
+# Cleanup
+$node_publisher->safe_psql('postgres', "DROP PUBLICATION mypub;");
+$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION mysub1");
+
+#
+# Test ALTER PUBLICATION RENAME command during the replication
+#
+
+pass "renaming publications can work";
+
+# Test function for swaping name of publications
+sub test_swap
+{
+	my ($table_name, $pubname, $appname) = @_;
+
+	# Confirms tuples can be replicated
+	$node_publisher->safe_psql('postgres', "INSERT INTO $table_name VALUES (1);");
+	$node_publisher->wait_for_catchup($appname);
+	my $result =
+		$node_subscriber->safe_psql('postgres', "SELECT a FROM $table_name");
+	is($result, qq(1), 'check replication worked well');
+
+	# Swap the name of publications; $pubname <-> pub_empty
+	$node_publisher->safe_psql('postgres', qq[
+		ALTER PUBLICATION $pubname RENAME TO tap_pub_tmp;
+		ALTER PUBLICATION pub_empty RENAME TO $pubname;
+		ALTER PUBLICATION tap_pub_tmp RENAME TO pub_empty;
+	]);
+
+	# Insert the data again
+	$node_publisher->safe_psql('postgres', "INSERT INTO $table_name VALUES (2);");
+	$node_publisher->wait_for_catchup($appname);
+
+	# Confirms the second tuple won't be replicated because $pubname does not
+	# contains relations anymore.
+	$result =
+		$node_subscriber->safe_psql('postgres', "SELECT a FROM $table_name ORDER BY a");
+	is($result, qq(1),
+		'check the tuple inserted after the RENAME was not replicated');
+
+	# Swap the name of publications again
+	$node_publisher->safe_psql('postgres', qq[
+		ALTER PUBLICATION $pubname RENAME TO tap_pub_tmp;
+		ALTER PUBLICATION pub_empty RENAME TO $pubname;
+		ALTER PUBLICATION tap_pub_tmp RENAME TO pub_empty;
+	]);
+
+	# Confirms the replication is now resumed
+	$node_publisher->safe_psql('postgres', "INSERT INTO $table_name VALUES (3);");
+	$result =
+		$node_subscriber->safe_psql('postgres', "SELECT a FROM $table_name ORDER BY a");
+	is($result, qq(1
+3), 'check replicated resumed after renaming gain');
+}
+
+# Create another table
+$ddl = "CREATE TABLE test2 (a int, b text);";
+$node_publisher->safe_psql('postgres', $ddl);
+$node_subscriber->safe_psql('postgres', $ddl);
+
+# Create publications and a subscription
+$node_publisher->safe_psql('postgres', qq[
+	CREATE PUBLICATION pub_empty;
+	CREATE PUBLICATION pub_for_tab FOR TABLE test1;
+	CREATE PUBLICATION pub_for_all_tables FOR ALL TABLES;
+]);
+$node_subscriber->safe_psql('postgres',
+	"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION pub_for_tab WITH (copy_data = off)"
+);
+
+# Confirms RENAME command works well for a publication
+test_swap('test1', 'pub_for_tab', 'tap_sub');
+
+# Switches a publication which includes all tables
+$node_subscriber->safe_psql('postgres',
+	"ALTER SUBSCRIPTION tap_sub SET PUBLICATION pub_for_all_tables WITH (refresh = true, copy_data = false);"
+);
+
+# Confirms RENAME command works well for ALL TABLES publication
+test_swap('test2', 'pub_for_all_tables', 'tap_sub');
+
 $node_subscriber->stop;
 $node_publisher->stop;
 
-- 
2.43.5

