From 7d48f8b9cc56010b0ba677d37144220060c1f874 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Sat, 15 Feb 2025 11:13:56 +0100 Subject: [PATCH] test --- postgres/config.json | 2 +- postgres/rootfs/etc/cont-init.d/99-run.sh | 119 +++++++++------------- 2 files changed, 49 insertions(+), 72 deletions(-) diff --git a/postgres/config.json b/postgres/config.json index 5448079eb..60acfdb31 100644 --- a/postgres/config.json +++ b/postgres/config.json @@ -38,5 +38,5 @@ "slug": "postgres", "udev": true, "url": "https://github.com/alexbelgium/hassio-addons/tree/master/postgres", - "version": "15.7-21" + "version": "15.7-22" } diff --git a/postgres/rootfs/etc/cont-init.d/99-run.sh b/postgres/rootfs/etc/cont-init.d/99-run.sh index 41ad83a34..024439298 100755 --- a/postgres/rootfs/etc/cont-init.d/99-run.sh +++ b/postgres/rootfs/etc/cont-init.d/99-run.sh @@ -11,27 +11,8 @@ PGDATA="${PGDATA:-/config/database}" PG_VERSION_FILE="$PGDATA/pg_major_version" VECTOR_VERSION_FILE="$PGDATA/pgvector_version" -# Define current versions +# Define current PostgreSQL major version PG_MAJOR_VERSION="${PG_MAJOR:-15}" -if [ ! -f "$PG_VERSION_FILE" ]; then - echo "$PG_MAJOR_VERSION" > "$PG_VERSION_FILE" -fi - -# Create necessary directories -mkdir -p "$CONFIG_HOME" -if [ ! -f "$CONFIG_HOME/postgresql.conf" ]; then - if [ -f /usr/local/share/postgresql/postgresql.conf.sample ]; then - cp /usr/local/share/postgresql/postgresql.conf.sample "$CONFIG_HOME/postgresql.conf" - elif [ -f /usr/share/postgresql/postgresql.conf.sample ]; then - cp /usr/share/postgresql/postgresql.conf.sample "$CONFIG_HOME/postgresql.conf" - else - bashio::exit.nok "Config file not found, please ask maintainer" - exit 1 - fi - bashio::log.warning "A default config.env file was copied in $CONFIG_HOME. Please customize according to https://hub.docker.com/_/postgres and restart the add-on" -else - bashio::log.warning "Using existing config.env file in $CONFIG_HOME." -fi # Setup data directory mkdir -p "$PGDATA" @@ -41,6 +22,21 @@ chmod 700 "$PGDATA" # Set permissions chmod -R 755 "$CONFIG_HOME" +# Ensure PostgreSQL config file exists +if [ ! -f "$CONFIG_HOME/postgresql.conf" ]; then + if [ -f /usr/local/share/postgresql/postgresql.conf.sample ]; then + cp /usr/local/share/postgresql/postgresql.conf.sample "$CONFIG_HOME/postgresql.conf" + elif [ -f /usr/share/postgresql/postgresql.conf.sample ]; then + cp /usr/share/postgresql/postgresql.conf.sample "$CONFIG_HOME/postgresql.conf" + else + bashio::exit.nok "Config file not found, please ask maintainer" + exit 1 + fi + bashio::log.warning "A default postgresql.conf file was copied to $CONFIG_HOME. Please customize it and restart the add-on." +else + bashio::log.info "Using existing postgresql.conf file in $CONFIG_HOME." +fi + ############################### # Launch PostgreSQL # ############################### @@ -49,7 +45,6 @@ cd /config || true bashio::log.info "Starting PostgreSQL..." -# Start background tasks if [ "$(bashio::info.arch)" = "armv7" ]; then bashio::log.warning "ARMv7 detected: Starting without vectors.so" docker-entrypoint.sh postgres & true @@ -58,25 +53,23 @@ else docker-entrypoint.sh postgres -c shared_preload_libraries=vectors.so -c search_path="public, vectors" & true fi -# If not armv7 -docker-entrypoint.sh postgres -c shared_preload_libraries=vectors.so -c search_path="public, vectors" & true - ############################### # Wait for PostgreSQL Startup # ############################### -( bashio::log.info "Waiting for PostgreSQL to start..." -bashio::net.wait_for 5432 localhost 900 +bashio::log.info "Waiting for PostgreSQL to start..." +( bashio::net.wait_for 5432 localhost 900 ############################### # PostgreSQL Major Version Upgrade Check # ############################### -OLD_PG_VERSION=$(cat "$PG_VERSION_FILE") +# Read the previous PostgreSQL version from file +OLD_PG_VERSION=$(cat "$PG_VERSION_FILE" 2>/dev/null || echo "") + if [ "$OLD_PG_VERSION" != "$PG_MAJOR_VERSION" ]; then bashio::log.warning "PostgreSQL major version changed ($OLD_PG_VERSION → $PG_MAJOR_VERSION). Running upgrade..." - # Note: Ensure pg_upgrade is installed and the paths below match your environment. pg_upgrade --old-datadir="$PGDATA" \ --new-datadir="$PGDATA-new" \ --old-bindir="/usr/lib/postgresql/$OLD_PG_VERSION/bin" \ @@ -87,7 +80,7 @@ if [ "$OLD_PG_VERSION" != "$PG_MAJOR_VERSION" ]; then mv "$PGDATA-new" "$PGDATA" rm -rf "$PGDATA-old" - # Update the version file + # Store the new PostgreSQL version echo "$PG_MAJOR_VERSION" > "$PG_VERSION_FILE" fi @@ -95,7 +88,7 @@ fi # Enable & Upgrade pgvector.rs # ##################################### -# Set connection variables for Postgres +# Set PostgreSQL connection variables DB_PORT=5432 DB_HOSTNAME=localhost DB_PASSWORD="$(bashio::config 'POSTGRES_PASSWORD')" @@ -105,7 +98,7 @@ if bashio::config.has_value "POSTGRES_USER"; then fi export DB_PORT DB_HOSTNAME DB_USERNAME DB_PASSWORD -# Function: Check if the "vectors" extension is enabled +# Function: Check if 'vectors' extension is enabled check_vector_extension() { bashio::log.info "Checking if 'vectors' extension is enabled..." local result @@ -120,18 +113,13 @@ check_vector_extension() { fi } -# Function: Enable (or re-create) the "vectors" extension +# Function: Enable (or re-create) 'vectors' extension enable_vector_extension() { bashio::log.info "Enabling 'vectors' extension..." - cat < /setup_postgres.sql -DROP EXTENSION IF EXISTS vectors; -CREATE EXTENSION vectors; -EOF - psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" < /setup_postgres.sql >/dev/null 2>&1 - rm /setup_postgres.sql || true + psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" -c "DROP EXTENSION IF EXISTS vectors; CREATE EXTENSION vectors;" >/dev/null 2>&1 } -# Function: Store the current pgvector.rs extension version in a file +# Function: Store the current pgvector.rs version in a file store_vector_version() { local version version=$(psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \ @@ -139,54 +127,44 @@ store_vector_version() { echo "$version" > "$VECTOR_VERSION_FILE" } -# Function: Cleanup outdated vector indexes (adjust as needed) -cleanup_vector_indexes() { - bashio::log.info "Cleaning up outdated vector indexes..." - psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \ - -c "DROP INDEX IF EXISTS clip_index;" >/dev/null 2>&1 -} - -# Function: Upgrade the "vectors" extension if a desired version is provided +# Function: Detect previous and new pgvector.rs versions, and upgrade if needed upgrade_vector_extension() { local current_version desired_version - current_version=$(psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \ - -tAc "SELECT extversion FROM pg_extension WHERE extname = 'vectors';") - # Use the desired version from config; if not set, use current version - desired_version=$(bashio::config 'VECTOR_EXTENSION_VERSION' || echo "$current_version") + current_version=$(cat "$VECTOR_VERSION_FILE" 2>/dev/null || echo "unknown") + desired_version=$(psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \ + -tAc "SELECT extversion FROM pg_extension WHERE extname = 'vectors';") if [[ "$current_version" != "$desired_version" ]]; then - bashio::log.warning "Upgrading 'vectors' extension from version $current_version to $desired_version..." + bashio::log.warning "Upgrading 'vectors' extension from version $current_version → $desired_version..." + psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" -c "ALTER EXTENSION vectors UPDATE;" >/dev/null 2>&1 + + # Cleanup outdated indexes + bashio::log.info "Cleaning up outdated vector indexes..." psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \ - -c "ALTER EXTENSION vectors UPDATE TO '$desired_version';" >/dev/null 2>&1 - cleanup_vector_indexes + -c "DROP INDEX IF EXISTS clip_index;" >/dev/null 2>&1 + + # Store new pgvector version echo "$desired_version" > "$VECTOR_VERSION_FILE" else - bashio::log.info "'vectors' extension is already at the desired version ($desired_version)." + bashio::log.info "'vectors' extension is already at the latest version ($desired_version)." fi } -# Function: Troubleshooting routine if extension checks fail +# Function: Troubleshoot vector extension troubleshoot_vector_extension() { - bashio::log.error "Troubleshooting pgvector.rs installation:" + bashio::log.error "Troubleshooting pgvector.rs installation..." - bashio::log.info "Checking if PostgreSQL is running..." if ! pg_isready -h "$DB_HOSTNAME" -p "$DB_PORT" -U "$DB_USERNAME" >/dev/null 2>&1; then - bashio::log.error "PostgreSQL is not running or unreachable. Start the database and check network settings." + bashio::log.error "PostgreSQL is not running or unreachable." exit 1 - else - bashio::log.info "PostgreSQL is running." fi - bashio::log.info "Verifying availability of the 'vectors' extension..." local ext_check ext_check=$(psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT" \ -tAc "SELECT count(*) FROM pg_available_extensions WHERE name = 'vectors';") if [[ "$ext_check" -eq 0 ]]; then - bashio::log.error "'vectors' extension is not available. Ensure that the pgvector.rs extension is installed." - bashio::log.error "Try running: ALTER SYSTEM SET shared_preload_libraries = 'vectors'; then restart PostgreSQL." + bashio::log.error "'vectors' extension is missing. Ensure pgvector.rs is installed." exit 1 - else - bashio::log.info "'vectors' extension is available." fi } @@ -194,18 +172,17 @@ troubleshoot_vector_extension() { # Main Extension Handling # ################################### -# If the extension isn’t enabled, create it. if ! check_vector_extension; then enable_vector_extension fi -# Store the initial pgvector.rs version +# Store previous vector version store_vector_version -# If a desired version is provided, attempt to upgrade. +# Upgrade vector extension if needed upgrade_vector_extension -# Verify that the extension is enabled (and upgraded, if needed). +# Final verification check_vector_extension || troubleshoot_vector_extension -echo "All initialization steps done" ) & true +bashio::log.info "All initialization steps completed successfully!" ) & true