Skip to content

Commit 21d8a53

Browse files
committed
Enhance command output; make command more generic
1 parent 336b0c9 commit 21d8a53

File tree

2 files changed

+65
-53
lines changed

2 files changed

+65
-53
lines changed

inc/fixdroppedfieldscommand.class.php inc/checkdatabasecommand.class.php

+36-29
Original file line numberDiff line numberDiff line change
@@ -34,65 +34,72 @@
3434
use Symfony\Component\Console\Input\InputOption;
3535
use Symfony\Component\Console\Output\OutputInterface;
3636

37-
class PluginFieldsFixDroppedFieldsCommand extends AbstractCommand
37+
class PluginFieldsCheckDatabaseCommand extends AbstractCommand
3838
{
3939
protected function configure()
4040
{
41-
$this->setName('plugin:fields:fixdroppedfields');
42-
$this->setAliases(['fields:fixdroppedfields']);
43-
$this->setDescription(
44-
'Remove fields that were wrongly kept in the database following an '
45-
. 'issue introduced in 1.15.0 and fixed in 1.15.3.'
41+
$this->setName('plugin:fields:check_database');
42+
$this->setDescription(__('Check database to detect inconsistencies.', 'fields'));
43+
$this->setHelp(
44+
__('This command will chec database to detect following inconsistencies:', 'fields')
45+
. "\n"
46+
. sprintf(
47+
__('- some deleted fields may still be present in database (bug introduced in %s and fixed in version %s)', 'fields'),
48+
'1.15.0',
49+
'1.15.3'
50+
)
4651
);
4752

4853
$this->addOption(
49-
"delete",
54+
'fix',
5055
null,
5156
InputOption::VALUE_NONE,
52-
"Use this option to actually delete data"
57+
__('Use this option to actually fix database', 'fields')
5358
);
5459
}
5560

5661
protected function execute(InputInterface $input, OutputInterface $output)
5762
{
5863
// Read option
59-
$delete = $input->getOption("delete");
64+
$fix = $input->getOption('fix');
6065

61-
$fields = PluginFieldsMigration::fixDroppedFields(!$delete);
66+
$dead_fields = PluginFieldsMigration::checkDeadFields($fix);
67+
$dead_fields_count = count($dead_fields, COUNT_RECURSIVE) - count($dead_fields);
6268

6369
// No invalid fields found
64-
if (!count($fields)) {
70+
if ($dead_fields_count === 0) {
6571
$output->writeln(
66-
__("Everything is in order - no action needed.", 'fields'),
72+
'<info>' . __('Everything is in order - no action needed.', 'fields') . '</info>',
6773
);
6874
return Command::SUCCESS;
6975
}
7076

7177
// Indicate which fields will have been or must be deleted
72-
foreach ($fields as $field) {
73-
if ($delete) {
74-
$info = sprintf(__("-> %s was deleted.", 'fields'), $field);
75-
} else {
76-
$info = sprintf(__("-> %s must be deleted.", 'fields'), $field);
77-
}
78+
$error = $fix
79+
? sprintf(__('Database was containing %s gone field(s).', 'fields'), $dead_fields_count)
80+
: sprintf(__('Database contains %s gone field(s).', 'fields'), $dead_fields_count);
81+
$output->writeln('<error>' . $error . '</error>', OutputInterface::VERBOSITY_QUIET);
7882

79-
$output->writeln($info);
83+
foreach ($dead_fields as $table => $fields) {
84+
foreach ($fields as $field) {
85+
$info = $fix
86+
? sprintf(__('-> "%s.%s" has been deleted.', 'fields'), $table, $field)
87+
: sprintf(__('-> "%s.%s" should be deleted.', 'fields'), $table, $field);
88+
$output->writeln($info);
89+
}
8090
}
8191

8292
// Show extra info in dry-run mode
83-
if (!$delete) {
84-
$fields_found = sprintf(
85-
__("%s field(s) need to be deleted.", 'fields'),
86-
count($fields)
87-
);
88-
$output->writeln($fields_found);
89-
93+
if (!$fix) {
9094
// Print command to do the actual deletion
9195
$next_command = sprintf(
92-
__("Run \"%s\" to delete the found field(s).", 'fields'),
93-
"php bin/console plugin:fields:fixdroppedfields --delete"
96+
__('Run "%s" to delete the found field(s).', 'fields'),
97+
sprintf("php bin/console %s --fix", $this->getName())
98+
);
99+
$output->writeln(
100+
'<comment>' . $next_command . '</comment>',
101+
OutputInterface::VERBOSITY_QUIET
94102
);
95-
$output->writeln($next_command);
96103
}
97104

98105
return Command::SUCCESS;

inc/migration.class.php

+29-24
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,24 @@ public static function getSQLFields(string $field_name, string $field_type): arr
8888
* field from glpi_plugin_fields_fields but not from the custom container
8989
* table
9090
*
91-
* This function find looks into containers tables for these fields that
92-
* should have been removed and list them (dry_run = true) or delete them
93-
* (dry_run = false)
91+
* This function looks into containers tables for fields that
92+
* should have been removed and list them.
93+
* If parameter $fix is true, fields are deleted from database.
9494
*
95-
* @param bool $dry_run
95+
* @param bool $fix
9696
*
9797
* @return array
9898
*/
99-
public static function fixDroppedFields(bool $dry_run = true): array
99+
public static function checkDeadFields(bool $fix): array
100100
{
101101
/** @var DBMysql $DB */
102102
global $DB;
103103

104-
// Keep track of dropped fields
105-
$dropped = [];
104+
$dead_fields = [];
106105

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

@@ -129,21 +129,25 @@ public static function fixDroppedFields(bool $dry_run = true): array
129129
// Compute which fields should be removed
130130
$fields_to_drop = array_diff($found_fields, $valid_fields);
131131

132-
// Drop fields
133-
$migration = new PluginFieldsMigration(0);
134-
135-
foreach ($fields_to_drop as $field) {
136-
$dropped[] = "$table.$field";
137-
$migration->dropField($table, $field);
132+
if (count($fields_to_drop) > 0) {
133+
$dead_fields[$table] = $fields_to_drop;
138134
}
135+
}
136+
}
139137

140-
if (!$dry_run) {
141-
$migration->migrationOneTable($table);
138+
if ($fix) {
139+
$migration = new PluginFieldsMigration(0);
140+
141+
foreach ($dead_fields as $table => $fields) {
142+
foreach ($fields as $field) {
143+
$migration->dropField($table, $field);
142144
}
143145
}
146+
147+
$migration->executeMigration();
144148
}
145149

146-
return $dropped;
150+
return $dead_fields;
147151
}
148152

149153
/**
@@ -155,15 +159,11 @@ public static function fixDroppedFields(bool $dry_run = true): array
155159
*/
156160
private static function getValidFieldsForContainer(int $container_id): array
157161
{
158-
// Keep track of fields found
159162
$valid_fields = [];
160163

161164
// For each defined fields in the given container
162-
foreach (
163-
(new PluginFieldsField())->find([
164-
'plugin_fields_containers_id' => $container_id
165-
]) as $row
166-
) {
165+
$fields = (new PluginFieldsField())->find(['plugin_fields_containers_id' => $container_id]);
166+
foreach ($fields as $row) {
167167
$fields = self::getSQLFields($row['name'], $row['type']);
168168
array_push($valid_fields, ...array_keys($fields));
169169
}
@@ -199,6 +199,11 @@ private static function getCustomFieldsInContainerTable(
199199
'itemtype',
200200
'plugin_fields_containers_id',
201201
];
202-
return array_filter($fields, fn($f) => !in_array($f, $basic_fields));
202+
return array_filter(
203+
$fields,
204+
function (string $field) use ($basic_fields) {
205+
return !in_array($field, $basic_fields);
206+
}
207+
);
203208
}
204209
}

0 commit comments

Comments
 (0)