Script to fix old numeric IDs to new text IDs
[contractdashboard.git] / admin / fixoldamend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
include_once("../lib/common.inc.php");
 
$query = 'update contractnotice set "parentCN" = null where "parentCN" = \'0\'';
$result = $conn->prepare($query);
$result->execute();
$query = 'update contractnotice set "childCN" = null where "childCN" = \'0\'';
$result = $conn->prepare($query);
$result->execute();
$query = 'select "CNID","parentCN" from contractnotice where "CNID" like \'%00_\' and "parentCN" is not null';
$result = $conn->prepare($query);
$result->execute();
foreach ($result->fetchAll() as $record) {
    $oldCN = $record['CNID'];
    $parentCN = substr($oldCN,0,-3);
    if ($parentCN == $record['parentCN']) {
    $newCN = $parentCN . "-A". substr($oldCN,-1); 
    $updateresult = $conn->exec('UPDATE contractnotice SET "CNID" = 
\'' . $newCN . '\' where "CNID" = \'' . $oldCN . '\';');
    echo "$oldCN => $newCN (from parent CN $parentCN) <br>\n";
    }
    else {
        echo "parent CN unexpected - $oldCN doesn't look like child of {$record['parentCN']} <br>\n";
    }
}