Просмотр исходного кода

add optional migration support; add engine stop/start support on upgrades

Ralf S. Engelschall 23 лет назад
Родитель
Сommit
6d954f37a0
2 измененных файлов с 256 добавлено и 2 удалено
  1. 187 0
      postgresql/pg_migrate
  2. 69 2
      postgresql/postgresql.spec

+ 187 - 0
postgresql/pg_migrate

@@ -0,0 +1,187 @@
+#!/bin/sh
+##
+##  pg_migrate -- PostgreSQL Database Migration Utility
+##  Copyright (c) 2000-2002 Cable & Wireless Deutschland GmbH
+##  Copyright (c) 2000-2002 The OpenPKG Project <http://www.openpkg.org/>
+##  Copyright (c) 2000-2002 Ralf S. Engelschall <rse@engelschall.com>
+##
+##  Permission to use, copy, modify, and distribute this software for
+##  any purpose with or without fee is hereby granted, provided that
+##  the above copyright notice and this permission notice appear in all
+##  copies.
+##
+##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
+##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+##  SUCH DAMAGE.
+##
+
+#   configuration
+l_prefix="@l_prefix@"
+l_rusr="@l_rusr@"
+l_rgrp="@l_rgrp@"
+
+#   establish sane environment
+LC_CTYPE=C
+export LC_CTYPE
+umask 022
+
+cmd="$1"
+shift
+case $cmd in
+    dump )
+        echo "++ enforcing full-superuser access policy"
+        cp -p $l_prefix/var/postgresql/db/pg_hba.conf \
+              $l_prefix/var/postgresql/db/pg_hba.conf.orig
+        ( echo "local all trust"
+          echo "host all 127.0.0.1 255.255.255.255 trust"
+        ) >$l_prefix/var/postgresql/db/pg_hba.conf
+
+        if [ ".`$l_prefix/etc/rc postgresql status 2>&1 | grep 'is running'`" != . ]; then
+            echo "++ reloading already running database engine"
+            $l_prefix/etc/rc postgresql reload
+            sleep 2
+            epilog=reload
+        else
+            echo "++ temporarily starting database engine"
+            $l_prefix/etc/rc postgresql start 
+            sleep 4
+            epilog=stop
+        fi
+
+        echo "++ rotating dump files $l_prefix/var/postgresql/db.dump*.sql.bz2"
+        i=9
+        rm -f $l_prefix/var/postgresql/db.dump.$i.sql.bz2 >/dev/null 2>&1 || true
+        while [ $i -gt 0 ]; do
+            j=$i
+            i=`expr $i - 1`
+            if [ $i -eq 0 ]; then
+                prev="$l_prefix/var/postgresql/db.dump.sql.bz2"
+                next="$l_prefix/var/postgresql/db.dump.$j.sql.bz2"
+            else
+                prev="$l_prefix/var/postgresql/db.dump.$i.sql.bz2"
+                next="$l_prefix/var/postgresql/db.dump.$j.sql.bz2"
+            fi
+            if [ -f $prev ]; then
+                mv $prev $next
+            fi
+        done
+
+        echo "++ dumping all databases into $l_prefix/var/postgresql/db.dump.sql.bz2"
+        $l_prefix/bin/pg_dumpall \
+            -U postgresql -Spostgresql -o -Xuse-set-session-authorization |\
+            $l_prefix/lib/openpkg/bzip2 -9 \
+                >$l_prefix/var/postgresql/db.dump.sql.bz2
+        chown ${l_rusr}:${l_rgrp} $l_prefix/var/postgresql/db.dump.sql.bz2
+        chmod 700 $l_prefix/var/postgresql/db.dump.sql.bz2
+
+        echo "++ restoring original access policy"
+        cp -p $l_prefix/var/postgresql/db/pg_hba.conf.orig \
+              $l_prefix/var/postgresql/db/pg_hba.conf
+        rm -f $l_prefix/var/postgresql/db/pg_hba.conf.orig
+
+        if [ ".$epilog" = .reload ]; then
+            echo "++ reloading already running database engine (again)"
+            $l_prefix/etc/rc postgresql reload
+            sleep 2
+        else
+            echo "++ stopping temporarily started database engine"
+            $l_prefix/etc/rc postgresql stop 
+            sleep 4
+        fi
+        ;;
+
+    restore )
+        if [ ".`$l_prefix/etc/rc postgresql status 2>&1 | grep 'is running'`" != . ]; then
+            echo "++ stopping already running database engine"
+            $l_prefix/etc/rc postgresql stop
+            sleep 2
+            epilog=start
+        else
+            epilog=none
+        fi
+
+        echo "++ rotating database directories $l_prefix/var/postgresql/db.old*/"
+        i=9
+        rm -rf $l_prefix/var/postgresql/db.old.$i >/dev/null 2>&1 || true
+        while [ $i -gt 0 ]; do
+            j=$i
+            i=`expr $i - 1`
+            if [ $i -eq 0 ]; then
+                prev="$l_prefix/var/postgresql/db"
+                next="$l_prefix/var/postgresql/db.old.$j"
+            else
+                prev="$l_prefix/var/postgresql/db.old.$i"
+                next="$l_prefix/var/postgresql/db.old.$j"
+            fi
+            if [ -d $prev ]; then
+                mv $prev $next
+            fi
+        done
+
+        echo "++ creating new database directory $l_prefix/var/postgresql/db/"
+        mkdir $l_prefix/var/postgresql/db
+        chown ${l_rusr}:${l_rgrp} $l_prefix/var/postgresql/db
+        chmod 700 $l_prefix/var/postgresql/db
+
+        su - ${l_rusr} -c \
+            "LC_CTYPE=C; export LC_CTYPE; umask 022; \
+            (echo 'postgresql'; echo 'postgresql') |\
+            $l_prefix/bin/pg_initdb -U postgresql -W -D $l_prefix/var/postgresql/db" 2>&1 |\
+                $l_prefix/lib/openpkg/shtool prop \
+                    -p "++ creating new database data"
+
+        echo "++ restoring database configurations"
+        for conf in pg_hba.conf pg_ident.conf postgresql.conf; do
+            cp -p $l_prefix/var/postgresql/db.old.1/$conf \
+                  $l_prefix/var/postgresql/db/
+        done
+
+        echo "++ enforcing full-superuser access policy"
+        cp -p $l_prefix/var/postgresql/db/pg_hba.conf \
+              $l_prefix/var/postgresql/db/pg_hba.conf.orig
+        ( echo "local all trust"
+          echo "host all 127.0.0.1 255.255.255.255 trust"
+        ) >$l_prefix/var/postgresql/db/pg_hba.conf
+
+        if [ ".$epilog" = .start ]; then
+            echo "++ starting database engine"
+        else
+            echo "++ temporarily starting database engine"
+        fi
+        $l_prefix/etc/rc postgresql start
+        sleep 4
+
+        echo "++ restoring all databases from $l_prefix/var/postgresql/db.dump.sql.bz2"
+        $l_prefix/lib/openpkg/bzip2 -c -d \
+            $l_prefix/var/postgresql/db.dump.sql.bz2 |\
+                $l_prefix/bin/psql -U postgresql -d template1 2>&1 |\
+                    tee $l_prefix/var/postgresql/db.log |\
+                        $l_prefix/lib/openpkg/shtool prop \
+                            -p "++ restoring data (see $l_prefix/var/postgresql/db.log)"
+
+        echo "++ restoring original access policy"
+        cp -p $l_prefix/var/postgresql/db/pg_hba.conf.orig \
+              $l_prefix/var/postgresql/db/pg_hba.conf
+        rm -f $l_prefix/var/postgresql/db/pg_hba.conf.orig
+
+        if [ ".$epilog" = .start ]; then
+            echo "++ reloading already running database engine"
+            $l_prefix/etc/rc postgresql reload
+            sleep 2
+        else
+            echo "++ stopping temporarily started database engine"
+            $l_prefix/etc/rc postgresql stop 
+            sleep 4
+        fi
+        ;;
+esac
+

