|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +show_help() { |
| 6 | + cat << 'EOF' |
| 7 | +cleanup_resource.sh — Delete all Indexd records associated with a given resource path. |
| 8 | +
|
| 9 | +USAGE: |
| 10 | + cleanup_resource.sh <pod-name> <postgres-password> [resource-name] |
| 11 | +
|
| 12 | +PARAMETERS: |
| 13 | + pod-name |
| 14 | + The name of the Kubernetes pod running Postgres. |
| 15 | + Example: local-postgresql-0 |
| 16 | +
|
| 17 | + postgres-password |
| 18 | + The password for the Postgres user inside the pod. |
| 19 | + Example: <see default/local-postgresql> |
| 20 | +
|
| 21 | + resource-name (optional) |
| 22 | + The Indexd resource path to delete. |
| 23 | + Defaults to: /programs/cbds/projects/monorepos |
| 24 | +
|
| 25 | +OPTIONS: |
| 26 | + --help |
| 27 | + Show this documentation and exit. |
| 28 | +
|
| 29 | +EXAMPLE: |
| 30 | + cleanup_resource.sh local-postgresql-0 <see default/local-postgresql> |
| 31 | + cleanup_resource.sh local-postgresql-0 <see default/local-postgresql> "/programs/myproj/resource" |
| 32 | +
|
| 33 | +DESCRIPTION: |
| 34 | + This script: |
| 35 | + • connects to a Postgres pod via kubectl exec |
| 36 | + • runs SQL that deletes all Indexd records associated with the resource |
| 37 | + • cleans up metadata, hash, url, authz, and main index_record rows |
| 38 | + • uses dynamic SQL so the resource path is customizable |
| 39 | +
|
| 40 | +EOF |
| 41 | +} |
| 42 | + |
| 43 | +# Detect --help anywhere |
| 44 | +for arg in "$@"; do |
| 45 | + if [[ "$arg" == "--help" ]]; then |
| 46 | + show_help |
| 47 | + exit 0 |
| 48 | + fi |
| 49 | +done |
| 50 | + |
| 51 | +# Parameters |
| 52 | +POD_NAME="${1:-}" |
| 53 | +POSTGRES_PASSWORD="${2:-}" |
| 54 | +RESOURCE_NAME="${3:-/programs/cbds/projects/monorepos}" |
| 55 | + |
| 56 | +if [[ -z "$POD_NAME" || -z "$POSTGRES_PASSWORD" ]]; then |
| 57 | + echo "Error: missing required parameters." |
| 58 | + echo "Run with --help for usage." |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +echo "🔧 Cleaning Indexd records for resource: $RESOURCE_NAME" |
| 63 | + |
| 64 | +# SQL script with dynamic resource name |
| 65 | +read -r -d '' SQL << EOF |
| 66 | +
|
| 67 | +-- Connect to the indexd_local database |
| 68 | +
|
| 69 | +\c indexd_local ; |
| 70 | +
|
| 71 | +-- Delete all records associated with the resource '${RESOURCE_NAME}' |
| 72 | +
|
| 73 | +-- Create a temporary table to hold DIDs for the resource |
| 74 | +select did into TEMP TABLE resource_did from index_record_authz where resource = '${RESOURCE_NAME}'; |
| 75 | +
|
| 76 | +-- Delete related records from metadata, hash, and url tables |
| 77 | +delete from index_record_metadata where did in (select did from resource_did); |
| 78 | +delete from index_record_hash where did in (select did from resource_did); |
| 79 | +delete from index_record_url where did in (select did from resource_did); |
| 80 | +
|
| 81 | +-- Delete authz records for the resource |
| 82 | +delete from index_record_authz where resource = '${RESOURCE_NAME}'; |
| 83 | +
|
| 84 | +-- Finally, delete the main index records |
| 85 | +delete from index_record where did in (select did from resource_did); |
| 86 | +
|
| 87 | +-- Drop the temporary table |
| 88 | +drop table resource_did; |
| 89 | +
|
| 90 | +EOF |
| 91 | + |
| 92 | +# Execute SQL inside the pod |
| 93 | +kubectl exec -it "$POD_NAME" -- \ |
| 94 | + env PGPASSWORD="$POSTGRES_PASSWORD" \ |
| 95 | + psql -U postgres -d postgres -v ON_ERROR_STOP=1 -c "$SQL" |
| 96 | + |
| 97 | +echo "✔ Cleanup complete for resource: $RESOURCE_NAME" |
0 commit comments