mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-12 23:14:18 +01:00
⚡️ Add faster way to rename column on the same table (#676)
* Add faster way to rename column on the same table
This commit is contained in:
@@ -272,6 +272,17 @@ abstract class ModuleInstallerAPI
|
||||
return;
|
||||
}
|
||||
|
||||
// Simple rename
|
||||
if ($sOrigTable === $sDstTable && !$bDstTableFieldExists)
|
||||
{
|
||||
$sFieldSpec = CMDBSource::GetFieldSpec($sOrigTable, $sOrigColumn);
|
||||
$sQueryRename = /** @lang MariaDB */ "ALTER TABLE `{$sOrigTable}` CHANGE `{$sOrigColumn}` `{$sDstColumn}` {$sFieldSpec};";
|
||||
CMDBSource::Query($sQueryRename);
|
||||
|
||||
CMDBSource::CacheReset($sOrigTable);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the destination field if necessary
|
||||
if($bDstTableFieldExists === false){
|
||||
$sSpec = CMDBSource::GetFieldSpec($sOrigTable, $sOrigColumn);
|
||||
|
||||
@@ -240,4 +240,48 @@ SQL
|
||||
$this->assertEquals('from table 1', $sFromTable1Data, "Data was not moved as expected");
|
||||
$this->assertEquals('from table 2', $sFromTable2Data, "Data was not moved as expected");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that moving columns from/to the same table works
|
||||
*
|
||||
* @covers \ModuleInstallerAPI::MoveColumnInDB
|
||||
*
|
||||
* @return void
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \MySQLException
|
||||
* @throws \MySQLHasGoneAwayException
|
||||
* @throws \MySQLQueryHasNoResultException
|
||||
*/
|
||||
public function testMoveColumnInDB_SameTable(): void
|
||||
{
|
||||
// Info from the original table
|
||||
$sOrigClass = "Person";
|
||||
$sOrigAttCode = "first_name";
|
||||
[$sOrigTable, $sOrigColName] = $this->GetInfoFromTable($sOrigClass, $sOrigAttCode);
|
||||
|
||||
// Info for the destination column
|
||||
$sDstNonExistingColName = "non_existing_column";
|
||||
|
||||
// Save value from original table as a reference
|
||||
$oPerson = MetaModel::GetObject($sOrigClass, 1);
|
||||
$sOrigValue = $oPerson->Get($sOrigAttCode);
|
||||
|
||||
// Try to move data
|
||||
ModuleInstallerAPI::MoveColumnInDB($sOrigTable, $sOrigColName, $sOrigTable, $sDstNonExistingColName);
|
||||
|
||||
// Check if data was actually moved
|
||||
// - Either way, the column should exist
|
||||
$sDstValue = CMDBSource::QueryToScalar(
|
||||
<<<SQL
|
||||
SELECT `{$sDstNonExistingColName}` FROM `{$sOrigTable}` WHERE `id` = 1
|
||||
LIMIT 1
|
||||
SQL
|
||||
);
|
||||
|
||||
// Put data back in the original table
|
||||
ModuleInstallerAPI::MoveColumnInDB($sOrigTable, $sDstNonExistingColName, $sOrigTable, $sOrigColName);
|
||||
|
||||
$this->assertEquals($sOrigValue, $sDstValue, "Data was not moved as expected");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user