This PR fixes the database name check to ignore query params. This is useful for situations where you need to force sslmode, like ?sslmode=require. Yarn seems to handle this, but this db creation check fails. My environment enforces ssl for all PG connections, so I need twenty to handle this check for me to test it locally.
51 lines
1.9 KiB
Bash
Executable File
51 lines
1.9 KiB
Bash
Executable File
#!/bin/sh
|
|
set -e
|
|
|
|
setup_and_migrate_db() {
|
|
if [ "${DISABLE_DB_MIGRATIONS}" = "true" ]; then
|
|
echo "Database setup and migrations are disabled, skipping..."
|
|
return
|
|
fi
|
|
|
|
echo "Running database setup and migrations..."
|
|
PGUSER=$(echo $PG_DATABASE_URL | awk -F '//' '{print $2}' | awk -F ':' '{print $1}')
|
|
PGPASS=$(echo $PG_DATABASE_URL | awk -F ':' '{print $3}' | awk -F '@' '{print $1}')
|
|
PGHOST=$(echo $PG_DATABASE_URL | awk -F '@' '{print $2}' | awk -F ':' '{print $1}')
|
|
PGPORT=$(echo $PG_DATABASE_URL | awk -F ':' '{print $4}' | awk -F '/' '{print $1}')
|
|
PGDATABASE=$(echo $PG_DATABASE_URL | awk -F '/' '{print $NF}' | cut -d'?' -f1)
|
|
|
|
# Creating the database if it doesn't exist
|
|
db_count=$(PGPASSWORD=${PGPASS} psql -h ${PGHOST} -p ${PGPORT} -U ${PGUSER} -d postgres -tAc "SELECT COUNT(*) FROM pg_database WHERE datname = '${PGDATABASE}'")
|
|
if [ "$db_count" = "0" ]; then
|
|
echo "Database ${PGDATABASE} does not exist, creating..."
|
|
PGPASSWORD=${PGPASS} psql -h ${PGHOST} -p ${PGPORT} -U ${PGUSER} -d postgres -c "CREATE DATABASE \"${PGDATABASE}\""
|
|
|
|
# Run setup and migration scripts
|
|
NODE_OPTIONS="--max-old-space-size=1500" tsx ./scripts/setup-db.ts
|
|
yarn database:migrate:prod
|
|
fi
|
|
|
|
yarn command:prod upgrade
|
|
echo "Successfully migrated DB!"
|
|
}
|
|
|
|
register_background_jobs() {
|
|
if [ "${DISABLE_CRON_JOBS_REGISTRATION}" = "true" ]; then
|
|
echo "Cron job registration is disabled, skipping..."
|
|
return
|
|
fi
|
|
|
|
echo "Registering background sync jobs..."
|
|
if yarn command:prod cron:register:all; then
|
|
echo "Successfully registered all background sync jobs!"
|
|
else
|
|
echo "Warning: Failed to register background jobs, but continuing startup..."
|
|
fi
|
|
}
|
|
|
|
setup_and_migrate_db
|
|
register_background_jobs
|
|
|
|
# Continue with the original Docker command
|
|
exec "$@"
|