fix: stop logging failures from silently breaking PC-machine relationships

Two related fixes in api.asp:

1. CreatePCMachineRelationship now archives stale Controls relationships in
   BOTH directions (Equipment->PC and the legacy PC->Equipment shape some
   prod rows use), so a self-registering PC cannot leave a stale controller
   active alongside its new link. Previously only the Equipment->PC direction
   was archived, leaving reversed legacy rows dangling.

2. LogToFile now clears Err before returning. It runs under On Error Resume
   Next; a failed write (logs dir not writable, disk full) left Err set, and
   CreatePCMachineRelationship tests Err.Number right after a LogToFile call,
   so a logging failure was misread as fatal and silently aborted relationship
   creation. Verified live: with the fix, updateCompleteAsset creates the PC,
   archives the reversed legacy relationship, and links the new PC.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-03 11:21:47 -04:00
parent fd295ef35e
commit a4051e381a

21
api.asp
View File

@@ -1821,13 +1821,20 @@ Function CreatePCMachineRelationship(pcMachineid, machineNumber)
' previous PC's relationship is stale (PC was replaced). Soft-archive via ' previous PC's relationship is stale (PC was replaced). Soft-archive via
' isactive=0 keeps history but excludes from active inventory views (the ' isactive=0 keeps history but excludes from active inventory views (the
' display pages already filter on isactive=1). ' display pages already filter on isactive=1).
'
' Match the equipment on EITHER side of the row. Legacy prod data stores
' some Controls links reversed (machineid = PC, related_machineid =
' equipment) instead of the canonical Equipment -> PC shape this function
' writes. A direction-blind archive catches both, so a self-registering
' DSC asset cannot leave a stale legacy controller active alongside it.
' The new PC's own link (either direction) is preserved by the <> guards.
Dim archiveSQL Dim archiveSQL
archiveSQL = "UPDATE machinerelationships " & _ archiveSQL = "UPDATE machinerelationships " & _
"SET isactive = 0, lastupdated = NOW() " & _ "SET isactive = 0, lastupdated = NOW() " & _
"WHERE machineid = " & CLng(equipmentMachineid) & _ "WHERE relationshiptypeid = " & CLng(relationshiptypeid) & _
" AND related_machineid <> " & CLng(pcMachineid) & _ " AND isactive = 1" & _
" AND relationshiptypeid = " & CLng(relationshiptypeid) & _ " AND ( (machineid = " & CLng(equipmentMachineid) & " AND related_machineid <> " & CLng(pcMachineid) & ")" & _
" AND isactive = 1" " OR (related_machineid = " & CLng(equipmentMachineid) & " AND machineid <> " & CLng(pcMachineid) & ") )"
LogToFile "CreatePCMachineRelationship: Archiving stale relationships: " & archiveSQL LogToFile "CreatePCMachineRelationship: Archiving stale relationships: " & archiveSQL
objConn.Execute archiveSQL objConn.Execute archiveSQL
If Err.Number <> 0 Then If Err.Number <> 0 Then
@@ -2732,6 +2739,12 @@ Sub LogToFile(message)
Set logFile = Nothing Set logFile = Nothing
Set fso = Nothing Set fso = Nothing
' Logging is best-effort. A failed write (e.g. logs dir not writable) must
' never leak Err into callers - several callers run under On Error Resume
' Next and test Err.Number right after a LogToFile call, so a logging
' failure would otherwise be misread as a fatal error.
Err.Clear
End Sub End Sub
' ============================================================================ ' ============================================================================