From 54f98d264b7ac463a5373950c548636bf9653690 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@otacoo.com>
Date: Wed, 9 Sep 2015 10:41:01 +0900
Subject: [PATCH] Add test facility to check dump/restore with extensions

The test added compares a dump taken from a source database containing a set
of extensions and a target database after dumping the contents from source and
restore them to target.

For now this test facility includes one extension to test dumpable objects
with foreign keys, but could be extended with more.
---
 src/test/modules/Makefile                          |  1 +
 src/test/modules/pg_dumprestore/.gitignore         |  1 +
 src/test/modules/pg_dumprestore/Makefile           | 24 ++++++++++++
 src/test/modules/pg_dumprestore/README             |  5 +++
 .../pg_dumprestore/t/001_dump_restore_test.pl      | 43 ++++++++++++++++++++++
 src/test/modules/pg_dumprestore/tables_fk--1.0.sql | 20 ++++++++++
 src/test/modules/pg_dumprestore/tables_fk.control  |  5 +++
 src/tools/msvc/Mkvcbuild.pm                        |  3 +-
 src/tools/msvc/vcregress.pl                        |  3 ++
 9 files changed, 104 insertions(+), 1 deletion(-)
 create mode 100644 src/test/modules/pg_dumprestore/.gitignore
 create mode 100644 src/test/modules/pg_dumprestore/Makefile
 create mode 100644 src/test/modules/pg_dumprestore/README
 create mode 100644 src/test/modules/pg_dumprestore/t/001_dump_restore_test.pl
 create mode 100644 src/test/modules/pg_dumprestore/tables_fk--1.0.sql
 create mode 100644 src/test/modules/pg_dumprestore/tables_fk.control

diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile
index 9b96654..633dc6f 100644
--- a/src/test/modules/Makefile
+++ b/src/test/modules/Makefile
@@ -8,6 +8,7 @@ SUBDIRS = \
 		  brin \
 		  commit_ts \
 		  dummy_seclabel \
+		  pg_dumprestore \
 		  test_ddl_deparse \
 		  test_parser \
 		  test_rls_hooks \