+ 69 - 2
postgresql/postgresql.spec

@@ -47,11 +47,12 @@ Distribution: OpenPKG [BASE]
 Group:        Database
 License:      GPL
 Version:      7.3
-Release:      20021128
+Release:      20021203
 
 #   list of sources
 Source0:      ftp://ftp.postgresql.org/pub/source/v%{version}/postgresql-%{version}.tar.gz
 Source1:      rc.postgresql
+Source2:      pg_migrate
 
 #   build information
 Prefix:       %{l_prefix}
@@ -169,6 +170,14 @@ AutoReqProv:  no
         $RPM_BUILD_ROOT%{l_prefix}/var/postgresql/db \
         $RPM_BUILD_ROOT%{l_prefix}/var/postgresql/run
 
+    #   install migration utility
+    %{l_shtool} install -c -m 755 \
+        -e 's;@l_prefix@;%{l_prefix};g' \
+        -e 's;@l_rusr@;%{l_rusr};g' \
+        -e 's;@l_rgrp@;%{l_rgrp};g' \
+        %{SOURCE pg_migrate} \
+        $RPM_BUILD_ROOT%{l_prefix}/bin/
+
     #   install run-command script
     %{l_shtool} mkdir -f -p -m 755 \
         $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
@@ -190,8 +199,44 @@ AutoReqProv:  no
 %clean
     rm -rf $RPM_BUILD_ROOT
 
