Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ModuleInstaller): Add method to rename tables in the DB #623

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 44 additions & 1 deletion setup/moduleinstaller.class.inc.php
Expand Up @@ -259,7 +259,7 @@ public static function MoveColumnInDB($sOrigTable, $sOrigColumn, $sDstTable, $sD
return;
}

if (!CMDBSource::IsTable($sOrigTable) || !CMDBSource::IsField($sOrigTable, $sOrigColumn))
Hipska marked this conversation as resolved.
Show resolved Hide resolved
if (!CMDBSource::IsField($sOrigTable, $sOrigColumn))
{
// Original field is not present
return;
Expand Down Expand Up @@ -291,4 +291,47 @@ public static function MoveColumnInDB($sOrigTable, $sOrigColumn, $sDstTable, $sD
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)
{
Hipska marked this conversation as resolved.
Show resolved Hide resolved
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))
{
// Origin table is not present
return;
}
piRGoif marked this conversation as resolved.
Show resolved Hide resolved

if (CMDBSource::IsTable($sDstTable))
{
// Destination table is already present
return;
Molkobain marked this conversation as resolved.
Show resolved Hide resolved
}

$sQueryRename = sprintf(/** @lang MariaDB */ "RENAME TABLE `%s` TO `%s`;", $sOrigTable, $sDstTable);
CMDBSource::Query($sQueryRename);
Hipska marked this conversation as resolved.
Show resolved Hide resolved

CMDBSource::CacheReset($sOrigTable);
}
}
Expand Up @@ -241,4 +241,30 @@ public function testMoveColumnInDB_MoveMultipleTable(): void
$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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also test :

  • orig and dest on the same existing table name
  • orig and dest on the same non existing table name
  • orig on a non existing table name
  • dest on an existing table name

This may be easier to code using a dataprovider returning orig and dest names, and expected result ?

{
$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');
Hipska marked this conversation as resolved.
Show resolved Hide resolved

// Revert
ModuleInstallerAPI::RenameTableInDB($sDstTable, $sOrigTable);
$this->assertEquals($aOrigTableInfo, CMDBSource::GetTableInfo($sOrigTable));
}
}