diff --git a/.gitignore b/.gitignore index 14cb5dfd..c2c3a11a 100644 --- a/.gitignore +++ b/.gitignore @@ -383,3 +383,4 @@ node_modules/ # InfluxDB exports and backups scripts/influxdb/exports/ +scripts/privy/privy-users.csv diff --git a/scripts/privy/import-privy-users.sh b/scripts/privy/import-privy-users.sh new file mode 100755 index 00000000..ee98d0fa --- /dev/null +++ b/scripts/privy/import-privy-users.sh @@ -0,0 +1,149 @@ +#!/bin/bash + +# Script to import privy-users.csv into WhitelistAccounts table +# Uses connection string from appsettings.ProductionLocal.json + +set -e # Exit on error + +# Get the directory where this script is located +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" +CSV_FILE="$SCRIPT_DIR/privy-users.csv" +SETTINGS_FILE="$PROJECT_ROOT/src/Managing.Api/appsettings.ProductionLocal.json" + +# Check if CSV file exists +if [ ! -f "$CSV_FILE" ]; then + echo "Error: CSV file not found at $CSV_FILE" + exit 1 +fi + +# Check if settings file exists +if [ ! -f "$SETTINGS_FILE" ]; then + echo "Error: Settings file not found at $SETTINGS_FILE" + exit 1 +fi + +# Extract connection string from JSON (using sed for macOS compatibility) +CONNECTION_STRING=$(grep '"ConnectionString"' "$SETTINGS_FILE" | sed 's/.*"ConnectionString": "\([^"]*\)".*/\1/') + +if [ -z "$CONNECTION_STRING" ]; then + echo "Error: Could not extract connection string from settings file" + exit 1 +fi + +# Parse connection string parameters (macOS compatible) +HOST=$(echo "$CONNECTION_STRING" | sed -n 's/.*Host=\([^;]*\).*/\1/p') +PORT=$(echo "$CONNECTION_STRING" | sed -n 's/.*Port=\([^;]*\).*/\1/p') +DATABASE=$(echo "$CONNECTION_STRING" | sed -n 's/.*Database=\([^;]*\).*/\1/p') +USERNAME=$(echo "$CONNECTION_STRING" | sed -n 's/.*Username=\([^;]*\).*/\1/p') +PASSWORD=$(echo "$CONNECTION_STRING" | sed -n 's/.*Password=\([^;]*\).*/\1/p') + +# Export password for psql +export PGPASSWORD="$PASSWORD" + +echo "Connecting to database: $DATABASE@$HOST:$PORT" +echo "Importing from: $CSV_FILE" +echo "" + +# Create SQL script as a here-document +psql -h "$HOST" -p "$PORT" -U "$USERNAME" -d "$DATABASE" <