[Bounty Proposal] Validate id and use merge writes in database apps and tester access
Problem
-
NotFoundexception in tester access mutations (backend/database/apps.py:325-334): Inadd_app_access_for_tester_dbandremove_app_access_for_tester_db:def add_app_access_for_tester_db(app_id: str, uid: str) -> None: app_ref = db.collection(testers_collection).document(uid) app_ref.update({'apps': ArrayUnion([app_id])})When granting or revoking tester access via admin endpoints (
POST /v1/apps/tester/accessandDELETE /v1/apps/tester/accessinbackend/routers/apps.py:2291-2313), if the target user's document intesters/{uid}has not yet been initialized,app_ref.update(...)raisesgoogle.api_core.exceptions.NotFound: 404 No document to update. This unhandled exception bubbles up as an HTTP 500 server error instead of safely initializing or modifying the record. -
NotFoundonset_app_popular_db(backend/database/apps.py:96-98):app_ref.update({'is_popular': popular})fails if the target plugin document is not yet initialized or undergoing restore. -
Unhandled
KeyErrorand overwrite risks inadd_app_to_db,upsert_app_to_db,update_app_in_db, andupdate_persona_in_db:app_data['id']andpersona_data['id']raise an unhandledKeyErrorif payloads lack explicit'id'.update_app_in_dbandupdate_persona_in_dbuse.update(...), throwingNotFoundon missing documents.upsert_app_to_dbuses bare.set(...)withoutmerge=True, wiping existing fields not present in the update dictionary.
Proposed Solution
- Switch
add_app_access_for_tester_dbandremove_app_access_for_tester_dbtoapp_ref.set({'apps': ArrayUnion([app_id])}, merge=True)andapp_ref.set({'apps': ArrayRemove([app_id])}, merge=True)for idempotent lifecycle persistence. - Switch
set_app_popular_dbtoapp_ref.set({'is_popular': popular}, merge=True). - Validate required
idwith explicitValueErrorinadd_app_to_db,upsert_app_to_db,update_app_in_db, andupdate_persona_in_db, and use.set(..., merge=True). - Add comprehensive unit test suite in
backend/tests/unit/test_database_apps_resilience.py.