Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 289a826

Browse files
committed
Here is a new version of my patch for allowing pg_dump to DROP schema
elements prior to CREATEing new ones. It is under control of the -c command line option (with the default being status quo). The DROP TRIGGER portion still needs implementation. Anyone able to help clarify what exactly the CREATE TRIGGER portion does so I can fix this? Again, I have tried this with tables/indexes/sequences, but do not have other schema elements in my database. As a result, I am not 100% convinced that I got the syntax correct in all cases (but think I did, nonetheless). If anyone can check the other cases, I'd appreciate it. Cheers, Brook [I added manual page and sgml additions for the new -c option.]
1 parent c91dbcc commit 289a826

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

doc/src/sgml/ref/pg_dump.sgml

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pg_dump [ <replaceable class="parameter">dbname</replaceable> ]
2222
pg_dump [ -h <replaceable class="parameter">host</replaceable> ] [ -p <replaceable class="parameter">port</replaceable> ]
2323
[ -t <replaceable class="parameter">table</replaceable> ]
2424
[ -f <replaceable class="parameter">outputfile</replaceable> ]
25-
[ -a ] [ -d ] [ -D ] [ -n ] [ -N ] [ -o ] [ -s ] [ -u ] [ -v ] [ -z ]
25+
[ -a ] [ -c ] [ -d ] [ -D ] [ -n ] [ -N ] [ -o ] [ -s ] [ -u ] [ -v ] [ -z ]
2626
[ <replaceable class="parameter">dbname</replaceable> ]
2727
</SYNOPSIS>
2828

