|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Load environment variables from .env file if it exists |
| 4 | +if [ -f .env ]; then |
| 5 | + export $(grep -v '^#' .env | xargs) |
| 6 | +fi |
| 7 | + |
| 8 | +# Generate a unique schema name and user credentials using a timestamp |
| 9 | +CURRENT_TIMESTAMP=$(date +"%Y%m%d_%H%M%S") |
| 10 | +NEW_SCHEMA_NAME="marketplace_squid_${CURRENT_TIMESTAMP}" |
| 11 | +NEW_DB_USER="marketplace_squid_user_${CURRENT_TIMESTAMP}" |
| 12 | + |
| 13 | +# Check if required environment variables are set |
| 14 | +if [ -z "$DB_USER" ] || [ -z "$DB_NAME" ] || [ -z "$DB_PASSWORD" ] || [ -z "$DB_HOST" ] || [ -z "$DB_PORT" ]; then |
| 15 | + echo "Error: Required environment variables are not set." |
| 16 | + echo "Ensure DB_USER, DB_NAME, DB_PASSWORD, DB_HOST, and DB_PORT are set." |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +# Log the generated variables |
| 21 | +echo "Generated schema name: $NEW_SCHEMA_NAME" |
| 22 | +echo "Generated user: $NEW_DB_USER" |
| 23 | + |
| 24 | +# Set PGPASSWORD to handle password prompt |
| 25 | +export PGPASSWORD=$DB_PASSWORD |
| 26 | + |
| 27 | +# Connect to the database and create the new schema and user |
| 28 | +psql -v ON_ERROR_STOP=1 --username "$DB_USER" --dbname "$DB_NAME" --host "$DB_HOST" --port "$DB_PORT" <<-EOSQL |
| 29 | + CREATE SCHEMA $NEW_SCHEMA_NAME; |
| 30 | + CREATE USER $NEW_DB_USER WITH PASSWORD '$DB_PASSWORD'; |
| 31 | + GRANT ALL PRIVILEGES ON SCHEMA $NEW_SCHEMA_NAME TO $NEW_DB_USER; |
| 32 | + GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $NEW_DB_USER; |
| 33 | + ALTER USER $NEW_DB_USER SET search_path TO $NEW_SCHEMA_NAME; |
| 34 | +EOSQL |
| 35 | + |
| 36 | +# Unset PGPASSWORD |
| 37 | +unset PGPASSWORD |
| 38 | + |
| 39 | +# Construct the DB_URL with the new user |
| 40 | +export DB_URL=postgresql://$NEW_DB_USER:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME |
| 41 | +export DB_SCHEMA=$NEW_SCHEMA_NAME |
| 42 | + |
| 43 | +# Log the constructed DB_URL |
| 44 | +echo "Exported DB_SCHEMA: $DB_SCHEMA" |
| 45 | + |
| 46 | +# Start the processor service and the GraphQL server, and write logs to a file |
| 47 | +LOG_FILE="sqd_run_log_${CURRENT_TIMESTAMP}.txt" |
| 48 | +echo "Starting squid services..." |
| 49 | +sqd run . > "$LOG_FILE" 2>&1 |
| 50 | + |
| 51 | +echo "Logs are being written to $LOG_FILE" |
0 commit comments