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

feat: Support all postgres version + use server postgres version in client tools #2

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

CuriousLearner
Copy link

Fixes #1

@CuriousLearner CuriousLearner requested a review from ipmb January 5, 2025 02:10
set -euf -o pipefail

cleanup() { rv=$?; if [ -f /tmp/db.dump ]; then shred -u /tmp/db.dump; fi; exit $rv; }
trap cleanup EXIT

NAME=${2:-$NAME}
CONNECT_DB_URL="postgres://$USER@$HOST:$PORT/$NAME"
# Extract database name from DATABASE_URL if not provided as an argument
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this change needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that having one variable as opposed to several ones will be easier. Plus, we try to extract the database name directly from the URL if it's not passed as an argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not following. Where are we using several variables?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ipmb: The idea behind this change was to simplify things by combining multiple variables (like USER, HOST, PORT, etc.) into one CONNECT_DB_URL. This makes the code easier to manage and avoids dealing with too many individual variables.

The part where we extract the database name from DATABASE_URL is just a fallback in case it's not passed explicitly, so it ensures everything still works without needing extra inputs.

If something's unclear or there’s a better way to handle this, happy to discuss!

@@ -1,9 +1,32 @@
FROM --platform=linux/amd64 python:3.12-alpine3.19
FROM ubuntu:22.04
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the latest debian slim


if ! command -v "$PG_DUMP" &>/dev/null; then
echo "Error: pg_dump for version $SERVER_VERSION is not installed." >&2
exit 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than failing hard here, let's make this a warning and default to the latest version

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

CONNECT_DB_URL="${DATABASE_URL%/*}/$DBNAME"

# Get PostgreSQL server version
SERVER_VERSION=$(psql "$CONNECT_DB_URL" -tAc "SHOW server_version;" | cut -d '.' -f 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to run this in the entrypoint?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I moved this to the entrypoint.sh script.

echo " PORT=$PORT"
echo " NAME=$NAME"
echo " USER=$USER"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to keep this DEBUG output in the final PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this is still in progress, because CI was failing. I'll fix this up.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fixed now.

set -euf -o pipefail

cleanup() { rv=$?; if [ -f /tmp/db.dump ]; then shred -u /tmp/db.dump; fi; exit $rv; }
trap cleanup EXIT

NAME=${2:-$NAME}
CONNECT_DB_URL="postgres://$USER@$HOST:$PORT/$NAME"
# Extract database name from DATABASE_URL if not provided as an argument
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not following. Where are we using several variables?

@CuriousLearner CuriousLearner requested a review from ipmb January 27, 2025 22:46
Copy link
Member

@ipmb ipmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Left a few minor comments. Can you also add docs on how to update when new PG versions are released?

ln -s /usr/lib/postgresql/$version/bin/pg_dump /usr/bin/pg_dump-$version && \
ln -s /usr/lib/postgresql/$version/bin/pg_restore /usr/bin/pg_restore-$version; \
done && \
# Use a virtual environment to install Python packages
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the virtualenv here. You can install on the system. There may be an apt installable version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there was an issue when I was trying to install it system-wide (but I might be wrong since it has been some time I did this). I'll give it another try.

CONNECT_DB_URL="${DATABASE_URL%/*}/$DBNAME"

# Use SERVER_VERSION set by the entrypoint
PG_DUMP="pg_dump-$SERVER_VERSION"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this work if $SERVER_VERSION is not supplied? It can default to the latest version.

@CuriousLearner CuriousLearner requested a review from ipmb January 27, 2025 23:37
@CuriousLearner
Copy link
Author

CuriousLearner commented Jan 27, 2025

@ipmb Added the docs here: postgres/docs/support-newer-postgres-versions.md


echo "Waiting for PostgreSQL server to be ready..."
until psql "$DATABASE_URL" -c '\q' 2>/dev/null || [ "$retries" -eq 0 ]; do
echo "PostgreSQL is unavailable - sleeping ($((retries--)) retries left)..."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove " sleeping " please

done

if [ "$retries" -eq 0 ]; then
echo "ERROR: PostgreSQL server did not become ready in time."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERROR: PostgreSQL server did not respond.

exit 1
fi

echo "PostgreSQL server is ready!"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this one please

and running the script against a PostgreSQL 18 server.
3. Ensure fallback compatibility:
Test the script with no `SERVER_VERSION` set to confirm it falls back to the latest version.
4. Update `dump-to-s3.sh` script's `SERVER_VERSION` default value if the new version becomes the default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's referenced in load-to-s3.sh too

Verify that the new version works by setting `SERVER_VERSION` to the new version (e.g., `18`)
and running the script against a PostgreSQL 18 server.
3. Ensure fallback compatibility:
Test the script with no `SERVER_VERSION` set to confirm it falls back to the latest version.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the automated tests do 2 & 3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

postgres tools should match server version
2 participants