From 0c7a45348d9a699aeeef6cbc911beca6e6e45235 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@otacoo.com>
Date: Sat, 7 Mar 2015 21:18:58 +0000
Subject: [PATCH 2/2] Add TAP test for pg_dump checking data dump of extension
 tables

The test added checks the data dump ordering of tables added in an extension
linked among them with foreign keys. In order to do that, a test extension in
the set of TAP tests of pg_dump, combined with a TAP test script making use
of it.
---
 src/bin/pg_dump/.gitignore                         |  2 ++
 src/bin/pg_dump/Makefile                           |  6 +++++
 src/bin/pg_dump/t/001_dump_test.pl                 | 27 ++++++++++++++++++++++
 src/bin/pg_dump/t/extra/Makefile                   | 14 +++++++++++
 src/bin/pg_dump/t/extra/tables_fk/Makefile         | 16 +++++++++++++
 src/bin/pg_dump/t/extra/tables_fk/README           |  5 ++++
 .../pg_dump/t/extra/tables_fk/tables_fk--1.0.sql   | 20 ++++++++++++++++
 .../pg_dump/t/extra/tables_fk/tables_fk.control    |  5 ++++
 8 files changed, 95 insertions(+)
 create mode 100644 src/bin/pg_dump/t/001_dump_test.pl
 create mode 100644 src/bin/pg_dump/t/extra/Makefile
 create mode 100644 src/bin/pg_dump/t/extra/tables_fk/Makefile
 create mode 100644 src/bin/pg_dump/t/extra/tables_fk/README
 create mode 100644 src/bin/pg_dump/t/extra/tables_fk/tables_fk--1.0.sql
 create mode 100644 src/bin/pg_dump/t/extra/tables_fk/tables_fk.control

diff --git a/src/bin/pg_dump/.gitignore b/src/bin/pg_dump/.gitignore
index c2c8677..02666f9 100644
--- a/src/bin/pg_dump/.gitignore
+++ b/src/bin/pg_dump/.gitignore
@@ -3,3 +3,5 @@
 /pg_dump
 /pg_dumpall
 /pg_restore
+
+/tmp_check
diff --git a/src/bin/pg_dump/Makefile b/src/bin/pg_dump/Makefile
index e890a62..1a3f2ca 100644
--- a/src/bin/pg_dump/Makefile
+++ b/src/bin/pg_dump/Makefile
@@ -49,5 +49,11 @@ installdirs:
 uninstall:
 	rm -f $(addprefix '$(DESTDIR)$(bindir)'/, pg_dump$(X) pg_restore$(X) pg_dumpall$(X))
 
+check: all
+	$(prove_check)
+
+installcheck: install
+	$(prove_installcheck)
+
 clean distclean maintainer-clean:
 	rm -f pg_dump$(X) pg_restore$(X) pg_dumpall$(X) $(OBJS) pg_dump.o common.o pg_dump_sort.o pg_restore.o pg_dumpall.o kwlookup.c $(KEYWRDOBJS)
diff --git a/src/bin/pg_dump/t/001_dump_test.pl b/src/bin/pg_dump/t/001_dump_test.pl
new file mode 100644
index 0000000..0953bf5
--- /dev/null
+++ b/src/bin/pg_dump/t/001_dump_test.pl
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+use TestLib;
+use Test::More tests => 3;
+
+my $tempdir = tempdir;
+
+start_test_server $tempdir;
+
+psql 'postgres', 'CREATE EXTENSION tables_fk';
+
+# Insert some data before running the dump, this is needed to check
+# consistent data dump of tables with foreign key dependencies
+psql 'postgres', 'INSERT INTO cc_tab_fkey VALUES (1)';
+psql 'postgres', 'INSERT INTO bb_tab_fkey VALUES (1)';
+psql 'postgres', 'INSERT INTO aa_tab_fkey VALUES (1)';
+
+# Create a table depending on a FK defined in the extension
+psql 'postgres', 'CREATE TABLE dd_tab_fkey (id int REFERENCES bb_tab_fkey(id))';
+psql 'postgres', 'INSERT INTO dd_tab_fkey VALUES (1)';
+
+# Take a dump then re-deploy it to a new database
+command_ok(['pg_dump', '-d', 'postgres', '-f', "$tempdir/dump.sql"],
+		   'taken dump with installed extension');
+command_ok(['createdb', 'foobar1'], 'created new database to redeploy dump');
+command_ok(['psql', '--set=ON_ERROR_STOP=on', '-d', 'foobar1', '-f',
+			"$tempdir/dump.sql"], 'dump restored');
diff --git a/src/bin/pg_dump/t/extra/Makefile b/src/bin/pg_dump/t/extra/Makefile
new file mode 100644
index 0000000..e37ab06
--- /dev/null
+++ b/src/bin/pg_dump/t/extra/Makefile
@@ -0,0 +1,14 @@
+# src/bin/pg_dump/t/extra/Makefile
+
+subdir = src/bin/pg_dump/t/extra
+top_builddir = ../../../../..
+include $(top_builddir)/src/Makefile.global
+
+SUBDIRS = tables_fk
+
+all: submake-errcodes
+
+submake-errcodes:
+	$(MAKE) -C $(top_builddir)/src/backend submake-errcodes
+
+$(recurse)
diff --git a/src/bin/pg_dump/t/extra/tables_fk/Makefile b/src/bin/pg_dump/t/extra/tables_fk/Makefile
new file mode 100644
index 0000000..3738028
--- /dev/null
+++ b/src/bin/pg_dump/t/extra/tables_fk/Makefile
@@ -0,0 +1,16 @@
+#  src/bin/pg_dump/t/extra/tables_fk/Makefile
+
+EXTENSION = tables_fk
+DATA = tables_fk--1.0.sql
+PGFILEDESC = "tables_fk - dumpable tables linked with foreign keys"
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/bin/pg_dump/t/extra/tables_fk
+top_builddir = ../../../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/bin/pg_dump/t/extra/tables_fk/README b/src/bin/pg_dump/t/extra/tables_fk/README
new file mode 100644
index 0000000..9914f1f
--- /dev/null
+++ b/src/bin/pg_dump/t/extra/tables_fk/README
@@ -0,0 +1,5 @@
+tables_fk
+=========
+
+Simple module used to check data dump ordering of pg_dump with tables
+linked with foreign keys.
diff --git a/src/bin/pg_dump/t/extra/tables_fk/tables_fk--1.0.sql b/src/bin/pg_dump/t/extra/tables_fk/tables_fk--1.0.sql
new file mode 100644
index 0000000..26b5d4c
--- /dev/null
+++ b/src/bin/pg_dump/t/extra/tables_fk/tables_fk--1.0.sql
@@ -0,0 +1,20 @@
+/* src/bin/pg_dump/t/extra/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/bin/pg_dump/t/extra/tables_fk/tables_fk.control b/src/bin/pg_dump/t/extra/tables_fk/tables_fk.control
new file mode 100644
index 0000000..b9f31ee
--- /dev/null
+++ b/src/bin/pg_dump/t/extra/tables_fk/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
-- 
2.3.1

