From 7e1b1779a94c151cb9e6c12b10630af03245bb40 Mon Sep 17 00:00:00 2001 From: Thomas Casteleyn Date: Thu, 14 Nov 2024 14:24:45 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Add=20faster=20way=20to=20?= =?UTF-8?q?rename=20column=20on=20the=20same=20table=20(#676)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add faster way to rename column on the same table --- setup/moduleinstaller.class.inc.php | 11 +++++ .../setup/ModuleInstallerAPITest.php | 44 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/setup/moduleinstaller.class.inc.php b/setup/moduleinstaller.class.inc.php index 0a22479dc7..31e339a7f1 100644 --- a/setup/moduleinstaller.class.inc.php +++ b/setup/moduleinstaller.class.inc.php @@ -272,6 +272,17 @@ public static function MoveColumnInDB($sOrigTable, $sOrigColumn, $sDstTable, $sD 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); diff --git a/tests/php-unit-tests/unitary-tests/setup/ModuleInstallerAPITest.php b/tests/php-unit-tests/unitary-tests/setup/ModuleInstallerAPITest.php index f243c0aaa2..19232f9731 100644 --- a/tests/php-unit-tests/unitary-tests/setup/ModuleInstallerAPITest.php +++ b/tests/php-unit-tests/unitary-tests/setup/ModuleInstallerAPITest.php @@ -240,4 +240,48 @@ public function testMoveColumnInDB_MoveMultipleTable(): void $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( + <<assertEquals($sOrigValue, $sDstValue, "Data was not moved as expected"); + } }