Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve database import #1557

Merged
merged 2 commits into from
Dec 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Containers/postgresql/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ if ! [ -w "$DUMP_DIR" ]; then
exit 1
fi

# Don't start if import failed
if [ -f "$DUMP_DIR/import.failed" ]; then
echo "The database import failed. Please restore a backup and try again."
echo "For further clues on what went wrong, look at the logs above."
exit 1
fi

# Delete the datadir once (needed for setting the correct credentials on old instances once)
if ! [ -f "$DUMP_DIR/export.failed" ] && ! [ -f "$DUMP_DIR/initial-cleanup-done" ]; then
set -ex
Expand All @@ -45,9 +52,16 @@ if ( [ -f "$DATADIR/PG_VERSION" ] && [ "$PG_MAJOR" != "$(cat "$DATADIR/PG_VERSIO
exit 1
fi

# Write output to logfile.
exec > >(tee -i "$DUMP_DIR/database-import.log")
exec 2>&1

# Inform
echo "Restoring from database dump."

# Add import.failed file
touch "$DUMP_DIR/import.failed"

# Exit if any command fails
set -ex

Expand Down Expand Up @@ -76,7 +90,12 @@ if ( [ -f "$DATADIR/PG_VERSION" ] && [ "$PG_MAJOR" != "$(cat "$DATADIR/PG_VERSIO

# Get the Owner
DB_OWNER="$(grep "$GREP_STRING" "$DUMP_FILE" | grep -oP 'Owner:.*$' | sed 's|Owner:||;s| ||g')"
if [ "$DB_OWNER" != "oc_$POSTGRES_USER" ]; then
if [ "$DB_OWNER" = "$POSTGRES_USER" ]; then
DIFFERENT_DB_OWNER=1
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
ALTER DATABASE "$POSTGRES_DB" OWNER TO "$POSTGRES_USER";
EOSQL
elif [ "$DB_OWNER" != "oc_$POSTGRES_USER" ]; then
DIFFERENT_DB_OWNER=1
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER "$DB_OWNER" WITH PASSWORD '$POSTGRES_PASSWORD' CREATEDB;
Expand Down Expand Up @@ -104,6 +123,9 @@ EOSQL

# Don't exit if command fails anymore
set +ex

# Remove import failed file if everything went correctly
rm "$DUMP_DIR/import.failed"
fi

# Cover the last case
Expand Down