clawdie-ai/docs/internal/scripts/fix-permissions.sh
Clawdie AI a0160a458d fix(multitenant): remove tenant-hostd naming and hardcode guard in audit (Sam & zAI)
Rename hostdSocketPath/hostdPidFile to platformHostdSocketPath/
platformHostdPidFile — the single hostd is platform-owned, not per-tenant.

Remove PLATFORM_SERVICE_NAME hardcode from classifyServiceOwner.
The shared-services check already wins for 'clawdie', so the guard
was redundant. Registry data now drives classification purely.

Update docs/internal/scripts/ to prefer TENANT_ID over AGENT_NAME
with backward-compatible fallback.

---
Build: pass | Tests: pass — 1757 passed (114 files)
2026-04-24 08:07:06 +02:00

78 lines
2.6 KiB
Bash
Executable file

#!/bin/sh
# fix-permissions.sh — Fix PostgreSQL permissions on existing databases
#
# Usage: ./fix-permissions.sh [db_user] [db_name]
#
# Defaults to values from .env if not provided.
#
# This script fixes the "permission denied for table memories" error
# by granting ALL PRIVILEGES on existing tables and sequences,
# and setting up DEFAULT PRIVILEGES for future tables.
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
cd "${PROJECT_ROOT}"
# ── Parse arguments or read from .env ───────────────────────────────────────
if [ $# -ge 2 ]; then
DB_USER="$1"
DB_NAME="$2"
else
# Read from .env
env_get() {
local key="$1" default="$2"
local val
val=$(grep -m1 "^${key}=" .env 2>/dev/null | cut -d= -f2- | sed "s/^['\"]//;s/['\"]$//")
printf '%s' "${val:-$default}"
}
TENANT_ID=$(env_get TENANT_ID "$(env_get AGENT_NAME clawdie)")
DB_USER="${1:-$(env_get MEMORY_DB_USER "${TENANT_ID}_brain")}"
DB_NAME="${2:-$(env_get MEMORY_DB_NAME "${TENANT_ID}_brain")}"
fi
echo "==> Fixing PostgreSQL permissions"
echo " Database: ${DB_NAME}"
echo " User: ${DB_USER}"
echo ""
# ── Check if psql is available ─────────────────────────────────────────────
if ! command -v psql >/dev/null 2>&1; then
echo "ERROR: psql not found in PATH" >&2
exit 1
fi
# ── Check for permissions.sql ─────────────────────────────────────────────
PERMISSIONS_SQL="${PROJECT_ROOT}/docs/internal/sql/permissions.sql"
if [ ! -f "${PERMISSIONS_SQL}" ]; then
echo "ERROR: ${PERMISSIONS_SQL} not found" >&2
echo " Make sure you're running from the Clawdie-AI repository" >&2
exit 1
fi
# ── Run permissions.sql ────────────────────────────────────────────────────
echo "==> Applying permissions..."
psql -v db_user="${DB_USER}" -d "${DB_NAME}" -f "${PERMISSIONS_SQL}"
if [ $? -eq 0 ]; then
echo ""
echo "✓ Permissions fixed successfully"
echo ""
echo " Tables in ${DB_NAME} are now accessible by ${DB_USER}"
echo " Future tables will auto-inherit permissions"
echo ""
else
echo ""
echo "✗ Failed to apply permissions"
echo " Check that:"
echo " - Database '${DB_NAME}' exists"
echo " - User '${DB_USER}' exists"
echo " - You have postgres superuser access"
exit 1
fi