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 3 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
39 changes: 38 additions & 1 deletion setup/moduleinstaller.class.inc.php
Expand Up @@ -258,7 +258,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 @@ -290,4 +290,41 @@ 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 $sOrigTable string
* @param $sDstTable string
Hipska marked this conversation as resolved.
Show resolved Hide resolved
*
* @return void
* @throws CoreException
* @throws MySQLException
*/
public static function RenameTableInDB(string $sOrigTable, string $sDstTable)
{
Hipska marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -128,4 +128,26 @@ public function MoveColumnInDB_IgnoreExistingDstColumnParamProvider(): array
];
}

/**
* Test that the table has been renamed
*
* @covers \ModuleInstallerAPI::MoveColumnInDB
*
* @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
}
}