Added tests, fix README.

This commit is contained in:
Rene Arumetsa
2026-07-26 20:32:13 +03:00
parent cb18d9b882
commit 5ba5642694
9 changed files with 769 additions and 138 deletions

View File

@@ -25,7 +25,9 @@ import config # noqa: E402
PB_URL = config.PB_URL
PB_ADMIN_EMAIL = config.PB_ADMIN_EMAIL
PB_ADMIN_PASSWORD = config.PB_ADMIN_PASSWORD
COLLECTION = config.PB_ECONOMY_COLLECTION
# Patch both profiles' collections so the result doesn't depend on which
# BOT_PROFILE happened to be set when the script was run.
COLLECTIONS = sorted({config.PB_ECONOMY_COLLECTION_DEV, config.PB_ECONOMY_COLLECTION_ECONOMY})
# ---------------------------------------------------------------------------
# New fields to add
@@ -55,41 +57,44 @@ async def main() -> None:
hdrs = {"Authorization": token}
# ── Fetch current collection ─────────────────────────────────────────
async with session.get(
f"{PB_URL}/api/collections/{COLLECTION}", headers=hdrs
) as resp:
if resp.status != 200:
print(f"Could not fetch collection ({resp.status}): {await resp.text()}")
return
col = await resp.json()
for collection in COLLECTIONS:
print(f"── {collection} ──")
existing = {f["name"] for f in col.get("fields", [])}
print(f"Existing fields ({len(existing)}): {sorted(existing)}\n")
# ── Fetch current collection ─────────────────────────────────────
async with session.get(
f"{PB_URL}/api/collections/{collection}", headers=hdrs
) as resp:
if resp.status != 200:
print(f"Could not fetch collection ({resp.status}): {await resp.text()}\n")
continue
col = await resp.json()
new_fields = []
for name in _NEW_JSON_FIELDS:
if name not in existing:
new_fields.append(_json_field(name))
print(f" + {name} (json)")
else:
print(f" = {name} (already exists)")
existing = {f["name"] for f in col.get("fields", [])}
print(f"Existing fields ({len(existing)}): {sorted(existing)}\n")
if not new_fields:
print("\nNothing to add - schema already up to date.")
return
new_fields = []
for name in _NEW_JSON_FIELDS:
if name not in existing:
new_fields.append(_json_field(name))
print(f" + {name} (json)")
else:
print(f" = {name} (already exists)")
# ── Patch collection schema ──────────────────────────────────────────
updated_fields = col.get("fields", []) + new_fields
async with session.patch(
f"{PB_URL}/api/collections/{COLLECTION}",
json={"fields": updated_fields},
headers=hdrs,
) as resp:
if resp.status != 200:
print(f"\nSchema update failed ({resp.status}): {await resp.text()}")
return
print(f"\n✅ Added {len(new_fields)} field(s) successfully.")
if not new_fields:
print("Nothing to add - schema already up to date.\n")
continue
# ── Patch collection schema ──────────────────────────────────────
updated_fields = col.get("fields", []) + new_fields
async with session.patch(
f"{PB_URL}/api/collections/{collection}",
json={"fields": updated_fields},
headers=hdrs,
) as resp:
if resp.status != 200:
print(f"Schema update failed ({resp.status}): {await resp.text()}\n")
continue
print(f"✅ Added {len(new_fields)} field(s) successfully.\n")
if __name__ == "__main__":