feat(ops): phase 8 — data migration scripts + fix password_reset_tokens in schema.sql

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-24 06:30:34 +08:00
co-authored by Claude Sonnet 4.6
parent 04c36f9d45
commit b3811b3633
6 changed files with 247 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
#!/bin/bash
# Usage:
# SUPABASE_URL="https://xxx.supabase.co" \
# SUPABASE_SERVICE_ROLE_KEY="eyJ..." \
# bash db/04-migrate-evidence.sh
set -e
if [ -z "$SUPABASE_URL" ] || [ -z "$SUPABASE_SERVICE_ROLE_KEY" ]; then
echo "ERROR: SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY must be set"
exit 1
fi
SERVER="setia@ims.setia.com.my"
PORT=9321
REMOTE_DIR="/var/lib/ims/evidence"
LOCAL_TMP="/tmp/ims-evidence-migration"
mkdir -p "$LOCAL_TMP"
echo "==> Listing all objects in Supabase Storage bucket 'evidence'..."
# Paginate: offset 0, limit 1000 (repeat if >1000 files)
OBJECTS=$(curl -s \
-H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \
-H "Content-Type: application/json" \
"$SUPABASE_URL/storage/v1/object/list/evidence" \
-d '{"limit": 1000, "offset": 0, "prefix": ""}')
COUNT=$(echo "$OBJECTS" | python3 -c "import sys,json; data=json.load(sys.stdin); print(len(data))" 2>/dev/null || echo 0)
echo "==> Found $COUNT objects"
if [ "$COUNT" -eq 0 ]; then
echo "==> No evidence files to migrate. Exiting."
exit 0
fi
echo "==> Downloading evidence files..."
# Export vars so the Python subprocess can access them
export LOCAL_TMP SERVER PORT REMOTE_DIR
echo "$OBJECTS" | python3 - <<'PYEOF'
import sys, json, os, subprocess, urllib.request
objects = json.load(sys.stdin)
supabase_url = os.environ['SUPABASE_URL']
service_role_key = os.environ['SUPABASE_SERVICE_ROLE_KEY']
local_tmp = os.environ.get('LOCAL_TMP', '/tmp/ims-evidence-migration')
server = os.environ['SERVER']
port = os.environ['PORT']
remote_dir = os.environ['REMOTE_DIR']
for i, obj in enumerate(objects):
name = obj['name'] # e.g. "userId/incidentId/stage/filename.jpg"
local_path = os.path.join(local_tmp, name)
os.makedirs(os.path.dirname(local_path), exist_ok=True)
# Download from Supabase Storage
url = f"{supabase_url}/storage/v1/object/evidence/{name}"
req = urllib.request.Request(url, headers={"Authorization": f"Bearer {service_role_key}"})
with urllib.request.urlopen(req) as resp, open(local_path, 'wb') as f:
f.write(resp.read())
print(f"[{i+1}/{len(objects)}] Downloaded: {name}")
PYEOF
echo "==> Uploading evidence files to server..."
# Create remote directory structure
ssh -p "$PORT" "$SERVER" "mkdir -p $REMOTE_DIR"
# rsync preserves directory structure
rsync -az --progress -e "ssh -p $PORT" "$LOCAL_TMP/" "$SERVER:$REMOTE_DIR/"
echo "==> Cleaning up local temp..."
rm -rf "$LOCAL_TMP"
echo "==> Verifying file count on server..."
ssh -p "$PORT" "$SERVER" "find $REMOTE_DIR -type f | wc -l"
echo "==> Evidence migration complete."