@@ -63,6 +63,17 @@ pg_dump [ -h <replaceable class="parameter">host</replaceable> ] [ -p <replaceab
6363
</listitem>
6464
</varlistentry>
6565

66+
<varlistentry>
67+
<term>
68+
-c
69+
</term>
70+
<listitem>
71+
<para>
72+
Clean(drop) schema prior to create.
73+
</para>
74+
</listitem>
75+
</varlistentry>
76+
6677
<varlistentry>
6778
<term>
6879
-d

src/bin/pg_dump/pg_dump.c

+81-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
*
2323
* IDENTIFICATION
24-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.99 1999/01/18 06:32:26 momjian Exp $
24+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.100 1999/01/21 22:53:36 momjian Exp $
2525
*
2626
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2727
*
@@ -117,6 +117,7 @@ int attrNames; /* put attr names into insert strings */
117117
int schemaOnly;
118118
int dataOnly;
119119
int aclsOption;
120+
bool drop_schema;
120121

121122
char g_opaque_type[10]; /* name for the opaque type */
122123

@@ -132,6 +133,8 @@ usage(const char *progname)
132133
"usage: %s [options] dbname\n", progname);
133134
fprintf(stderr,
134135
"\t -a \t\t dump out only the data, no schema\n");
136+
fprintf(stderr,
137+
"\t -c \t\t clean(drop) schema prior to create\n");
135138
fprintf(stderr,
136139
"\t -d \t\t dump data as proper insert strings\n");
137140
fprintf(stderr,
@@ -556,6 +559,7 @@ main(int argc, char **argv)
556559

557560
g_verbose = false;
558561
force_quotes = true;
562+
drop_schema = false;
559563

560564
strcpy(g_comment_start, "-- ");
561565
g_comment_end[0] = '\0';
@@ -565,13 +569,16 @@ main(int argc, char **argv)
565569

566570
progname = *argv;
567571

568-
while ((c = getopt(argc, argv, "adDf:h:nNop:st:vzu")) != EOF)
572+
while ((c = getopt(argc, argv, "acdDf:h:nNop:st:vzu")) != EOF)
569573
{
570574
switch (c)
571575
{
572576
case 'a': /* Dump data only */
573577
dataOnly = 1;
574578
break;
579+
case 'c': /* clean (i.e., drop) schema prior to create */
580+
drop_schema = true;
581+
break;
575582
case 'd': /* dump data as proper insert strings */
576583
dumpData = 1;
577584
break;
@@ -1638,6 +1645,18 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
16381645
exit_nicely(g_conn);
16391646
}
16401647
tgfunc = finfo[findx].proname;
1648+
1649+
#if 0
1650+
/* XXX - how to emit this DROP TRIGGER? */
1651+
if (drop_schema)
1652+
{
1653+
sprintf(query, "DROP TRIGGER %s ON %s;\n",
1654+
fmtId(PQgetvalue(res2, i2, i_tgname), force_quotes),
1655+
fmtId(tblinfo[i].relname, force_quotes));
1656+
fputs(query, fout);
1657+
}
1658+
#endif
1659+
16411660
sprintf(query, "CREATE TRIGGER %s ", fmtId(PQgetvalue(res2, i2, i_tgname), force_quotes));
16421661
/* Trigger type */
16431662
findx = 0;
@@ -2034,6 +2053,12 @@ dumpTypes(FILE *fout, FuncInfo *finfo, int numFuncs,
20342053

20352054
becomeUser(fout, tinfo[i].usename);
20362055

2056+
if (drop_schema)
2057+
{
2058+
sprintf(q, "DROP TYPE %s;\n", fmtId(tinfo[i].typname, force_quotes));
2059+
fputs(q, fout);
2060+
}
2061+
20372062
sprintf(q,
20382063
"CREATE TYPE %s "
20392064
"( internallength = %s, externallength = %s, input = %s, "
@@ -2130,6 +2155,9 @@ dumpProcLangs(FILE *fout, FuncInfo *finfo, int numFuncs,
21302155
lanname = checkForQuote(PQgetvalue(res, i, i_lanname));
21312156
lancompiler = checkForQuote(PQgetvalue(res, i, i_lancompiler));
21322157

2158+
if (drop_schema)
2159+
fprintf(fout, "DROP PROCEDURAL LANGUAGE '%s';\n", lanname);
2160+
21332161
fprintf(fout, "CREATE %sPROCEDURAL LANGUAGE '%s' "
21342162
"HANDLER %s LANCOMPILER '%s';\n",
21352163
(PQgetvalue(res, i, i_lanpltrusted)[0] == 't') ? "TRUSTED " : "",
@@ -2245,6 +2273,23 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
22452273
PQclear(res);
22462274
}
22472275

2276+
if (drop_schema)
2277+
{
2278+
sprintf(q, "DROP FUNCTION %s (", fmtId(finfo[i].proname, force_quotes));
2279+
for (j = 0; j < finfo[i].nargs; j++)
2280+
{
2281+
char *typname;
2282+
2283+
typname = findTypeByOid(tinfo, numTypes, finfo[i].argtypes[j]);
2284+
sprintf(q, "%s%s%s",
2285+
q,
2286+
(j > 0) ? "," : "",
2287+
fmtId(typname, false));
2288+
}
2289+
sprintf (q, "%s);\n", q);
2290+
fputs(q, fout);
2291+
}
2292+
22482293
sprintf(q, "CREATE FUNCTION %s (", fmtId(finfo[i].proname, force_quotes));
22492294
for (j = 0; j < finfo[i].nargs; j++)
22502295
{
@@ -2355,6 +2400,14 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
23552400

23562401
becomeUser(fout, oprinfo[i].usename);
23572402

2403+
if (drop_schema)
2404+
{
2405+
sprintf(q, "DROP OPERATOR %s (%s, %s);\n", oprinfo[i].oprname,
2406+
fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft), false),
2407+
fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprright), false));
2408+
fputs(q, fout);
2409+
}
2410+
23582411
sprintf(q,
23592412
"CREATE OPERATOR %s "
23602413
"(PROCEDURE = %s %s %s %s %s %s %s %s %s);\n ",
@@ -2450,6 +2503,13 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
24502503

24512504
becomeUser(fout, agginfo[i].usename);
24522505

2506+
if (drop_schema)
2507+
{
2508+
sprintf(q, "DROP AGGREGATE %s %s;\n", agginfo[i].aggname,
2509+
fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggbasetype), false));
2510+
fputs(q, fout);
2511+
}
2512+
24532513
sprintf(q, "CREATE AGGREGATE %s ( %s %s%s %s%s %s );\n",
24542514
agginfo[i].aggname,
24552515
basetype,
@@ -2649,6 +2709,12 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
26492709

26502710
becomeUser(fout, tblinfo[i].usename);
26512711

2712+
if (drop_schema)
2713+
{
2714+
sprintf(q, "DROP TABLE %s;\n", fmtId(tblinfo[i].relname, force_quotes));
2715+
fputs(q, fout);
2716+
}
2717+
26522718
sprintf(q, "CREATE TABLE %s (\n\t", fmtId(tblinfo[i].relname, force_quotes));
26532719
actual_atts = 0;
26542720
for (j = 0; j < tblinfo[i].numatts; j++)
@@ -2865,6 +2931,13 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
28652931

28662932
strcpy(id1, fmtId(indinfo[i].indexrelname, force_quotes));
28672933
strcpy(id2, fmtId(indinfo[i].indrelname, force_quotes));
2934+
2935+
if (drop_schema)
2936+
{
2937+
sprintf(q, "DROP INDEX %s;\n", id1);
2938+
fputs(q, fout);
2939+
}
2940+
28682941
fprintf(fout, "CREATE %s INDEX %s on %s using %s (",
28692942
(strcmp(indinfo[i].indisunique, "t") == 0) ? "UNIQUE" : "",
28702943
id1,
@@ -3125,6 +3198,12 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
31253198

31263199
PQclear(res);
31273200

3201+
if (drop_schema)
3202+
{
3203+
sprintf(query, "DROP SEQUENCE %s;\n", fmtId(tbinfo.relname, force_quotes));
3204+
fputs(query, fout);
3205+
}
3206+
31283207
sprintf(query,
31293208
"CREATE SEQUENCE %s start %d increment %d maxvalue %d "
31303209
"minvalue %d cache %d %s;\n",

src/man/pg_dump.1

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" This is -*-nroff-*-
22
.\" XXX standard disclaimer belongs here....
3-
.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.15 1998/10/07 02:49:10 momjian Exp $
3+
.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.16 1999/01/21 22:53:37 momjian Exp $
44
.TH PG_DUMP UNIX 7/15/98 PostgreSQL PostgreSQL
55
.SH NAME
66
pg_dump - dumps out a Postgres database into a script file
@@ -10,6 +10,9 @@ pg_dump - dumps out a Postgres database into a script file
1010
.BR "-a"
1111
]
1212
[\c
13+
.BR "-c"
14+
]
15+
[\c
1316
.BR "-d"
1417
]
1518
[\c
@@ -74,6 +77,9 @@ pg_dump understands the following options:
7477
.BR "-a" ""
7578
Dump out only the data, no schema
7679
.TP
80+
.BR "-c" ""
81+
Clean(drop) schema prior to create
82+
.TP
7783
.BR "-d" ""
7884
Dump data as proper insert strings
7985
.TP

0 commit comments

Comments
 (0)