From c9d4987e6f164e98ba1e1264c199de2cdb0ccd01 Mon Sep 17 00:00:00 2001 From: stevensting Date: Sat, 1 Mar 2025 12:54:15 +0100 Subject: [PATCH] db:prepare is running in issues, therefore use single db commands depending on the state of the database. tested with fresh deployment and migrations --- compose.yml | 1 + entrypoint.sh | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/compose.yml b/compose.yml index 77f7ecd..ea5acff 100644 --- a/compose.yml +++ b/compose.yml @@ -97,6 +97,7 @@ services: - loomio_files:/loomio/public/files - loomio_plugins:/loomio/plugins/docker db: + # loomio version upgrades and postgres version upgrade should not be performed at the same time. image: pgautoupgrade/pgautoupgrade:17-debian networks: - backend diff --git a/entrypoint.sh b/entrypoint.sh index 173b390..d71cd7b 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -25,6 +25,7 @@ file_env "DEVISE_SECRET" file_env "SECRET_COOKIE_TOKEN" file_env "POSTGRES_PASSWORD" file_env "SMTP_PASSWORD" +export DB_HOST="db" export DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db/${POSTGRES_DB}" if [ -n "$1" ]; then @@ -36,8 +37,33 @@ else if [ "$TASK" = "worker" ]; then bundle exec sidekiq else + sudo apt update -y && sudo apt install -y postgresql-client bundle install - bundle exec rake db:prepare + + # running this code instaed of db:prepare in docker_start.sh in loomio container + # as postgres container creates empty db, somehow db:prepare cannot cope. + # therefore we run db:setup or db:migrate individually + if PGPASSWORD=$(cat /run/secrets/db_password) psql -U "$POSTGRES_USER" -h "$DB_HOST" -lqt | cut -d \| -f 1 | grep -wq "$POSTGRES_DB"; then + echo "database '$POSTGRES_DB' exists." + + # check if the database contains tables + TABLE_COUNT=$(PGPASSWORD=$(cat /run/secrets/db_password) psql -U "$POSTGRES_USER" -h "$DB_HOST" -d "$POSTGRES_DB" -t -c "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public';") + + if [ "$TABLE_COUNT" -eq 0 ]; then + echo "Database '$POSTGRES_DB' is empty, running db:setup." + bundle exec rake db:setup + #bundle exec rake db:schema:load + #bundle exec rake db:migrate + #bundle exec rake db:seed + else + echo "database '$POSTGRES_DB' not empty, running migrations." + bundle exec rake db:migrate + fi + else + echo "database '$POSTGRES_DB' does not exist, running db:setup." + bundle exec rake db:setup + fi + bundle exec puma -C config/puma.rb fi fi