← back to Japan Enrich

scripts/load-lines-remote.sh

30 lines

#!/usr/bin/env bash
# Push the loader + manifest to Kamatera and create the 19 distributor internal LINES
# in canonical dw_unified (each its own id + unique DW prefix + range, use_for_ai=true,
# keep_offline via vendor_never_on_shopify). Idempotent. NO Shopify publish.
# Run under Steve:  bash ~/Projects/japan-enrich/scripts/load-lines-remote.sh
set -euo pipefail

# db scripts → db/ ; manifest → staging/ (the loader reads ../staging/onboarding-manifest.json)
rsync -avz ~/Projects/japan-enrich/db/ root@45.61.58.125:/root/Projects/japan-staging-viewer/db/
rsync -avz ~/Projects/japan-enrich/staging/onboarding-manifest.json \
  root@45.61.58.125:/root/Projects/japan-staging-viewer/staging/onboarding-manifest.json

ssh root@45.61.58.125 bash -s <<'REMOTE'
set -e
cd /root/Projects/japan-staging-viewer
npm i pg -s
# collect every dw_unified DSN across project .env files, then PREFER the dw_admin (owner) one,
# because ALTER TABLE (use_for_ai column) requires table ownership.
CANDS=$(grep -rhoE "^DATABASE_URL=[^ ]+" /root/Projects/*/.env /root/Projects/*/*/.env /root/.env 2>/dev/null | cut -d= -f2- | tr -d '"' | grep dw_unified | sort -u)
echo "candidate DSN users:"; printf '%s\n' "$CANDS" | sed -E 's#://([^:]+):[^@]*@#://\1:***@#'
DATABASE_URL=$(printf '%s\n' "$CANDS" | grep -m1 dw_admin || true)
[ -z "$DATABASE_URL" ] && DATABASE_URL=$(printf '%s\n' "$CANDS" | grep -m1 -E "://postgres[:@]" || true)
[ -z "$DATABASE_URL" ] && DATABASE_URL=$(printf '%s\n' "$CANDS" | head -1)
[ -z "$DATABASE_URL" ] && DATABASE_URL="postgresql:///dw_unified"
export DATABASE_URL
echo "using user: $(printf '%s' "$DATABASE_URL" | sed -E 's#://([^:]+):.*#\1#')"
echo "db + owner check:"; psql "$DATABASE_URL" -Atc "select current_user, (select tableowner from pg_tables where tablename='vendor_registry')"
node db/load-distributor-lines.js
REMOTE