diff --git a/src/test/modules/pg_dumprestore/.gitignore b/src/test/modules/pg_dumprestore/.gitignore
new file mode 100644
index 0000000..b6a2a01
--- /dev/null
+++ b/src/test/modules/pg_dumprestore/.gitignore
@@ -0,0 +1 @@
+/tmp_check/
diff --git a/src/test/modules/pg_dumprestore/Makefile b/src/test/modules/pg_dumprestore/Makefile
new file mode 100644
index 0000000..611f23a
--- /dev/null
+++ b/src/test/modules/pg_dumprestore/Makefile
@@ -0,0 +1,24 @@
+# src/test/modules/pg_dumprestore/Makefile
+
+EXTENSION = tables_fk
+DATA = tables_fk--1.0.sql
+PGFILEDESC = "pg_dumprestore - set of extensions dumped and restored"
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/pg_dumprestore
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
+
+check: all
+	$(prove_check)
+
+installcheck: install
+	$(prove_installcheck)
+
+temp-install: EXTRA_INSTALL=src/test/modules/pg_dumprestore
diff --git a/src/test/modules/pg_dumprestore/README b/src/test/modules/pg_dumprestore/README
new file mode 100644
index 0000000..6932c7e
--- /dev/null
+++ b/src/test/modules/pg_dumprestore/README
@@ -0,0 +1,5 @@
+pg_dumprestore
+==============
+
+Facility to test dump and restore of a PostgreSQL instance using a set
+of extensions.
diff --git a/src/test/modules/pg_dumprestore/t/001_dump_restore_test.pl b/src/test/modules/pg_dumprestore/t/001_dump_restore_test.pl
new file mode 100644
index 0000000..1b57c34
--- /dev/null
+++ b/src/test/modules/pg_dumprestore/t/001_dump_restore_test.pl
@@ -0,0 +1,43 @@
+use strict;
+use warnings;
+use File::Compare;
+use TestLib;
+use Test::More tests => 4;
+
+my $tempdir = tempdir;
+
+start_test_server $tempdir;
+
+my $source_db = 'foobar1';
+my $target_db = 'foobar2';
+my $source_dump = $tempdir . '/dump_' . $source_db . '.sql';
+my $target_dump = $tempdir . '/dump_' . $target_db . '.sql';
+system_or_bail 'createdb', $source_db;
+system_or_bail 'createdb', $target_db;
+
+# Deploy extensions to test
+psql $source_db, 'CREATE EXTENSION tables_fk';
+
+# Insert some data before running the dump on tables of tables_fk, this
+# is needed to check consistent data dump of tables with foreign key
+# dependencies.
+psql $source_db, 'INSERT INTO cc_tab_fkey VALUES (1)';
+psql $source_db, 'INSERT INTO bb_tab_fkey VALUES (1)';
+psql $source_db, 'INSERT INTO aa_tab_fkey VALUES (1)';
+
+# Create a table depending on a FK defined in tables_fk.
+psql $source_db, 'CREATE TABLE dd_tab_fkey (id int REFERENCES bb_tab_fkey(id))';
+psql $source_db, 'INSERT INTO dd_tab_fkey VALUES (1)';
+
+# Take a dump from source then re-deploy it to target database.
+command_ok(['pg_dump', '-d', $source_db, '-f', $source_dump],
+		   'dump taken from source database');
+command_ok(['psql', '--set=ON_ERROR_STOP=on', '-d', $target_db, '-f',
+			$source_dump], 'dump of source restored on target database');
+
+# Finish by taking a dump of the target database and then compare it
+# with the one from source.
+command_ok(['pg_dump', '-d', $target_db, '-f', $target_dump],
+		   'dump taken from target database');
+my $result = compare($source_dump, $target_dump) == 0;
+ok($result, "Diff between source and target dump");
diff --git a/src/test/modules/pg_dumprestore/tables_fk--1.0.sql b/src/test/modules/pg_dumprestore/tables_fk--1.0.sql
new file mode 100644
index 0000000..e424610
--- /dev/null
+++ b/src/test/modules/pg_dumprestore/tables_fk--1.0.sql
@@ -0,0 +1,20 @@
+/* src/test/modules/tables_fk/tables_fk--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION tables_fk" to load this file. \quit
+
+CREATE TABLE IF NOT EXISTS cc_tab_fkey (
+	id int PRIMARY KEY
+);
+
+CREATE TABLE IF NOT EXISTS bb_tab_fkey (
+	id int PRIMARY KEY REFERENCES cc_tab_fkey(id)
+);
+
+CREATE TABLE IF NOT EXISTS aa_tab_fkey (
+	id int REFERENCES bb_tab_fkey(id)
+);
+
+SELECT pg_catalog.pg_extension_config_dump('aa_tab_fkey', '');
+SELECT pg_catalog.pg_extension_config_dump('bb_tab_fkey', '');
+SELECT pg_catalog.pg_extension_config_dump('cc_tab_fkey', '');
diff --git a/src/test/modules/pg_dumprestore/tables_fk.control b/src/test/modules/pg_dumprestore/tables_fk.control
new file mode 100644
index 0000000..b9f31ee
--- /dev/null
+++ b/src/test/modules/pg_dumprestore/tables_fk.control
@@ -0,0 +1,5 @@
+# tables_fk extension
+comment = 'tables_fk - dumpable tables linked with foreign keys'
+default_version = '1.0'
+module_pathname = '$libdir/tables_fk'
+relocatable = true
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 3abbb4c..71e0749 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -41,7 +41,8 @@ my $contrib_extrasource = {
 	'seg'  => [ 'contrib/seg/segscan.l',   'contrib/seg/segparse.y' ], };
 my @contrib_excludes = (
 	'commit_ts',      'hstore_plperl', 'hstore_plpython', 'intagg',
-	'ltree_plpython', 'pgcrypto',      'sepgsql',         'brin');
+	'ltree_plpython', 'pgcrypto',      'sepgsql',         'brin',
+	'pg_dumprestore');
 
 # Set of variables for frontend modules
 my $frontend_defines = { 'initdb' => 'FRONTEND' };
diff --git a/src/tools/msvc/vcregress.pl b/src/tools/msvc/vcregress.pl
index d3d736b..c1bd98b 100644
--- a/src/tools/msvc/vcregress.pl
+++ b/src/tools/msvc/vcregress.pl
@@ -344,6 +344,9 @@ sub modulescheck
 	my $mstat = 0;
 	foreach my $module (glob("*"))
 	{
+		# Already tested by tapcheck
+		next if ($module eq "pg_dumprestore");
+
 		subdircheck("$topdir/src/test/modules", $module);
 		my $status = $? >> 8;
 		$mstat ||= $status;
-- 
2.5.1

