@@ -278,7 +278,7 @@ def duplicate_document_comments(
278278
279279
280280@app .get ("/" )
281- async def root ():
281+ def root ():
282282 return {"message" : "Hello World" }
283283
284284
@@ -790,6 +790,9 @@ def _sync_update_assignments(
790790 status_code = status .HTTP_409_CONFLICT ,
791791 detail = "Document has been updated since the last update" ,
792792 )
793+ # Track whether anything actually changed so we can skip the updated_at bump on
794+ # true no-op requests (which would otherwise break optimistic concurrency for
795+ # other clients).
793796 mutated = False
794797
795798 # Snapshot pre-existing district-mode assignments to compute which zones
@@ -814,6 +817,10 @@ def _sync_update_assignments(
814817 {"document_id" : document_id },
815818 )
816819
820+ # The assignments field is always a full replacement set:
821+ # [] means "delete all assignments" (user cleared everything)
822+ # [...] means "replace with these assignments"
823+ # Always DELETE existing rows, then INSERT new ones if any.
817824 delete_result = session .connection ().execute (
818825 text (f"DELETE FROM { assignment_table } WHERE document_id = :document_id" ),
819826 {"document_id" : document_id },
@@ -823,6 +830,12 @@ def _sync_update_assignments(
823830 inserted_count = 0
824831 has_assignments = len (assignments ) > 0
825832 if has_assignments :
833+ # For community maps, build the set of valid community_ids so we can reject
834+ # orphan-producing writes before they hit the table. 0 is the "unassigned"
835+ # sentinel; positive ids must exist in the effective metadata list. Skip the
836+ # check entirely when no metadata has been established yet (either in this
837+ # request or previously persisted) — that's the bootstrap path where the UI
838+ # writes assignments before the metadata save lands.
826839 valid_community_ids : set [int ] | None = None
827840 if is_community_map :
828841 if validated_community_metadata is not None :
@@ -978,6 +991,7 @@ def _sync_update_assignments(
978991 ):
979992 mutated = True
980993
994+ # Sync scoped comments via comments schema (None = no change, [] = delete all)
981995 if data .comments is not None :
982996 comment_inputs : list [DistrictCommentInput ] = []
983997 for c in data .comments :
@@ -1050,10 +1064,16 @@ def _sync_update_assignments(
10501064 if mutated :
10511065 updated_at = update_timestamp (session , document_id )
10521066 else :
1067+ # No-op request (e.g. assignments=[] on an already-empty doc with no metadata
1068+ # or comment changes). Keep updated_at pinned to its current value so other
1069+ # clients' optimistic-concurrency windows aren't invalidated.
10531070 updated_at = session .exec (
10541071 select (Document .updated_at ).where (Document .document_id == document_id )
10551072 ).one ()
10561073 if dirty_zones :
1074+ # Bump assignments_updated_at so /stats can tell that the CDN object
1075+ # is stale and republish, even on the path that doesn't otherwise
1076+ # change document.updated_at.
10571077 session .connection ().execute (
10581078 text (
10591079 "UPDATE document.document SET assignments_updated_at = NOW() "
0 commit comments