mirror of https://github.com/zulip/zulip.git
create-database: Hide harmless "non-existant database" warnings.
During installation on a new host, `create-database` attempts to verify that there isn't a bunch of data already in the database which is it about to drop and recreate. In the most common case, this statement emits a scary-looking warning, since the database does not exist yet: ``` + /home/zulip/deployments/current/scripts/setup/create-database + POSTGRES_USER=postgres ++ crudini --get /etc/zulip/zulip.conf postgresql database_name ++ echo zulip + DATABASE_NAME=zulip ++ crudini --get /etc/zulip/zulip.conf postgresql database_user ++ echo zulip + DATABASE_USER=zulip ++ cd / ++ su postgres -c 'psql -v ON_ERROR_STOP=1 -Atc '\''SELECT COUNT(*) FROM zulip.zerver_message;'\'' zulip' psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: database "zulip" does not exist ``` Because we are attempting to gracefully handle the case where the database does not exist yet, we also continue (and drop the database) in other, less expected cases -- for instance, if database contains a schema we do not expect. Explicitly check for the database existence first, and once we verify that, allow any further failures in the `SELECT COUNT(*)` to abort `create-database`. This serves the dual purpose of hiding the "FATAL" error for the common case when the database does not exist, as well as preventing dropping the database if anything else goes awry.
This commit is contained in:
parent
778275575e
commit
a1151aef8b
|
@ -14,20 +14,18 @@ POSTGRES_USER="${POSTGRES_USER:-postgres}"
|
||||||
DATABASE_NAME=$(crudini --get /etc/zulip/zulip.conf postgresql database_name 2>/dev/null || echo zulip)
|
DATABASE_NAME=$(crudini --get /etc/zulip/zulip.conf postgresql database_name 2>/dev/null || echo zulip)
|
||||||
DATABASE_USER=$(crudini --get /etc/zulip/zulip.conf postgresql database_user 2>/dev/null || echo zulip)
|
DATABASE_USER=$(crudini --get /etc/zulip/zulip.conf postgresql database_user 2>/dev/null || echo zulip)
|
||||||
|
|
||||||
# This psql command may fail because the Zulip database doesn’t exist,
|
if [ "$(su "$POSTGRES_USER" -c "cd / && psql -v ON_ERROR_STOP=1 -Atc \"SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME';\"")" = "1" ]; then
|
||||||
# hence the &&.
|
records="$(su "$POSTGRES_USER" -c "cd / && psql -v ON_ERROR_STOP=1 -Atc 'SELECT COUNT(*) FROM $DATABASE_NAME.zerver_message;' $DATABASE_USER")"
|
||||||
if records="$(
|
if [ "$records" -gt 200 ]; then
|
||||||
cd / # Make sure the current working directory is readable by postgres
|
set +x
|
||||||
su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -Atc 'SELECT COUNT(*) FROM $DATABASE_NAME.zerver_message;' $DATABASE_USER"
|
echo "WARNING: This will delete your Zulip database which currently contains $records messages."
|
||||||
)" && [ "$records" -gt 200 ]; then
|
read -p "Do you want to proceed? [y/N] " -r
|
||||||
set +x
|
echo
|
||||||
echo "WARNING: This will delete your Zulip database which currently contains $records messages."
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||||
read -p "Do you want to proceed? [y/N] " -r
|
exit 1
|
||||||
echo
|
fi
|
||||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
set -x
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
set -x
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Shut down all services to ensure a quiescent state.
|
# Shut down all services to ensure a quiescent state.
|
||||||
|
|
Loading…
Reference in New Issue