fix(pb-init): add user_id field migration for progress collection

The progress collection was created before user_id was added to the schema,
causing all user-keyed progress queries to return 400. Add an ensure_fields
helper that idempotently patches existing collections with missing fields,
and run it for progress.user_id on every init.
This commit is contained in:
Admin
2026-03-04 13:49:55 +05:00
parent 06feb91f4f
commit 9906c7d862

View File

@@ -38,7 +38,8 @@ if [ -z "$TOKEN" ] || [ "$TOKEN" = "$AUTH_RESPONSE" ]; then
fi
log "auth token obtained"
# ─── 3. Helper: create a collection (ignores 400/422 already-exists) ─────────
# ─── 3. Helpers ───────────────────────────────────────────────────────────────
create_collection() {
NAME="$1"
BODY="$2"
@@ -54,7 +55,60 @@ create_collection() {
esac
}
# ─── 4. Create collections ────────────────────────────────────────────────────
# Patch a collection by name: adds any fields listed in FIELDS_JSON that are
# missing from the current schema. This is idempotent and safe to re-run.
ensure_fields() {
NAME="$1"
FIELDS_JSON="$2" # JSON array of field objects to guarantee exist
# Fetch current schema
CURRENT=$(wget -qO- \
--header="Authorization: Bearer $TOKEN" \
"$PB_URL/api/collections/$NAME" 2>/dev/null)
COLLECTION_ID=$(echo "$CURRENT" | sed 's/.*"id":"\([^"]*\)".*/\1/')
if [ -z "$COLLECTION_ID" ] || [ "$COLLECTION_ID" = "$CURRENT" ]; then
log "WARNING: could not get id for collection $NAME — skipping ensure_fields"
return
fi
# Build merged fields list via a small python snippet
MERGED=$(python3 - "$CURRENT" "$FIELDS_JSON" <<'PYEOF'
import sys, json
current = json.loads(sys.argv[1])
desired = json.loads(sys.argv[2])
existing_names = {f["name"] for f in current.get("fields", [])}
all_fields = list(current.get("fields", []))
added = []
for f in desired:
if f["name"] not in existing_names:
all_fields.append(f)
added.append(f["name"])
print(json.dumps({"fields": all_fields, "_added": added}))
PYEOF
)
ADDED=$(echo "$MERGED" | python3 -c 'import sys,json; d=json.load(sys.stdin); print(",".join(d.get("_added",[])))' 2>/dev/null)
if [ -z "$ADDED" ]; then
log "collection $NAME schema up-to-date"
return
fi
PATCH_BODY=$(echo "$MERGED" | python3 -c 'import sys,json; d=json.load(sys.stdin); d.pop("_added",None); print(json.dumps(d))' 2>/dev/null)
STATUS=$(wget -qSO- \
--header="Content-Type: application/json" \
--header="Authorization: Bearer $TOKEN" \
--body-data="$PATCH_BODY" \
--method=PATCH \
"$PB_URL/api/collections/$COLLECTION_ID" 2>&1 | grep "HTTP/" | tail -1 | awk '{print $2}')
case "$STATUS" in
200|201) log "patched collection $NAME — added fields: $ADDED" ;;
*) log "WARNING: patch returned $STATUS for collection $NAME" ;;
esac
}
# ─── 4. Create collections (idempotent — skips if already exist) ─────────────
create_collection "books" '{
"name": "books",
@@ -134,4 +188,11 @@ create_collection "app_users" '{
]
}'
# ─── 5. Schema migrations (idempotent field additions) ───────────────────────
# Ensures fields added after initial deploy are present in existing instances.
ensure_fields "progress" '[
{"name": "user_id", "type": "text"}
]'
log "all collections ready"