add support for alternate postgres ports and db names in docker

This commit is contained in:
Joey Eamigh 2024-09-17 18:53:10 -04:00
parent adf32353ab
commit 6f658562f6
No known key found for this signature in database
GPG key ID: CE8C05DFFC53C9CB
3 changed files with 25 additions and 7 deletions

View file

@ -11,13 +11,13 @@ default: &default
development:
<<: *default
database: dawarich_development
database: <%= ENV['DATABASE_NAME'] || 'dawarich_development' %>
test:
<<: *default
database: dawarich_test
database: <%= ENV['DATABASE_NAME'] || 'dawarich_test' %>
production:
<<: *default
database: dawarich_production
database: <%= ENV['DATABASE_NAME'] || 'dawarich_production' %>
url: <%= ENV['DATABASE_URL'] %>

View file

@ -7,11 +7,18 @@ set -e
echo "Environment: $RAILS_ENV"
# set env var defaults
DATABASE_HOST=${DATABASE_HOST:-"dawarich_db"}
DATABASE_PORT=${DATABASE_PORT:-5432}
DATABASE_USERNAME=${DATABASE_USERNAME:-"postgres"}
DATABASE_PASSWORD=${DATABASE_PASSWORD:-"password"}
DATABASE_NAME=${DATABASE_NAME:-"dawarich_development"}
# Remove pre-existing puma/passenger server.pid
rm -f $APP_PATH/tmp/pids/server.pid
# Wait for the database to be ready
until nc -zv $DATABASE_HOST 5432; do
until nc -zv $DATABASE_HOST ${DATABASE_PORT:-5432}; do
echo "Waiting for PostgreSQL to be ready..."
sleep 1
done
@ -21,8 +28,12 @@ gem update --system 3.5.7
gem install bundler --version '2.5.9'
# Create the database
echo "Creating database $DATABASE_NAME..."
bundle exec rails db:create
if [ "$(psql "postgres://$DATABASE_USERNAME:$DATABASE_PASSWORD@$DATABASE_HOST:$DATABASE_PORT" -XtAc "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'")" = '1' ]; then
echo "Database $DATABASE_NAME already exists, skipping creation..."
else
echo "Creating database $DATABASE_NAME..."
bundle exec rails db:create
fi
# Run database migrations
echo "PostgreSQL is ready. Running database migrations..."

View file

@ -4,8 +4,15 @@ set -e
echo "Environment: $RAILS_ENV"
# set env var defaults
DATABASE_HOST=${DATABASE_HOST:-"dawarich_db"}
DATABASE_PORT=${DATABASE_PORT:-5432}
DATABASE_USER=${DATABASE_USER:-"postgres"}
DATABASE_PASSWORD=${DATABASE_PASSWORD:-"password"}
DATABASE_NAME=${DATABASE_NAME:-"dawarich_development"}
# Wait for the database to be ready
until nc -zv $DATABASE_HOST 5432; do
until nc -zv $DATABASE_HOST ${DATABASE_PORT:-5432}; do
echo "Waiting for PostgreSQL to be ready..."
sleep 1
done