+%pre
+    if [ $1 -gt 1 ]; then
+        #   initial installation
+        if [ -f $RPM_INSTALL_PREFIX/var/postgresql/db/PG_VERSION -a \
+             -f $RPM_INSTALL_PREFIX/bin/pg_migrate ]; then
+            #   database migration dumping hint
+            v_old_all=`cat $RPM_INSTALL_PREFIX/var/postgresql/db/PG_VERSION`
+            v_old_maj=`echo "$v_old_all" | sed -e 's/^\([0-9]*\.[0-9]*\).*/\1/'`
+            v_new_all="%{version}"
+            v_new_maj=`echo "$v_new_maj" | sed -e 's/^\([0-9]*\.[0-9]*\).*/\1/'`
+            if [ ".$v_old_maj" != ".$v_new_maj" ]; then
+               if [ ! -f $RPM_INSTALL_PREFIX/var/postgresql/db.dump.sql.bz2 -a ".$PG_MIGRATE" != .ignore ]; then
+                    ( echo "You are upgrading from PostgreSQL $v_old_all to PostgresSQL $v_new_all,"
+                      echo "which is a major version change. We expect a database incompatibility,"
+                      echo "so we strongly recommend that you backup your existing database"
+                      echo "($RPM_INSTALL_PREFIX/var/postgresql/db/) first by running:"
+                      echo "    \$ $RPM_INSTALL_PREFIX/bin/pg_migrate dump" 
+                      echo "Alternatively, if you want force this package to be installed without"
+                      echo "performing a database dump, run the following command before upgrading:"
+                      echo "    \$ PG_MIGRATE=ignore; export PG_MIGRATE"
+                    ) | %{l_rpmtool} msg -b -t error
+                    exit 1
+                fi
+            fi
+        fi
+    elif [ $1 -gt 1 ]; then
+        #   upgrading of installation
+        rm -f $RPM_INSTALL_PREFIX/var/posgresql/RESTART >/dev/null 2>&1 || true
+        if [ ".`$l_prefix/etc/rc postgresql status 2>&1 | grep 'is running'`" != . ]; then
+            echo "Shutting down currently running database engine." | %{l_rpmtool} msg -b -t notice
+            $RPM_INSTALL_PREFIX/etc/rc postgresql stop 
+            touch $RPM_INSTALL_PREFIX/var/posgresql/RESTART
+            sleep 4
+        fi
+    fi
+
 %post
-    if [ ".$1" = .1 ]; then
+    if [ $1 -eq 1 ]; then
         #   create initial database
         su - %{l_rusr} -c \
             "LC_CTYPE=C; export LC_CTYPE; umask 022; \
@@ -233,5 +278,27 @@ AutoReqProv:  no
           echo "Threading       = 2"
         ) $RPM_INSTALL_PREFIX/bin/odbcinst -i -d -r
 %endif
+    elif [ $1 -gt 1 ]; then
+        if [ -f $RPM_INSTALL_PREFIX/var/posgresql/RESTART ]; then
+            echo "Starting database engine again." | %{l_rpmtool} msg -b -t notice
+            $RPM_INSTALL_PREFIX/etc/rc postgresql start 
+            rm -f $RPM_INSTALL_PREFIX/var/posgresql/RESTART >/dev/null 2>&1 || true
+            sleep 2
+        fi
+        if [ -f $RPM_INSTALL_PREFIX/var/postgresql/db/PG_VERSION -a ".$PG_MIGRATE" != .ignore ]; then
+            #   database migration restoring hint
+            v_old_all=`cat $RPM_INSTALL_PREFIX/var/postgresql/db/PG_VERSION`
+            v_old_maj=`echo "$v_old_all" | sed -e 's/^\([0-9]*\.[0-9]*\).*/\1/'`
+            v_new_all="%{version}"
+            v_new_maj=`echo "$v_new_maj" | sed -e 's/^\([0-9]*\.[0-9]*\).*/\1/'`
+            if [ ".$v_old_maj" != ".$v_new_maj" ]; then
+                ( echo "You upgraded from PostgreSQL $v_old_all to PostgresSQL $v_new_all,"
+                  echo "which is a major version upgrade. We expect a database incompatibility,"
+                  echo "so we strongly recommend you to recreate the existing database under"
+                  echo "$RPM_INSTALL_PREFIX/var/postgresql/db/ by running the following command:"
+                  echo "    \$ $RPM_INSTALL_PREFIX/bin/pg_dump restore"
+                ) | %{l_rpmtool} msg -b -t warn
+            fi
+        fi
     fi