Skip to content

Commit 8269e4a

Browse files
committed
Integrated pgpro_upgrade system into the sources
1 parent a24a89c commit 8269e4a

File tree

6 files changed

+130
-1
lines changed

6 files changed

+130
-1
lines changed

src/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ SUBDIRS = \
2525
bin \
2626
pl \
2727
makefiles \
28-
test/regress
28+
test/regress \
29+
pgpro-upgrade
2930

3031
# There are too many interdependencies between the subdirectories, so
3132
# don't attempt parallel make here.

src/pgpro-upgrade/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#-------------------------------------------------------------------------
2+
#
3+
# Makefile for src/pgpro-upgrade (Postgres Pro specific upgrade scripts)
4+
#
5+
# Copyright (c) 2016, Postgres Professional
6+
#
7+
# src/pgpro-upgrade/Makefile
8+
#
9+
#-------------------------------------------------------------------------
10+
11+
subdir = src/pgpro-upgrade
12+
top_builddir = ../..
13+
include $(top_builddir)/src/Makefile.global
14+
srcdir=$(top_srcdir)/$(subdir)
15+
16+
all:
17+
true
18+
19+
install: installdirs
20+
$(INSTALL_PROGRAM) $(srcdir)/pgpro_upgrade '$(DESTDIR)$(bindir)/pgpro_upgrade'
21+
$(INSTALL_DATA) $(srcdir)/*.sql $(srcdir)/*.test '$(DESTDIR)$(datadir)/pgpro-upgrade'
22+
installdirs:
23+
$(MKDIR_P) '$(DESTDIR)$(bindir)'
24+
$(MKDIR_P) '$(DESTDIR)$(datadir)/pgpro-upgrade'

src/pgpro-upgrade/README

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
PGPRO upgarde system
2+
====================
3+
4+
This directory contains pgpro_upgrade script which allows to add some
5+
features (such as internal functions) into catalog of existing database.
6+
7+
HOW TO INVOKE SCRIPT
8+
--------------------
9+
10+
Script is intended to be run from postinst script of installable
11+
packages. It should be invoked as database owning system user
12+
(typically, 'postgres').
13+
14+
Script expects that PGDATA environment variable points to database
15+
cluster to be upgraded.
16+
17+
Script is installed into postgresql bin directory, and user can run it
18+
manually, if he has database cluster, which are not known by package
19+
configuration file.
20+
21+
Script should be invoked when instance of PostgreSQL which is upgraded,
22+
is stopped.
23+
24+
Script doesn't depend of any authentication settings in pg_hba.conf,
25+
because it starts postgres in single-user mode to perform upgrades.
26+
27+
It is safe to invoke script multiple times, because it does check for
28+
features it is going to add.
29+
30+
HOW TO ADD NEW FEATURE
31+
----------------------
32+
33+
You need to add two sql files in this directory, if you want to add a
34+
feature.
35+
36+
Both files should have same base name and one — extension .test and
37+
other .sql
38+
39+
1. Script with .sql extension contains usial create commands which would
40+
create neccessary database object(s).
41+
42+
2. Script with .text extension should contain single select query,
43+
which returns single boolean value (i.e. one column and one row). This
44+
value should be 'f' if feature is not found in the database and should
45+
be installed, and 't' if it already exists.
46+

src/pgpro-upgrade/pgpro_upgrade

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/sh
2+
# Utilities needed
3+
# 1. sh
4+
# 3. sed
5+
if [ -z "$PGDATA" ]; then
6+
echo "PGDATA environment variable is not set. Stop." 1>&2
7+
exit 1
8+
fi
9+
PGBIN="`echo $0|sed 's![^/\\]*$!!'`"
10+
11+
echo "$PGBIN"
12+
13+
case "$PGBIN" in
14+
*/bin/)
15+
PGSHARE="`echo $PGBIN|sed 's!bin/$!share!'`"
16+
;;
17+
*) PGBIN=""
18+
PGSHARE=""
19+
;;
20+
esac
21+
22+
for dir in "$PGSHARE" /usr/pgsql-9.5/share /usr/share/postgresql/9.5 /usr/pgsql/9.5/share ; do
23+
if [ -d "$dir/pgpro-upgrade" ]; then
24+
DIR="$dir/pgpro-upgrade"
25+
break
26+
fi
27+
done
28+
if [ -z "$DIR" ]; then
29+
echo "Cannot find feature scripts" 1>&2
30+
exit 1
31+
fi
32+
BASELIST=`echo "select datname from pg_database;"|
33+
"${PGBIN}postgres" --single template0 |
34+
sed -n 's/^.*1: datname = "\([^"]*\)".*$/\1/p'`
35+
36+
if [ -z "$BASELIST" ]; then
37+
echo "Databases for upgrade not found" 1>&2
38+
exit 1
39+
fi
40+
41+
echo "Upgrading databases $BASELIST"
42+
43+
#Search for upgrade scripts
44+
for i in "$DIR"/*.test; do
45+
create="`echo "$i" |sed 's/\.test$/.sql/'`"
46+
found=`< "$i" "${PGBIN}postgres" --single template0 |
47+
sed -n 's/^[ ]*1: [^ ]* = "\([ft]\)"[ ].*$/\1/p'`
48+
if [ "$found" = "f" ]; then
49+
for base in $BASELIST; do
50+
echo "Executing $create in $base"
51+
< "$create" "${PGBIN}postgres" --single template0
52+
done
53+
fi
54+
done
55+
56+

src/pgpro-upgrade/pgpro_version.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE FUNCTION pgpro_version() RETURNS TEXT AS 'pgpro_version' LANGUAGE internal STRICT IMMUTABLE;

src/pgpro-upgrade/pgpro_version.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select count(*) > 0 as pgpro_version from pg_proc where proname = 'pgpro_version';

0 commit comments

Comments
 (0)