Skip to content

Instantly share code, notes, and snippets.

@xinaxu
Created May 20, 2024 21:26
Show Gist options
  • Save xinaxu/64258f447ff7ce30f84d76535dc6d0f2 to your computer and use it in GitHub Desktop.
Save xinaxu/64258f447ff7ce30f84d76535dc6d0f2 to your computer and use it in GitHub Desktop.
Add a tenant to Spade database
#!/bin/bash
# Script to add a tenant to the Spade database
# Usage: ./add_tenant.sh <tenant_name> <client_id> <client_address> [dataset_slug]
# Check if the correct number of arguments are passed
if [ "$#" -lt 3 ] || [ "$#" -gt 4 ]; then
echo "Usage: $0 <tenant_name> <client_id> <client_address> [dataset_slug]"
echo "<tenant_name>: The name of the tenant to be added."
echo "<client_id>: The f0 address of the wallet on-chain, converted to an integer (e.g., f02949060)."
echo "<client_address>: The wallet address that will spend FIL+ (must be an f1, f3, or f4 address)."
echo "[dataset_slug]: (Optional) A descriptive text string for the dataset (URL-encodable)."
exit 1
fi
# Assigning arguments to variables
TENANT_NAME=$1
CLIENT_ID=$2
CLIENT_ADDRESS=$3
DATASET_SLUG=$4
# Define database connection variables
DB_HOST="your_db_host"
DB_NAME="your_db_name"
DB_USER="your_db_user"
DB_PASSWORD="your_db_password"
# Function to execute SQL commands
execute_sql() {
SQL_COMMAND=$1
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -d $DB_NAME -U $DB_USER -c "$SQL_COMMAND"
}
# Start script execution
echo "Starting the process to add tenant: $TENANT_NAME"
# Insert tenant and capture tenant_id
SQL_INSERT_TENANT="INSERT INTO spd.tenants (tenant_name, tenant_meta) VALUES ('$TENANT_NAME', '{
\"deal_params\": {
\"duration_days\": 540,
\"start_within_hours\": 72
},
\"max\": {
\"total_replicas\": 10,
\"per_org\": 1,
\"per_city\": 3,
\"per_country\": 2,
\"per_continent\": 1,
\"filplus_exclusive\": true,
\"tenant_exclusive\": false
}
}'::jsonb) RETURNING tenant_id;"
TENANT_ID=$(execute_sql "$SQL_INSERT_TENANT" | grep -Eo '[0-9]+')
if [ -z "$TENANT_ID" ]; then
echo "Failed to insert tenant."
exit 1
fi
echo "Tenant added successfully with tenant_id: $TENANT_ID"
# Insert client for the tenant
SQL_INSERT_CLIENT="INSERT INTO spd.clients (tenant_id, client_id, client_address) VALUES ($TENANT_ID, $CLIENT_ID, '$CLIENT_ADDRESS');"
execute_sql "$SQL_INSERT_CLIENT"
echo "Client added successfully for tenant_id: $TENANT_ID"
# Insert dataset for the tenant, if provided
if [ -n "$DATASET_SLUG" ]; then
SQL_INSERT_DATASET="INSERT INTO spd.datasets (tenant_id, dataset_slug) VALUES ($TENANT_ID, '$DATASET_SLUG');"
execute_sql "$SQL_INSERT_DATASET"
echo "Dataset added successfully for tenant_id: $TENANT_ID"
else
echo "No dataset slug provided, skipping dataset creation."
fi
echo "Tenant setup completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment