From eb1c1d4165004b47c19531c628bf0c44b3e34a4c Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Fri, 28 Mar 2025 15:17:11 -0500 Subject: [PATCH] Fixes db migration. --- .gitignore | 9 ++++++++- docker/entrypoint.sh | 1 + docker/init/02-postgres.sh | 39 ++++++++++++++++++++++++++++---------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index b6631ac0..1dd591d4 100755 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store **/__pycache__/ **/.vscode/ +**/venv *.pyc node_modules/ .history/ @@ -10,4 +11,10 @@ docker/Dockerfile DEV static/ data/ .next -next-env.d.ts \ No newline at end of file +next-env.d.ts +media/ +celerybeat-schedule* +dump.rdb +debugpy* +uwsgi.sock +package-lock.json \ No newline at end of file diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 4fe82ae0..2e34e3c1 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -54,6 +54,7 @@ if [[ ! -f /etc/profile.d/dispatcharr.sh ]]; then echo "export DISPATCHARR_ENV=$DISPATCHARR_ENV" >> /etc/profile.d/dispatcharr.sh echo "export REDIS_HOST=$REDIS_HOST" >> /etc/profile.d/dispatcharr.sh echo "export REDIS_DB=$REDIS_DB" >> /etc/profile.d/dispatcharr.sh + echo "export POSTGRES_DIR=$POSTGRES_DIR" >> /etc/profile.d/dispatcharr.sh fi chmod +x /etc/profile.d/dispatcharr.sh diff --git a/docker/init/02-postgres.sh b/docker/init/02-postgres.sh index 64662865..77684501 100644 --- a/docker/init/02-postgres.sh +++ b/docker/init/02-postgres.sh @@ -3,14 +3,33 @@ # Temporary migration from postgres in /data to /data/db. Can likely remove # some time in the future. if [ -e "/data/postgresql.conf" ]; then - mv /data /db - mkdir /data - mv /db /data/ + echo "Migrating PostgreSQL data from /data to /data/db..." + + # Create a temporary directory outside of /data + mkdir -p /tmp/postgres_migration + + # Move the PostgreSQL files to the temporary directory + mv /data/* /tmp/postgres_migration/ + + # Create the target directory + mkdir -p /data/db + + # Move the files from temporary directory to the final location + mv /tmp/postgres_migration/* /data/db/ + + # Clean up the temporary directory + rmdir /tmp/postgres_migration + + # Set proper ownership and permissions for PostgreSQL data directory + chown -R postgres:postgres /data/db + chmod 700 /data/db + + echo "Migration completed successfully." fi -# Inwitialize PostgreSQL database +# Initialize PostgreSQL database if [ -z "$(ls -A $POSTGRES_DIR)" ]; then - echo_with_timestamp "Initializing PostgreSQL database..." + echo "Initializing PostgreSQL database..." mkdir -p $POSTGRES_DIR chown -R postgres:postgres $POSTGRES_DIR chmod 700 $POSTGRES_DIR @@ -26,7 +45,7 @@ if [ -z "$(ls -A $POSTGRES_DIR)" ]; then su - postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D ${POSTGRES_DIR} start -w -t 300 -o '-c port=${POSTGRES_PORT}'" # Wait for PostgreSQL to be ready until su - postgres -c "/usr/lib/postgresql/14/bin/pg_isready -h ${POSTGRES_HOST} -p ${POSTGRES_PORT}" >/dev/null 2>&1; do - echo_with_timestamp "Waiting for PostgreSQL to be ready..." + echo "Waiting for PostgreSQL to be ready..." sleep 1 done @@ -35,11 +54,11 @@ if [ -z "$(ls -A $POSTGRES_DIR)" ]; then # Setup database if needed if ! su - postgres -c "psql -p ${POSTGRES_PORT} -tAc \"SELECT 1 FROM pg_database WHERE datname = '$POSTGRES_DB';\"" | grep -q 1; then # Create PostgreSQL database - echo_with_timestamp "Creating PostgreSQL database..." + echo "Creating PostgreSQL database..." su - postgres -c "createdb -p ${POSTGRES_PORT} ${POSTGRES_DB}" # Create user, set ownership, and grant privileges - echo_with_timestamp "Creating PostgreSQL user..." + echo "Creating PostgreSQL user..." su - postgres -c "psql -p ${POSTGRES_PORT} -d ${POSTGRES_DB}" <