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

Command: fix dropped fields #532

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions inc/checkdatabasecommand.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* -------------------------------------------------------------------------
* Fields plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Fields.
*
* Fields is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Fields is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fields. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2013-2022 by Fields plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/fields
* -------------------------------------------------------------------------
*/

use Glpi\Console\AbstractCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class PluginFieldsCheckDatabaseCommand extends AbstractCommand
{
protected function configure()
{
$this->setName('plugin:fields:check_database');
$this->setDescription(__('Check database to detect inconsistencies.', 'fields'));
$this->setHelp(
__('This command will chec database to detect following inconsistencies:', 'fields')
. "\n"
. sprintf(
__('- some deleted fields may still be present in database (bug introduced in %s and fixed in version %s)', 'fields'),
'1.15.0',
'1.15.3'
)
);

$this->addOption(
'fix',
null,
InputOption::VALUE_NONE,
__('Use this option to actually fix database', 'fields')
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
// Read option
$fix = $input->getOption('fix');

$dead_fields = PluginFieldsMigration::checkDeadFields($fix);
$dead_fields_count = count($dead_fields, COUNT_RECURSIVE) - count($dead_fields);

// No invalid fields found
if ($dead_fields_count === 0) {
$output->writeln(
'<info>' . __('Everything is in order - no action needed.', 'fields') . '</info>',
);
return Command::SUCCESS;
}

// Indicate which fields will have been or must be deleted
$error = $fix
? sprintf(__('Database was containing %s gone field(s).', 'fields'), $dead_fields_count)
: sprintf(__('Database contains %s gone field(s).', 'fields'), $dead_fields_count);
$output->writeln('<error>' . $error . '</error>', OutputInterface::VERBOSITY_QUIET);

foreach ($dead_fields as $table => $fields) {
foreach ($fields as $field) {
$info = $fix
? sprintf(__('-> "%s.%s" has been deleted.', 'fields'), $table, $field)
: sprintf(__('-> "%s.%s" should be deleted.', 'fields'), $table, $field);
$output->writeln($info);
}
}

// Show extra info in dry-run mode
if (!$fix) {
// Print command to do the actual deletion
$next_command = sprintf(
__('Run "%s" to delete the found field(s).', 'fields'),
sprintf("php bin/console %s --fix", $this->getName())
);
$output->writeln(
'<comment>' . $next_command . '</comment>',
OutputInterface::VERBOSITY_QUIET
);
}

return Command::SUCCESS;
}
}
125 changes: 125 additions & 0 deletions inc/migration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,129 @@ public static function getSQLFields(string $field_name, string $field_type): arr

return $fields;
}

/**
* An issue affected field removal in 1.15.0, 1.15.1 and 1.15.2.
* Using these versions, removing a field from a container would drop the
* field from glpi_plugin_fields_fields but not from the custom container
* table
*
* This function looks into containers tables for fields that
* should have been removed and list them.
* If parameter $fix is true, fields are deleted from database.
*
* @param bool $fix
*
* @return array
*/
public static function checkDeadFields(bool $fix): array
{
/** @var DBMysql $DB */
global $DB;

$dead_fields = [];

// For each existing container
$containers = (new PluginFieldsContainer())->find([]);
foreach ($containers as $row) {
// Get expected fields
$valid_fields = self::getValidFieldsForContainer($row['id']);

// Read itemtypes and container name
$itemtypes = importArrayFromDB($row['itemtypes']);
$name = $row['name'];

// One table to handle per itemtype
foreach ($itemtypes as $itemtype) {
// Build table name
$table = getTableForItemType("PluginFields{$itemtype}{$name}");

if (!$DB->tableExists($table)) {
// Missing table; skip (abnormal)
continue;
}

// Get the actual fields defined in the container table
$found_fields = self::getCustomFieldsInContainerTable($table);

// Compute which fields should be removed
$fields_to_drop = array_diff($found_fields, $valid_fields);

if (count($fields_to_drop) > 0) {
$dead_fields[$table] = $fields_to_drop;
}
}
}

if ($fix) {
$migration = new PluginFieldsMigration(0);

foreach ($dead_fields as $table => $fields) {
foreach ($fields as $field) {
$migration->dropField($table, $field);
}
}

$migration->executeMigration();
}

return $dead_fields;
}

/**
* Get all fields defined for a container in glpi_plugin_fields_fields
*
* @param int $container_id Id of the container
*
* @return array
*/
private static function getValidFieldsForContainer(int $container_id): array
{
$valid_fields = [];

// For each defined fields in the given container
$fields = (new PluginFieldsField())->find(['plugin_fields_containers_id' => $container_id]);
foreach ($fields as $row) {
$fields = self::getSQLFields($row['name'], $row['type']);
array_push($valid_fields, ...array_keys($fields));
}

return $valid_fields;
}

/**
* Get custom fields in a given container table
* This means all fields found in the table expect those defined in
* $basic_fields
*
* @param string $table
*
* @return array
*/
private static function getCustomFieldsInContainerTable(
string $table
): array {
/** @var DBMysql $DB */
global $DB;

// Read table fields
$fields = $DB->listFields($table);

// Reduce to fields name only
$fields = array_column($fields, "Field");

// Remove basic fields
$basic_fields = [
'id',
'items_id',
'itemtype',
'plugin_fields_containers_id',
];
return array_filter(
$fields,
function (string $field) use ($basic_fields) {
return !in_array($field, $basic_fields);
}
);
}
}