N°7130 - Allows to ignore existing column field in setup's data migration method (#597)

* Allows to ignore existing column field in setup's data migration method

* Update setup/moduleinstaller.class.inc.php

* N°7130 - Fix typo in variable extraction

* N°7130 - Add unit tests

* Update setup/moduleinstaller.class.inc.php

---------

Co-authored-by: Molkobain <lajarige.guillaume@free.fr>
This commit is contained in:
Stephen Abello
2024-01-09 09:43:22 +01:00
committed by GitHub
parent bfb05d331e
commit 73c989ee67
2 changed files with 148 additions and 9 deletions

View File

@@ -242,11 +242,15 @@ abstract class ModuleInstallerAPI
* @param $sOrigColumn
* @param $sDstTable
* @param $sDstColumn
* @param bool $bIgnoreExistingDstColumn
*
* @throws \MySQLException
* @throws \CoreException
* @throws \MySQLException
* @throws \MySQLHasGoneAwayException
*
* @since 3.2.0 N°7130 Add parameter $bIgnoreExistingDstColumn
*/
public static function MoveColumnInDB($sOrigTable, $sOrigColumn, $sDstTable, $sDstColumn)
public static function MoveColumnInDB($sOrigTable, $sOrigColumn, $sDstTable, $sDstColumn, $bIgnoreExistingDstColumn = false)
{
if (!MetaModel::DBExists(false))
{
@@ -259,17 +263,20 @@ abstract class ModuleInstallerAPI
// Original field is not present
return;
}
if (!CMDBSource::IsTable($sDstTable) || CMDBSource::IsField($sDstTable, $sDstColumn))
$bDstTableFieldExists = CMDBSource::IsField($sDstTable, $sDstColumn);
if (!CMDBSource::IsTable($sDstTable) || ($bDstTableFieldExists && !$bIgnoreExistingDstColumn))
{
// Destination field is already created
// Destination field is already created, and we are not ignoring it
return;
}
// Create the destination field
$sSpec = CMDBSource::GetFieldSpec($sOrigTable, $sOrigColumn);
$sQueryAdd = "ALTER TABLE `{$sDstTable}` ADD `{$sDstColumn}` {$sSpec}";
CMDBSource::Query($sQueryAdd);
// Create the destination field if necessary
if($bDstTableFieldExists === false){
$sSpec = CMDBSource::GetFieldSpec($sOrigTable, $sOrigColumn);
$sQueryAdd = "ALTER TABLE `{$sDstTable}` ADD `{$sDstColumn}` {$sSpec}";
CMDBSource::Query($sQueryAdd);
}
// Copy the data
$sQueryUpdate = "UPDATE `{$sDstTable}` AS d LEFT JOIN `{$sOrigTable}` AS o ON d.id = o.id SET d.`{$sDstColumn}` = o.`{$sOrigColumn}` WHERE 1";