Camp laptop down-sync re-inserts the camp's own visits as duplicates and overwrites central VanSerialNo (camp 454 RCA)

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: High
    • 2.4.2
    • Affects Version/s: None

      Summary

      When a camp runs the manual down-sync (central to camp laptop), central sends the camp its own previously uploaded visits. The laptop does not recognise them, inserts each one again under a new ID, and the acknowledgement then overwrites the central row's VanSerialNo with the new copy's ID. Result: duplicate visits on the laptop, and central now points at the copy instead of the original, so child rows that still reference the original visit are rejected on upload.

      Introduced with the down-sync feature (AMM-2421, MMU-API PR #179, AMRIT-DB V119-V122).

      Impact

      • Camp laptop gets a duplicate of every visit returned by down-sync. Camp 454: 12,819 duplicate visits from two runs (12,746 visits now stored 2-3 times).
      • Central t_benvisitdetail.VanSerialNo is rewritten to the duplicate's ID, so the link to the original laptop visit is lost.
      • Child rows (t_PrescribedDrug, vitals, referrals, etc.) still point at the original visit ID. translateVanLinks cannot find it on central and rejects them: "Linked t_benvisitdetail (van ID 2420) not on central yet; sent again on next upload". These never clear.
      • Undoes the VanSerialNo repair done on central on 6 Nov for every van that runs down-sync afterwards.

      Scale on central (checked 6 Nov 2026):

      • t_benvisitdetail rows already down-synced back to their own van since 1 Nov: 91,158 across 146 vans (31,386 rows / 98 vans on 6 Nov alone).
      • Still queued for down-sync (DownSynced = 'N'): 122,241 rows across 136 vans. Any camp pressing down-sync will get these back as duplicates.

      Evidence: camp 454 (Dammapeta Van1)

      Laptop (Fleet queries #28, #32): rows with CentralID set and a LastDownSyncDate are all copies of existing visits (same VisitCode + BeneficiaryRegID + CreatedDate).

      Down-sync run (IST) Copies inserted New laptop BenVisitIDs
      5 Nov 16:22-16:23 3,189 15546-18734
      6 Nov 16:28-16:31 9,630 18831-28460
      • Copies have Processed = 'P' and VanSerialNo = their own new ID.
      • No t_prescription row has CentralID, so prescriptions were not copied; only visits.

      Central (t_benvisitdetail, VanID 454): DownSynced = 'P' with DownSyncDate at the same moments (UTC 5 Nov 10:52-10:53, 6 Nov 10:58-11:01), and VanSerialNo = the copy IDs (15546-18734, 18831-28460). All 13,887 central visits for van 454 now point at the newest copy.

      Example (visit 2420):

      • Laptop: BenVisitID 2420 (original, created 2026-09-28 11:29:23, VisitCode 10045400002420) and BenVisitID 16211 (copy, same VisitCode/beneficiary/created, LastModDate 2026-11-05 16:22:44, CentralID set).
      • Central: BenVisitID 7734, VisitCode 10045400002420, VanSerialNo = *16211*.
      • 30 t_PrescribedDrug rows on the laptop reference visit 2420 and fail with "Linked t_benvisitdetail (van ID 2420) not on central yet".

      Root cause (MMU-API, release-3.12.0 / main)

      1. Central selects the van's own rows. DataSyncRepositoryCentralDownload.getDownSyncDataFromTable: WHERE VanID = ? AND (DownSynced IS NULL OR DownSynced IN ('N','U','F') OR (DownSynced='P' AND LastModDate > DownSyncDate)). Every row the camp itself uploaded before the feature went live is still DownSynced = 'N': the initial setup (AMRIT-DB V122__Downsync_Central_Initial_setup.sql) marked 'P' only rows WHERE VanSerialNo IS NOT NULL, and most camp uploads before 31 Oct had VanSerialNo NULL.
      2. Laptop matches only on CentralID. DataSyncRepository.getLocalRecordForDownSync: WHERE CentralID = ? AND VanID = ?. Rows the laptop created itself have CentralID NULL, so nothing matches.
      3. Laptop inserts a copy. DownSyncDataFromServerImpl.insertRecord inserts with a new local PK, Processed = 'P', CentralID set, then updateVanSerialNoInLocal sets VanSerialNo = new local ID.
      4. Central overwrites the key. DataSyncRepositoryCentralDownload.updateDownSyncFlagPostDownload: UPDATE ... SET DownSynced='P', DownSyncDate=now(), DownSyncFailureReason=NULL, VanSerialNo = ? WHERE <pk> = ?, unconditionally, even when VanSerialNo was already set.

      Fix required

      1. Laptop: before inserting a down-synced row, match an existing local row by VanSerialNo + VanID (and, for visits, VisitCode + BeneficiaryRegID + CreatedDate). If found, stamp CentralID on it and update instead of insert.
      2. Central: updateDownSyncFlagPostDownload must not overwrite a VanSerialNo that is already set (only fill when NULL, and log a mismatch).
      3. Central: do not send a van rows it uploaded itself unless the laptop asks for a restore (e.g. exclude rows uploaded by this van, or mark all historical camp-uploaded rows DownSynced = 'P', not only VanSerialNo IS NOT NULL).
      4. Same review for t_prescription and every other TRANSACTIONAL table in m_downsynctabledetail.
      5. Add a test: upload a visit from a van, run down-sync for the same van, assert no new local row and central VanSerialNo unchanged.

      Data repair needed (after the fix)

      • Laptops: delete the down-sync copies (rows with CentralID set that duplicate an original row) and keep the originals, because child rows point at the originals.
      • Central: reset VanSerialNo to the original laptop IDs for rows whose key was set by a down-sync ack.
      • Camp 454 first (12,819 copies identified). A Fleet query across all laptops is needed to size the rest.

      Immediate mitigation

      • Ask camps not to run down-sync until the fix is released.
      • Optional: on central, mark camp-uploaded rows still queued (DownSynced = 'N') as 'P' so an accidental press does not create more copies.

      Related, separate root cause on the same camp (not covered by this fix)

      Camp 454 also has 13 t_section_response rows rejected (FK_section_response_section) and 57 t_question_response rows rejected (FK_question_response_section_response):

      • t_form_section differs between laptop (34 sections, form versions 1-14) and central (18 sections, versions 1-8); the same sectionId means different sections on each side.
      • translateVanLinks (VAN_LINKS) translates only BenVisitID and PrescriptionID; responseId and sectionResponseId are uploaded as raw laptop IDs, so the 5 section responses that did upload are linked to the wrong form responses on central.

      Needs its own ticket.

      References

      • Down-sync feature: AMM-2421 (QA approved, closed 16 Oct 2026); MMU-API PR #179, PR #203; AMRIT-DB V119, V120, V121, V122.
      • Repair log: plans/stoptb-vanserialno-repair.md (changes #10-#14).
      • Fleet queries used: #21, #28, #29, #30, #31, #32 (camp 454).

              Assignee:
              Vanitha S
              Reporter:
              Dr Mithun James
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated: