Files
loki/scripts/db/sync-user-data.sh
CatoandClaude Opus 5.5 5f1e283117 fix(scripts): grep -q is never the reader of a pipe under pipefail (#874)
`echo "$x" | grep -q PAT` under `set -o pipefail` can report "not found"
for text that IS there: grep -q exits on its first match and closes the
pipe, the writer takes SIGPIPE (141), and pipefail makes the pipeline
status 141. An `if` then takes the else branch on a successful match.

#587 found this in one file and noted it "only fires when the payload
exceeds the ~4KB pipe buffer". That is not so, and the belief is why
82 copies in 22 scripts survived. Measured today on retire-site's real
1,158-byte plan output with has()'s exact pattern, 3,000 calls each:

  'DRY RUN'        line 3 of 20    echo|grep -q   2/3000 false misses
  'apps.conf row'  line 11 of 20   echo|grep -q   2/3000 false misses
  last phrase      line 20 of 20   echo|grep -q   0/3000
  'DRY RUN'                        here-string    0/3000

Rare per call, but verify makes hundreds of these calls, and the
retire-site suite went red on those two exact phrases twice this week,
passing every rerun. It is worse in a NEGATIVE check - hasnt() read a
false miss as a PASS. And several copies are not tests: deploy-hetzner.sh
exits 1 on a false miss, and the sshd drift watch would raise a false
security alert.

All 82 rewritten to `grep -q PAT <<<"$x"` (a here-string is not a
pipeline, so there is no writer to signal) by a script that only touched
the simple quoted-variable form and reported anything else. Checked: all
22 files pass `bash -n`, every file is a 1:1 line swap (82 lines), and
the 20 line-continuation sites keep the here-string before the trailing
backslash (the first draft of the rewrite got those wrong; caught in a
dry-run diff before anything was written).

#587's comment is corrected so it stops teaching the wrong threshold.

Gate: scripts/test/no-grep-q-in-a-pipe.ts ratchets it repo-wide,
including forms the rewrite skipped (e.g. "$(cmd)" | grep -q), and
self-tests that its detector still fires. Proven by restoring the
original deploy-hetzner.sh line.

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 02:57:19 -07:00

152 lines
4.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copy user-owned rows from one Postgres database to another, rewriting user_id.
# Vendor-neutral — works for local→remote, box→box, etc.
#
# Usage:
# SOURCE_DATABASE_URL=... TARGET_DATABASE_URL=... TARGET_USER_ID=<uuid> \
# ./scripts/db/sync-user-data.sh [--dry-run] [--force]
#
# Env vars:
# SOURCE_DATABASE_URL Source (defaults to local cockpit Postgres)
# TARGET_DATABASE_URL Target direct URL. REQUIRED.
# SOURCE_USER_ID Source user_id to rewrite (default: dev seed UUID)
# TARGET_USER_ID Target user_id in destination. REQUIRED.
#
# Legacy aliases (still accepted): LOCAL_DATABASE_URL, LOCAL_USER_ID
#
# Prerequisites: psql + pg_dump in PATH.
set -euo pipefail
SOURCE_URL="${SOURCE_DATABASE_URL:-${LOCAL_DATABASE_URL:-postgresql://cockpit:cockpit_local@127.0.0.1:5432/cockpit}}"
TARGET_URL="${TARGET_DATABASE_URL:-}"
SOURCE_USER_ID="${SOURCE_USER_ID:-${LOCAL_USER_ID:-00000000-0000-0000-0000-000000000001}}"
TARGET_USER_ID="${TARGET_USER_ID:-}"
if [ -z "$TARGET_URL" ]; then
echo "ERROR: set TARGET_DATABASE_URL before running." >&2
exit 2
fi
if [ -z "$TARGET_USER_ID" ]; then
echo "ERROR: set TARGET_USER_ID to the destination user's UUID." >&2
echo "Find it via: psql \"\$TARGET_DATABASE_URL\" -c \"SELECT id, email FROM users\"" >&2
exit 2
fi
DRY_RUN=false
FORCE=false
for arg in "$@"; do
[[ "$arg" == "--dry-run" ]] && DRY_RUN=true
[[ "$arg" == "--force" ]] && FORCE=true
done
echo "=== Postgres user-data sync ==="
echo " Source user_id : $SOURCE_USER_ID"
echo " Target user_id : $TARGET_USER_ID"
$DRY_RUN && echo " MODE: DRY RUN (no writes to target)"
echo ""
TABLES=(
orgs
org_memberships
entities
attributes
entity_relations
interactions
alerts
user_projects
goals
subscriptions
commitments
events
actions
habits
habit_completions
orchestration_runs
orchestration_events
project_states
prompt_history
pending_commands
)
echo "Verifying target connection..."
TARGET_USER_COUNT=$(psql "$TARGET_URL" -t -c "SELECT count(*) FROM users;" 2>/dev/null | tr -d ' \n')
[[ -z "$TARGET_USER_COUNT" || "$TARGET_USER_COUNT" == "0" ]] && { echo "ERROR: target users table empty or unreachable."; exit 1; }
echo " Target users: $TARGET_USER_COUNT ✓"
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
IMPORTED=0
SKIPPED=0
for t in "${TABLES[@]}"; do
SOURCE_COUNT=$(psql "$SOURCE_URL" -t -c "SELECT count(*) FROM $t;" 2>/dev/null | tr -d ' \n')
TARGET_COUNT=$(psql "$TARGET_URL" -t -c "SELECT count(*) FROM $t;" 2>/dev/null | tr -d ' \n')
if [[ "$SOURCE_COUNT" == "0" ]]; then
echo " $t: 0 rows — skip"
SKIPPED=$((SKIPPED + 1))
continue
fi
if [[ "$TARGET_COUNT" != "0" && "$FORCE" == "false" ]]; then
echo " $t: already has $TARGET_COUNT rows — skip (--force to overwrite)"
SKIPPED=$((SKIPPED + 1))
continue
fi
echo -n " $t: $SOURCE_COUNT rows — "
COPY_FILE="$TMPDIR/${t}.copy"
pg_dump "$SOURCE_URL" \
--table="$t" \
--data-only \
--no-comments \
--no-owner \
--no-privileges \
2>/dev/null \
| awk '/^COPY /,/^\\./' \
| sed "s/$SOURCE_USER_ID/$TARGET_USER_ID/g" \
> "$COPY_FILE"
if $DRY_RUN; then
echo "would import (dry run)"
head -3 "$COPY_FILE"
continue
fi
if [[ "$TARGET_COUNT" != "0" && "$FORCE" == "true" ]]; then
psql "$TARGET_URL" -c "TRUNCATE TABLE $t CASCADE;" 2>/dev/null
fi
RESULT=$(psql "$TARGET_URL" -f "$COPY_FILE" 2>&1)
if grep -q "^COPY" <<<"$RESULT"; then
ROWS=$(echo "$RESULT" | grep "^COPY" | awk '{print $2}')
echo "imported $ROWS rows ✓"
IMPORTED=$((IMPORTED + 1))
else
echo "ERROR"
echo "$RESULT" | head -5
fi
done
echo ""
echo "=== Verification ==="
psql "$TARGET_URL" -c "
SELECT 'entities' as t, count(*) FROM entities
UNION ALL SELECT 'user_projects', count(*) FROM user_projects
UNION ALL SELECT 'goals', count(*) FROM goals
UNION ALL SELECT 'subscriptions', count(*) FROM subscriptions
UNION ALL SELECT 'commitments', count(*) FROM commitments
UNION ALL SELECT 'events', count(*) FROM events
UNION ALL SELECT 'interactions', count(*) FROM interactions
UNION ALL SELECT 'prompt_history', count(*) FROM prompt_history
UNION ALL SELECT 'orchestration_events', count(*) FROM orchestration_events
ORDER BY t;" 2>/dev/null
echo ""
echo "Done: $IMPORTED tables imported, $SKIPPED skipped."