From a749a4654efeddb215200b790c52a74fdb2d3241 Mon Sep 17 00:00:00 2001 From: Thomas Casteleyn Date: Wed, 24 Jul 2024 10:51:07 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B07697=20-=20Add=20method=20to=20rename=20?= =?UTF-8?q?DB=20table=20during=20setup=20(#623)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup/moduleinstaller.class.inc.php | 43 +++++++++++++++++++ .../setup/ModuleInstallerAPITest.php | 26 +++++++++++ 2 files changed, 69 insertions(+) diff --git a/setup/moduleinstaller.class.inc.php b/setup/moduleinstaller.class.inc.php index fc6aa78b5..0a22479dc 100644 --- a/setup/moduleinstaller.class.inc.php +++ b/setup/moduleinstaller.class.inc.php @@ -291,4 +291,47 @@ abstract class ModuleInstallerAPI CMDBSource::CacheReset($sDstTable); } + /** + * Rename a table providing: + * - The original name exists + * - The destination name does not exist + * + * @param string $sOrigTable + * @param string $sDstTable + * + * @return void + * @throws CoreException + * @throws CoreUnexpectedValue + * @throws MySQLException + */ + public static function RenameTableInDB(string $sOrigTable, string $sDstTable) + { + if ($sOrigTable == $sDstTable) + { + throw new CoreUnexpectedValue("Origin table and destination table are the same"); + } + + if (!MetaModel::DBExists(false)) + { + // Install from scratch, no migration + return; + } + + if (!CMDBSource::IsTable($sOrigTable)) + { + SetupLog::Warning(sprintf('Rename table in DB - Origin table %s doesn\'t exist', $sOrigTable)); + return; + } + + if (CMDBSource::IsTable($sDstTable)) + { + SetupLog::Warning(sprintf('Rename table in DB - Destination table %s already exists', $sDstTable)); + return; + } + + $sQueryRename = sprintf(/** @lang MariaDB */ "RENAME TABLE `%s` TO `%s`;", $sOrigTable, $sDstTable); + CMDBSource::Query($sQueryRename); + + CMDBSource::CacheReset($sOrigTable); + } } diff --git a/tests/php-unit-tests/unitary-tests/setup/ModuleInstallerAPITest.php b/tests/php-unit-tests/unitary-tests/setup/ModuleInstallerAPITest.php index 0c91246e7..433359b7d 100644 --- a/tests/php-unit-tests/unitary-tests/setup/ModuleInstallerAPITest.php +++ b/tests/php-unit-tests/unitary-tests/setup/ModuleInstallerAPITest.php @@ -241,4 +241,30 @@ SQL $this->assertEquals('from table 2', $sFromTable2Data, "Data was not moved as expected"); } + /** + * Test that the table has been renamed + * + * @covers ModuleInstallerAPI::RenameTableInDB + * + * @return void + * @throws \CoreException + * @throws \MySQLException + */ + public function testRenameTableInDB() + { + $sOrigTable = MetaModel::DBGetTable('Person'); + $aOrigTableInfo = CMDBSource::GetTableInfo($sOrigTable); + $this->assertNotEmpty($aOrigTableInfo, 'Origin table does not exist'); + + $sDstTable = static::$sWorkTable; + $this->assertFalse(CMDBSource::IsTable($sDstTable), 'Work table already exists'); + + ModuleInstallerAPI::RenameTableInDB($sOrigTable, $sDstTable); + + $this->assertEquals($aOrigTableInfo, CMDBSource::GetTableInfo($sDstTable), 'Table was not renamed'); + + // Revert + ModuleInstallerAPI::RenameTableInDB($sDstTable, $sOrigTable); + $this->assertEquals($aOrigTableInfo, CMDBSource::GetTableInfo($sOrigTable)); + } }