summaryrefslogtreecommitdiff
path: root/src/bin/pg_dump/pg_upgrade
blob: 06cf8e3411b7a9d298bb887fa8bcc9762ec18f69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
:
trap "rm -f /tmp/$$" 0 1 2 3 15

if [ "$#" -eq 0 ]
then	echo "Usage:  $0 [-f inputfile] old_data_dir database" 1>&2
	exit 1
fi

if [ "X$1" = "X-f" ]
then	INPUT="$2"
	shift 2
	if [ ! -f "$INPUT" ]
	then	echo "$INPUT does not exist" 1>&2
		exit 1
	fi
else	INPUT=""
fi

if [ "$#" -ne 2 ]
then	echo "Usage:  $0 [-f input_file] old_data_dir database" 1>&2
	exit 1
fi

OLDDIR="$1"
DATABASE="$2"

# check things

if [ ! -f "./lib/global1.bki.source" ]
then	echo "$0 must be run from the top of the postgres directory tree." 1>&2
	exit 1
fi

if [ ! -d "./$OLDDIR" ]
then	echo "You must rename your old /data directory to /$OLDDIR and run initdb." 1>&2
	exit 1
fi

if [ ! -d "./$OLDDIR/data/base/$DATABASE" ]
then	echo "There is not database $DATABASE in ./$OLDDIR/data/base." 1>&2
	exit 1
fi

if [ ! -d "./data" ]
then	echo "You must run initdb to create the template1 database." 1>&2
	exit 1
fi

if [ ! -d "./data/base/template1" ]
then	echo "$0 must be run as the postgres superuser." 1>&2
	exit 1
fi

# do I need to create a database?

if [ "$DATABASE" != "template1" ]
then	echo "Dropping and recreating database $DATABASE." 1>&2
	destroydb "$DATABASE" >/dev/null 2>&1
	createdb "$DATABASE"
fi

# remove any COPY statements, preserve pgdump_oid setting from pg_dumpall

cat $INPUT | awk '	{
				if (toupper($0) ~ /^COPY / &&
				    toupper($0) !~ /^COPY[ 	]*PGDUMP_OID/ )
					while (getline $0 > 0 && $0 != "\\.")
						;
				else	print $0;
			}' >/tmp/$$
 
#create empty tables/indexes

psql "$DATABASE" <"/tmp/$$"
set -x

for DIR in data/base/*
do
	BASEDIR="`basename $DIR`"
	if [ -d "$DIR" -a \
	     -d "$OLDDIR/$DIR" -a \
		\( "$DATABASE" = "$BASEDIR" -o "$DATABASE" = "template1" \) ]
	then	for FILE in $OLDDIR/$DIR/*
		do
			BASEFILE="`basename $FILE`"
			if [ `expr "$BASEFILE" : "pg_"` -ne 3 -a \
				"$BASEFILE" != "PG_VERSION" ]
			then	mv $FILE $DIR
			fi
		done
	fi
done

echo "You may removed the $OLDDIR directory with 'rm -r $OLDDIR'."