import
[web.mtrack] / schema / 4-pre.php
1 <?php # vim:ts=2:sw=2:et:
2 # De-dupe components table
3
4 $names = array();
5 foreach ($db->query('select compid, name from components')->fetchAll() as $row)
6 {
7   $names[$row[1]][] = $row[0];
8 }
9
10 foreach ($names as $name => $ids) {
11   if (count($ids) == 1) continue;
12   echo "Fixing duplicate component: $name\n";
13   sort($ids);
14   $id = array_shift($ids);
15   $change = join(',', $ids);
16
17   $q = $db->prepare("update ticket_components set compid = ? where compid in ($change)");
18   $q->execute(array($id));
19   $q = $db->prepare("update components_by_project set compid = ? where compid in ($change)");
20   $q->execute(array($id));
21   $comps = array();
22   foreach ($ids as $i) {
23     $comps[] = $db->quote("component:$i");
24   }
25   $comps = join(',', $comps);
26   $db->exec("update changes set object = 'component:$id' where object in ($comps)");
27   $db->exec("delete from components where compid in ($change)");
28 }
29