Hi

2017-02-10 6:00 GMT+01:00 Michael Paquier <michael.paqu...@gmail.com>:

> On Thu, Feb 9, 2017 at 5:13 AM, Pavel Stehule <pavel.steh...@gmail.com>
> wrote:
> > here is a patch
>
> Thanks.
>
> -       for (sl = dblist; sl; sl = sl->next)
> -           create_database(sl->str);
> +       if (templatelist != NULL)
> +       {
> +           _stringlist *tl;
> +
> +           for (sl = dblist, tl = templatelist; sl; sl = sl->next, tl
> = tl->next)
> +           {
> +               if (tl != NULL)
> +                   create_database(sl->str, tl->str);
> +               else
> +               {
> +                   fprintf(stderr, _("%s: the template list is
> shorter than database list\n"),
> +                           progname);
> +                   exit(2);
> +               }
> +           }
> +       }
> +       else
> +           for (sl = dblist; sl; sl = sl->next)
> +               create_database(sl->str, "template0");
> There is one problem here: if the length of the template list is
> shorter than the database list, databases get halfly created, then
> pg_regress complains, letting the instance in a half-way state. I
> think that you had better do any sanity checks before creating or even
> dropping existing databases.
>

here is new update - check is done before any creating

Regards

Pavel


> --
> Michael
>
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index d4d00d9..b5f5c2f 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -68,6 +68,7 @@ const char *pretty_diff_opts = "-w -C3";
 
 /* options settable from command line */
 _stringlist *dblist = NULL;
+_stringlist *templatelist = NULL;
 bool		debug = false;
 char	   *inputdir = ".";
 char	   *outputdir = ".";
@@ -1907,7 +1908,7 @@ drop_database_if_exists(const char *dbname)
 }
 
 static void
-create_database(const char *dbname)
+create_database(const char *dbname, const char *template)
 {
 	_stringlist *sl;
 
@@ -1917,10 +1918,12 @@ create_database(const char *dbname)
 	 */
 	header(_("creating database \"%s\""), dbname);
 	if (encoding)
-		psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0 ENCODING='%s'%s", dbname, encoding,
+		psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=\"%s\" ENCODING='%s'%s",
+					 dbname, template, encoding,
 					 (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
 	else
-		psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=template0%s", dbname,
+		psql_command("postgres", "CREATE DATABASE \"%s\" TEMPLATE=\"%s\"%s",
+					 dbname, template,
 					 (nolocale) ? " LC_COLLATE='C' LC_CTYPE='C'" : "");
 	psql_command(dbname,
 				 "ALTER DATABASE \"%s\" SET lc_messages TO 'C';"
@@ -1995,6 +1998,7 @@ help(void)
 	printf(_("  --outputdir=DIR           place output files in DIR (default \".\")\n"));
 	printf(_("  --schedule=FILE           use test ordering schedule from FILE\n"));
 	printf(_("                            (can be used multiple times to concatenate)\n"));
+	printf(_("  --template=DB             use template DB (default \"template0\")\n"));
 	printf(_("  --temp-instance=DIR       create a temporary instance in DIR\n"));
 	printf(_("  --use-existing            use an existing installation\n"));
 	printf(_("\n"));
@@ -2041,6 +2045,7 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 		{"launcher", required_argument, NULL, 21},
 		{"load-extension", required_argument, NULL, 22},
 		{"config-auth", required_argument, NULL, 24},
+		{"template", required_argument, NULL, 25},
 		{NULL, 0, NULL, 0}
 	};
 
@@ -2154,6 +2159,16 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 			case 24:
 				config_auth_datadir = pg_strdup(optarg);
 				break;
+			case 25:
+
+				/*
+				 * If a default template was specified, we need to remove it
+				 * before we add the specified one.
+				 */
+				free_stringlist(&templatelist);
+				split_to_stringlist(optarg, ",", &templatelist);
+				break;
+
 			default:
 				/* getopt_long already emitted a complaint */
 				fprintf(stderr, _("\nTry \"%s -h\" for more information.\n"),
@@ -2454,8 +2469,35 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 	 */
 	if (!use_existing)
 	{
-		for (sl = dblist; sl; sl = sl->next)
-			create_database(sl->str);
+		if (templatelist != NULL)
+		{
+			_stringlist *tl;
+
+			/*
+			 * The template list should to have same length as database list.
+			 * Check it before any database creation.
+			 */
+			for (sl = dblist, tl = templatelist; sl; sl = sl->next, tl = tl->next)
+				if (tl == NULL)
+				{
+					fprintf(stderr, _("%s: the template list is shorter than database list\n"),
+							progname);
+					exit(2);
+				}
+			if (tl != NULL)
+			{
+				fprintf(stderr, _("%s: the template list is longer than database list\n"),
+						progname);
+				exit(2);
+			}
+
+			for (sl = dblist, tl = templatelist; sl; sl = sl->next, tl = tl->next)
+				create_database(sl->str, tl->str);
+		}
+		else
+			for (sl = dblist; sl; sl = sl->next)
+				create_database(sl->str, "template0");
+
 		for (sl = extraroles; sl; sl = sl->next)
 			create_role(sl->str, dblist);
 	}
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to