From 124140f530d07a951a96be21d1a3cce406c38f0b Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Tue, 17 Mar 2015 13:19:54 +0100 Subject: postgresql-setup: better error handling Fix the bug in handling with already initialized data directory. Now, when doing --upgrade, postgresql-setup does not try to re-initialize already initialized PGDATA directory and, after "this" unsuccessful initdb, the datadir is not removed. * postgresql-setup.in (check_not_initialized): New function. (perform_initdb): Simplify return value handling. (initdb): Use check_not_initialized function. (upgrade): Use check_not_initialized to avoid PGDATA removal. When $cleanup is false, don't remove $PGDATA. --- postgresql-setup.in | 54 +++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/postgresql-setup.in b/postgresql-setup.in index 2c21349..dcc2475 100644 --- a/postgresql-setup.in +++ b/postgresql-setup.in @@ -91,6 +91,16 @@ print_version() } +check_not_initialized() +{ + if test -f "$pgdata/PG_VERSION"; then + error $"Data directory $pgdata is not empty!" + return 1 + fi + return 0 +} + + # code shared between initdb and upgrade actions perform_initdb() { @@ -138,31 +148,22 @@ perform_initdb() fi fi - if [ -f "$pgdata/PG_VERSION" ]; then - return 0 - fi - - return 1 + test -f "$pgdata/PG_VERSION" } initdb() { - if [ -f "$pgdata/PG_VERSION" ]; then - error $"Data directory $pgdata is not empty!" - script_result=1 + port_info= + test "$pgport" != "$PGPORT_DEF" \ + && port_info=$", listening on port '$pgport'" + + info $"Initializing database in '$pgdata'$port_info" + if check_not_initialized && perform_initdb; then + info $"Initialized, logs are in ${initdb_log}" else - port_info= - test "$pgport" != "$PGPORT_DEF" \ - && port_info=$", listening on port '$pgport'" - - info $"Initializing database in '$pgdata'$port_info" - if perform_initdb; then - info $"Initialized, logs are in ${initdb_log}" - else - error $"Initializing database failed, possibly see $initdb_log" - script_result=1 - fi + error $"Initializing database failed, possibly see $initdb_log" + script_result=1 fi } @@ -237,9 +238,15 @@ upgrade() pghost_override="PGHOST='$upgradefrom_pghost_override'" } - # Create empty new-format database - if perform_initdb; then - # Do the upgrade + local cleanup=true + + if ! check_not_initialized; then + # Don't try to re-init initialized data directory and also do not + # remove it after this unsuccessful upgrade. + cleanup=false + script_result=1 + elif perform_initdb; then + # After creating the empty new-format database, do the upgrade run_cmd_as_dbadmin "\ $scls_upgrade_hacks \ $socket_hacks \ @@ -260,7 +267,6 @@ upgrade() script_result=1 fi else - # initdb failed error $"initdb failed" script_result=1 fi @@ -277,7 +283,7 @@ upgrade() warn $pgdataold. else # Clean up after failure. - rm -rf "$pgdata" + $cleanup && rm -rf "$pgdata" $inplace && mv "$pgdataold" "$pgdata" error $"Upgrade failed." fi -- cgit