Merge pull request #1103 from Freika/fix/auto-db-creation

Fix database creation on startup
This commit is contained in:
Evgenii Burmakin 2025-04-24 20:51:15 +02:00 committed by GitHub
commit 8a33c23bec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 10 deletions

View file

@ -1 +1 @@
0.25.7
0.25.8

View file

@ -5,6 +5,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
# 0.25.8 - 2025-04-24
## Fixed
- Database was not being created if it didn't exist. #1076
## Removed
- `RAILS_MASTER_KEY` environment variable is no longer being set. You can safely remove it from your environment variables.
# 0.25.7 - 2025-04-24
## Fixed

View file

@ -70,7 +70,6 @@ services:
PROMETHEUS_EXPORTER_HOST: 0.0.0.0
PROMETHEUS_EXPORTER_PORT: 9394
SELF_HOSTED: "true"
RAILS_MASTER_KEY: ${RAILS_MASTER_KEY}
logging:
driver: "json-file"
options:
@ -123,7 +122,6 @@ services:
PROMETHEUS_EXPORTER_HOST: dawarich_app
PROMETHEUS_EXPORTER_PORT: 9394
SELF_HOSTED: "true"
RAILS_MASTER_KEY: ${RAILS_MASTER_KEY}
logging:
driver: "json-file"
options:

View file

@ -27,20 +27,17 @@ fi
# Remove pre-existing puma/passenger server.pid
rm -f $APP_PATH/tmp/pids/server.pid
echo "Attempting to create database $DATABASE_NAME if it doesn't exist..."
PGPASSWORD=$DATABASE_PASSWORD createdb -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" "$DATABASE_NAME" 2>/dev/null || echo "Note: Database may already exist or couldn't be created now"
# Wait for the database to become available
echo "⏳ Waiting for database to be ready..."
until PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" -d "$DATABASE_NAME" -c '\q'; do
until PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" -d "$DATABASE_NAME" -c '\q' 2>/dev/null; do
>&2 echo "Postgres is unavailable - retrying..."
sleep 2
done
echo "✅ PostgreSQL is ready!"
# Create database if it doesn't exist
if ! PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" -d "$DATABASE_NAME" -c "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'" | grep -q 1; then
echo "Creating database $DATABASE_NAME..."
PGPASSWORD=$DATABASE_PASSWORD createdb -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" "$DATABASE_NAME"
fi
# Run database migrations
echo "PostgreSQL is ready. Running database migrations..."
bundle exec rails db:migrate