#!/usr/bin/env php query("SELECT ticket_id, title, hash FROM tickets WHERE status <> 'Closed' ORDER BY ticket_id") ->fetch_all(MYSQLI_ASSOC); $counts = ['updated' => 0, 'unchanged' => 0, 'conflict' => 0]; $lookup = $conn->prepare("SELECT ticket_id FROM tickets WHERE hash = ? AND ticket_id <> ? LIMIT 1"); $update = $conn->prepare("UPDATE tickets SET hash = ? WHERE ticket_id = ?"); foreach ($rows as $row) { $data = ['title' => (string)$row['title']]; $components = TicketHash::components($data); if ($components['issue_category'] !== 'network' || $components['source_type'] !== 'auto') { continue; } $newHash = TicketHash::generate($data); $id = $row['ticket_id']; if ($newHash === $row['hash']) { $counts['unchanged']++; echo "unchanged $id {$row['title']}\n"; continue; } $lookup->bind_param('ss', $newHash, $id); $lookup->execute(); $other = $lookup->get_result()->fetch_assoc(); if ($other) { $counts['conflict']++; echo "CONFLICT $id {$row['title']} (new hash already on ticket {$other['ticket_id']})\n"; continue; } if (!$dryRun) { $update->bind_param('ss', $newHash, $id); $update->execute(); } $counts['updated']++; echo ($dryRun ? 'would update' : 'updated ') . " $id {$row['title']}\n"; } printf( "%s%d updated, %d already current, %d conflicts\n", $dryRun ? '[dry run] ' : '', $counts['updated'], $counts['unchanged'], $counts['conflict'] );