package device import ( "context" "crypto/sha256" "encoding/hex" "github.com/jackc/pgx/v5" ) func recordFirmwareAssignmentEvent(ctx context.Context, tx pgx.Tx, commandID, commandStatus, assignmentStatus string, progress *int, resultJSON []byte) error { digest := sha256.Sum256(resultJSON) _, err := tx.Exec(ctx, ` INSERT INTO firmware_assignment_event( assignment_id,tenant_id,device_id,command_id,command_status,assignment_status,phase,progress_percent,result_code, active_slot,target_slot,previous_slot,failed_slot,boot_attempt_id,boot_health_deadline,rollback_applied,rollback_version, installed_version,verified_sha256,verified_signing_key_id, result_digest,result_summary ) SELECT fa.id,fa.tenant_id,fa.device_id,fa.command_id,$2,$3,NULLIF($6::jsonb->>'phase',''),$4,NULLIF($6::jsonb->>'result_code',''), NULLIF($6::jsonb->>'active_slot',''),NULLIF($6::jsonb->>'target_slot',''),NULLIF($6::jsonb->>'previous_slot',''), NULLIF($6::jsonb->>'failed_slot',''),NULLIF($6::jsonb->>'boot_attempt_id',''),NULLIF($6::jsonb->>'boot_health_deadline',''), CASE WHEN jsonb_typeof($6::jsonb->'rollback_applied')='boolean' THEN ($6::jsonb->>'rollback_applied')::boolean ELSE NULL END, NULLIF($6::jsonb->>'rollback_version',''),NULLIF($6::jsonb->>'installed_version',''),NULLIF($6::jsonb->>'verified_sha256',''), NULLIF($6::jsonb->>'verified_signing_key_id',''),$5,$6::jsonb FROM firmware_assignment fa WHERE fa.command_id=$1::uuid ON CONFLICT(assignment_id,command_status,result_digest) DO NOTHING`, commandID, commandStatus, assignmentStatus, progress, hex.EncodeToString(digest[:]), resultJSON) return err }