Allow database migration from legacy versions even if the perms_label column is missing in the groups table

This fixes issue #366 and #67
This commit is contained in:
Jan Böhmer 2023-09-09 23:04:50 +02:00
parent 55943f5d8f
commit 4bed50d894
2 changed files with 24 additions and 0 deletions

View file

@ -69,6 +69,12 @@ final class Version20190902140506 extends AbstractMultiPlatformMigration
//For attachments
$this->addSql('DELETE FROM `attachements` WHERE attachements.class_name = "Part" AND (SELECT COUNT(parts.id) FROM parts WHERE parts.id = attachements.element_id) = 0;');
//Add perms_labels column to groups table if not existing (it was not created in certain legacy versions)
//This prevents the migration failing (see https://github.com/Part-DB/Part-DB-server/issues/366 and https://github.com/Part-DB/Part-DB-server/issues/67)
if (!$this->doesColumnExist('groups', 'perms_labels')) {
$this->addSql('ALTER TABLE `groups` ADD `perms_labels` SMALLINT NOT NULL AFTER `perms_tools`');
}
/**************************************************************************************************************
* Doctrine generated SQL
**************************************************************************************************************/

View file

@ -132,6 +132,24 @@ abstract class AbstractMultiPlatformMigration extends AbstractMigration
return $result > 0;
}
/**
* Checks if a column exists in a table.
* @return bool Returns true, if the column exists
* @throws Exception
*/
public function doesColumnExist(string $table, string $column_name): bool
{
$db_type = $this->getDatabaseType();
if ($db_type !== 'mysql') {
throw new \RuntimeException('This method is only supported for MySQL/MariaDB databases!');
}
$sql = "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column_name'";
$result = (int) $this->connection->fetchOne($sql);
return $result > 0;
}
/**
* Returns the database type of the used database.
* @return string|null Returns 'mysql' for MySQL/MariaDB and 'sqlite' for SQLite. Returns null if unknown type