diff --git a/scripts/init_database/00_init_user.sh b/scripts/init_database/00_init_user.sh new file mode 100755 index 0000000..8aff661 --- /dev/null +++ b/scripts/init_database/00_init_user.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# Initialize database user and database +# This script runs before SQL files in docker-entrypoint-initdb.d +# It must be named with 00_ prefix to run first + +set -e + +APP_DB_NAME="${APP_DB_NAME:-ai_conversations}" +APP_DB_USER="${APP_DB_USER:-myapp_user}" +APP_DB_PASSWORD="${APP_DB_PASSWORD:-secure_password_123}" + +echo "Creating database user: $APP_DB_USER" +# Create user +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + DO \$\$ + BEGIN + IF NOT EXISTS (SELECT FROM pg_catalog.pg_user WHERE usename = '$APP_DB_USER') THEN + CREATE USER $APP_DB_USER WITH PASSWORD '$APP_DB_PASSWORD'; + END IF; + END + \$\$; + ALTER USER $APP_DB_USER CREATEDB; +EOSQL + +echo "Creating database: $APP_DB_NAME" +# Create database +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + SELECT 'CREATE DATABASE $APP_DB_NAME' + WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$APP_DB_NAME')\gexec + GRANT ALL PRIVILEGES ON DATABASE $APP_DB_NAME TO $APP_DB_USER; +EOSQL + +echo "Granting schema privileges" +# Grant schema privileges +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$APP_DB_NAME" <<-EOSQL + GRANT ALL ON SCHEMA public TO $APP_DB_USER; +EOSQL + +echo "Database initialization complete!" + diff --git a/scripts/init_database/01_run_sql_files.sh b/scripts/init_database/01_run_sql_files.sh new file mode 100755 index 0000000..443bc2e --- /dev/null +++ b/scripts/init_database/01_run_sql_files.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# Run SQL initialization files in the correct database context +# This script runs after 00_init_user.sh creates the database + +set -e + +APP_DB_NAME="${APP_DB_NAME:-ai_conversations}" + +echo "Running SQL initialization files in database: $APP_DB_NAME" + +# Run create_conv_store.sql +if [ -f /docker-entrypoint-initdb.d/create_conv_store.sql ]; then + echo "Executing create_conv_store.sql..." + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$APP_DB_NAME" -f /docker-entrypoint-initdb.d/create_conv_store.sql +fi + +# Run create_prompt_config.sql +if [ -f /docker-entrypoint-initdb.d/create_prompt_config.sql ]; then + echo "Executing create_prompt_config.sql..." + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$APP_DB_NAME" -f /docker-entrypoint-initdb.d/create_prompt_config.sql +fi + +echo "SQL initialization files completed!" + diff --git a/scripts/init_database/init_all.sh b/scripts/init_database/init_all.sh new file mode 100755 index 0000000..474afef --- /dev/null +++ b/scripts/init_database/init_all.sh @@ -0,0 +1,48 @@ +#!/bin/bash +# Database initialization script +# This script runs all SQL initialization files in the correct order + +set -e + +DB_NAME="${POSTGRES_DB:-ai_conversations}" +DB_USER="${POSTGRES_USER:-myapp_user}" +DB_PASSWORD="${POSTGRES_PASSWORD:-secure_password_123}" +DB_HOST="${POSTGRES_HOST:-localhost}" +DB_PORT="${POSTGRES_PORT:-5432}" + +export PGPASSWORD="$DB_PASSWORD" + +echo "Initializing database: $DB_NAME on $DB_HOST:$DB_PORT" + +# Wait for PostgreSQL to be ready +until psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -c '\q' 2>/dev/null; do + echo "Waiting for PostgreSQL to be ready..." + sleep 2 +done + +echo "PostgreSQL is ready!" + +# Create database if it doesn't exist +psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres <