← back to Silverleafwallpaper

scripts/activate-kamatera.sh

47 lines

#!/usr/bin/env bash
# activate-kamatera.sh — activate the RML- feed on the LIVE silverleafwallpaper.com.
#
# The live server reads dw_unified.microsite_products (not the static JSON), so
# after the code+data are deployed this rebuilds that table for the slug:
#   1. inherit the running app's DB env from pm2 (creds never printed)
#   2. delete the stale rows for this slug
#   3. migrate the 61 RML- products from the deployed data/products.json
#   4. pm2 restart + HTTP verify
#
# Run ON Kamatera:  bash /var/www/silverleafwallpaper.com/scripts/activate-kamatera.sh
set -euo pipefail

SLUG=silverleafwallpaper
SITE=/var/www/silverleafwallpaper.com
MIGRATE=/root/Projects/_shared/scripts/migrate-microsite-products.js
PM2_ID=29

# Pull the DB connection env the live app uses, without echoing it.
set -a
eval "$(pm2 env "$PM2_ID" 2>/dev/null | grep -E '^(MICROSITE_DB_URL|DATABASE_URL|PGHOST|PGPORT|PGUSER|PGPASSWORD|PGDATABASE)=' || true)"
set +a
# migrate-microsite-products.js reads MICROSITE_DB_URL; fall back to DATABASE_URL.
export MICROSITE_DB_URL="${MICROSITE_DB_URL:-${DATABASE_URL:-}}"

PSQL=(psql)
[ -n "${MICROSITE_DB_URL:-}" ] && PSQL=(psql "$MICROSITE_DB_URL")

echo "== before =="
"${PSQL[@]}" -tAc "select count(*) from microsite_products where site_slug='$SLUG';"

echo "== delete stale slug rows =="
"${PSQL[@]}" -c "delete from microsite_products where site_slug='$SLUG';"

echo "== migrate RML- feed =="
cd "$SITE"
node "$MIGRATE" "$SITE"

echo "== after =="
"${PSQL[@]}" -tAc "select count(*) total, count(*) filter (where sku ilike 'RML-%') rml from microsite_products where site_slug='$SLUG';"

echo "== restart + verify =="
pm2 restart "$SLUG" --update-env >/dev/null 2>&1
sleep 3
curl -s "http://127.0.0.1:9912/api/products?limit=60" \
  | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{const j=JSON.parse(s);console.log("LIVE total:",j.total,"| all RML-:",j.items.every(p=>/rml-/i.test(p.sku||p.handle||"")),"| first:",j.items[0]&&j.items[0].title)})'