My solution was this:
$table = "tableName";
$createTableSQL = $dbh->Execute('SHOW CREATE TABLE ' . $table);
$createTableSQL = $createTableSQL[0][1];
$mappingTable = "originalToDevMapping";
//get mapping
$sql = "SELECT origField, newField
FROM " . $mappingTable;
$newColumns = $dbh->Execute($sql);
foreach ($newColumns as $newColumn) {
if (strlen($newColumn['newField'])<1) {
echo "***Removing*** " . $newColumn['origField'] . "<br><br>";
$sql = "ALTER TABLE " . $table . " DROP COLUMN " . $newColumn['origField'];
$dbh->Execute($sql);
if (strlen($dbh->errorStr)>1) {
echo "<br>************************<br>";
echo "<br>ERROR:<br>";
echo $dbh->errorStr;
echo "<br>************************<br>";
}
} else {
echo "Renaming " . $newColumn['origField'] . " to " . $newColumn['newField'] . "<br><br>";
$sql = "ALTER TABLE " . $table . " CHANGE COLUMN " . $newColumn['origField'] . " " . $newColumn['newField'];
$fieldPos = strpos($createTableSQL,$newColumn['origField']);
$definitionStart = $fieldPos + strlen($newColumn['origField']) + 2;
$definitionEnd = strpos($createTableSQL,',',$definitionStart) - 1;
$definition = substr($createTableSQL,$definitionStart,$definitionEnd-$definitionStart+1);
//workaround - if enum type, comma is included.
if (strstr($definition,'enum')) {
//look for comma after enum end bracket.
$commaPos = strpos($createTableSQL, ',', strpos($createTableSQL,')',$definitionStart));
$definition = substr($createTableSQL,$definitionStart,$commaPos-$definitionStart);
}
$dbh->Execute($sql . " " . $definition);
if (strlen($dbh->errorStr)>1) {
echo "<br>************************<br>";
echo "ERROR:<br>";
echo $dbh->errorStr;
echo "<br>************************<br>";
}
}
}