#!/bin/bash # Initialize the Silo database # This script waits for PostgreSQL to be ready and runs migrations set -e # Configuration DB_HOST="${SILO_DB_HOST:-localhost}" DB_PORT="${SILO_DB_PORT:-5432}" DB_NAME="${SILO_DB_NAME:-silo}" DB_USER="${SILO_DB_USER:-silo}" DB_PASSWORD="${SILO_DB_PASSWORD:-silodev}" # Wait for PostgreSQL to be ready echo "Waiting for PostgreSQL at $DB_HOST:$DB_PORT..." until PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c '\q' 2>/dev/null; do echo "PostgreSQL is unavailable - sleeping" sleep 2 done echo "PostgreSQL is ready!" # Run migrations SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" MIGRATIONS_DIR="${SCRIPT_DIR}/../migrations" echo "Running migrations from $MIGRATIONS_DIR..." for migration in "$MIGRATIONS_DIR"/*.sql; do if [ -f "$migration" ]; then echo "Applying $(basename "$migration")..." PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$migration" fi done echo "Database initialization complete!"