Skip to content

Commit d16dbeb

Browse files
authored
Merge pull request #17 from fredden/force
Add --force option
2 parents a67181f + 6ba61e9 commit d16dbeb

6 files changed

+95
-20
lines changed

Console/Command/CleanUpAttributesAndValuesWithoutParentCommand.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,27 @@ protected function configure()
4242
$this
4343
->setName('eav:clean:attributes-and-values-without-parent')
4444
->setDescription($description)
45-
->addOption('dry-run');
45+
->addOption('dry-run')
46+
->addOption('force');
4647
}
4748

48-
public function execute(InputInterface $input, OutputInterface $output)
49+
public function execute(InputInterface $input, OutputInterface $output): int
4950
{
5051
$isDryRun = $input->getOption('dry-run');
52+
$isForce = $input->getOption('force');
53+
54+
if (!$isDryRun && !$isForce) {
55+
if (!$input->isInteractive()) {
56+
$output->writeln('ERROR: neither --dry-run nor --force options were supplied, and we are not running interactively.');
57+
58+
return 1; // error.
59+
}
5160

52-
if (!$isDryRun && $input->isInteractive()) {
5361
$output->writeln('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.');
5462
$question = new ConfirmationQuestion('Are you sure you want to continue? [No] ', false);
5563

5664
if (!$this->getHelper('question')->ask($input, $output, $question)) {
57-
return;
65+
return 1; // error.
5866
}
5967
}
6068

@@ -66,12 +74,14 @@ public function execute(InputInterface $input, OutputInterface $output)
6674
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
6775
AddressMetadataInterface::ENTITY_TYPE_ADDRESS
6876
];
77+
6978
foreach ($entityTypeCodes as $code) {
7079
$entityType = $this->eavEntityTypeCollectionFactory
7180
->create()
7281
->addFieldToFilter('entity_type_code', $code)
7382
->getFirstItem();
7483
$output->writeln("<info>Cleaning values for $code</info>");
84+
7585
foreach ($types as $type) {
7686
$eavTable = $this->resourceConnection->getTableName('eav_attribute');
7787
$entityValueTable = $this->resourceConnection->getTableName($code . '_entity_' . $type);
@@ -86,5 +96,7 @@ public function execute(InputInterface $input, OutputInterface $output)
8696
}
8797
}
8898
}
99+
100+
return 0; // success.
89101
}
90102
}

Console/Command/RemoveUnusedAttributesCommand.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,27 @@ protected function configure()
4545
$this
4646
->setName('eav:attributes:remove-unused')
4747
->setDescription('Remove unused attributes (without values or not assigned to any attribute set')
48-
->addOption('dry-run');
48+
->addOption('dry-run')
49+
->addOption('force');
4950
}
5051

51-
public function execute(InputInterface $input, OutputInterface $output)
52+
public function execute(InputInterface $input, OutputInterface $output): int
5253
{
5354
$isDryRun = $input->getOption('dry-run');
54-
if (!$isDryRun && $input->isInteractive()) {
55+
$isForce = $input->getOption('force');
56+
57+
if (!$isDryRun && !$isForce) {
58+
if (!$input->isInteractive()) {
59+
$output->writeln('ERROR: neither --dry-run nor --force options were supplied, and we are not running interactively.');
60+
61+
return 1; // error.
62+
}
63+
5564
$output->writeln('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.');
5665
$question = new ConfirmationQuestion('Are you sure you want to continue? [No] ', false);
66+
5767
if (!$this->getHelper('question')->ask($input, $output, $question)) {
58-
return;
68+
return 1; // error.
5969
}
6070
}
6171

@@ -70,35 +80,45 @@ public function execute(InputInterface $input, OutputInterface $output)
7080
->getItems();
7181
$eavAttributeTable = $this->resourceConnection->getTableName('eav_attribute');
7282
$eavEntityAttributeTable = $this->resourceConnection->getTableName('eav_entity_attribute');
83+
7384
foreach ($attributes as $attribute) {
7485
$table = $this->resourceConnection->getTableName('catalog_product_entity_' . $attribute->getBackendType());
7586
/* Look for attributes that have no values set in products */
7687
$attributeValues = (int)$db->fetchOne('SELECT COUNT(*) FROM ' . $table . ' WHERE attribute_id = ?',
7788
[$attribute->getAttributeId()]);
89+
7890
if ($attributeValues === 0) {
7991
$output->writeln($attribute->getAttributeCode() . ' has ' . $attributeValues
8092
. ' values; deleting attribute');
93+
8194
if (!$isDryRun) {
8295
$db->query('DELETE FROM ' . $eavAttributeTable . ' WHERE attribute_code = ?',
8396
$attribute->getAttributeCode());
8497
}
98+
8599
$deleted++;
86100
} else {
87101
/* Look for attributes that are not assigned to attribute sets */
88102
$attributeGroups = (int)$db->fetchOne('SELECT COUNT(*) FROM ' . $eavEntityAttributeTable
89103
. ' WHERE attribute_id = ?', [$attribute->getAttributeId()]);
104+
90105
if ($attributeGroups === 0) {
91106
$output->writeln($attribute->getAttributeCode()
92107
. ' is not assigned to any attribute set; deleting attribute and its ' . $attributeValues
93108
. ' orphaned value(s)');
109+
94110
if (!$isDryRun) {
95111
$db->query('DELETE FROM ' . $eavAttributeTable . ' WHERE attribute_code = ?',
96112
$attribute->getAttributeCode());
97113
}
114+
98115
$deleted++;
99116
}
100117
}
101118
}
119+
102120
$output->writeln('Deleted ' . $deleted . ' attributes.');
121+
122+
return 0; // success.
103123
}
104124
}

Console/Command/RemoveUnusedMediaCommand.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,31 @@ protected function configure()
3939
$this
4040
->setName('eav:media:remove-unused')
4141
->setDescription('Remove unused product images')
42-
->addOption('dry-run');
42+
->addOption('dry-run')
43+
->addOption('force');
4344
}
4445

45-
public function execute(InputInterface $input, OutputInterface $output): void
46+
public function execute(InputInterface $input, OutputInterface $output): int
4647
{
4748
$fileSize = 0;
4849
$countFiles = 0;
4950
$isDryRun = $input->getOption('dry-run');
51+
$isForce = $input->getOption('force');
52+
53+
if (!$isDryRun && !$isForce) {
54+
if (!$input->isInteractive()) {
55+
$output->writeln('ERROR: neither --dry-run nor --force options were supplied, and we are not running interactively.');
56+
57+
return 1; // error.
58+
}
5059

51-
if (!$isDryRun && $input->isInteractive()) {
5260
$output->writeln(
5361
'<comment>WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.</comment>'
5462
);
5563
$question = new ConfirmationQuestion('<comment>Are you sure you want to continue? [No]</comment>', false);
64+
5665
if (!$this->getHelper('question')->ask($input, $output, $question)) {
57-
return;
66+
return 1; // error.
5867
}
5968
}
6069

@@ -89,6 +98,7 @@ public function execute(InputInterface $input, OutputInterface $output): void
8998

9099
$fileSize += filesize($file);
91100
$countFiles++;
101+
92102
if (!$isDryRun) {
93103
unlink($file);
94104
$output->writeln('## REMOVING: ' . $filePath . ' ##');
@@ -98,14 +108,18 @@ public function execute(InputInterface $input, OutputInterface $output): void
98108
}
99109

100110
$this->printResult($output, $isDryRun, $countFiles, $fileSize);
111+
112+
return 0; // success.
101113
}
102114

103115
private function printResult(OutputInterface $output, $isDryRun, int $countFiles, int $filesize): void
104116
{
105117
$actionName = 'Deleted';
118+
106119
if ($isDryRun) {
107120
$actionName = 'Would delete';
108121
}
122+
109123
$fileSizeInMB = number_format($filesize / 1024 / 1024, '2');
110124

111125
$output->writeln("<info>{$actionName} {$countFiles} unused images. {$fileSizeInMB} MB</info>");

Console/Command/RestoreUseDefaultConfigValueCommand.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,27 @@ protected function configure()
3939
$this
4040
->setName('eav:config:restore-use-default-value')
4141
->setDescription($description)
42-
->addOption('dry-run');
42+
->addOption('dry-run')
43+
->addOption('force');
4344
}
4445

45-
public function execute(InputInterface $input, OutputInterface $output)
46+
public function execute(InputInterface $input, OutputInterface $output): int
4647
{
4748
$isDryRun = $input->getOption('dry-run');
49+
$isForce = $input->getOption('force');
50+
51+
if (!$isDryRun && !$isForce) {
52+
if (!$input->isInteractive()) {
53+
$output->writeln('ERROR: neither --dry-run nor --force options were supplied, and we are not running interactively.');
54+
55+
return 1; // error.
56+
}
4857

49-
if (!$isDryRun && $input->isInteractive()) {
5058
$output->writeln('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.');
5159
$question = new ConfirmationQuestion('Are you sure you want to continue? [No] ', false);
5260

5361
if (!$this->getHelper('question')->ask($input, $output, $question)) {
54-
return;
62+
return 1; // error.
5563
}
5664
}
5765

@@ -61,18 +69,22 @@ public function execute(InputInterface $input, OutputInterface $output)
6169
$tableName = $this->resourceConnection->getTableName('core_config_data');
6270
$configData = $db->fetchAll('SELECT DISTINCT path, value FROM ' . $tableName
6371
. ' WHERE scope_id = 0');
72+
6473
foreach ($configData as $config) {
6574
$count = $db->fetchOne('SELECT COUNT(*) FROM ' . $tableName
6675
. ' WHERE path = ? AND BINARY value = ?', [$config['path'], $config['value']]);
76+
6777
if ($count > 1) {
6878
$output->writeln('Config path ' . $config['path'] . ' with value ' . $config['value'] . ' has ' . $count
6979
. ' values; deleting non-default values');
80+
7081
if (!$isDryRun) {
7182
$db->query(
7283
'DELETE FROM ' . $tableName . ' WHERE path = ? AND BINARY value = ? AND scope_id != ?',
7384
[$config['path'], $config['value'], 0]
7485
);
7586
}
87+
7688
$removedConfigValues += ($count - 1);
7789
}
7890

@@ -98,6 +110,8 @@ public function execute(InputInterface $input, OutputInterface $output)
98110
}
99111

100112
$output->writeln('Removed ' . $removedConfigValues . ' values from core_config_data table.');
113+
114+
return 0; // success.
101115
}
102116

103117
/**

Console/Command/RestoreUseDefaultValueCommand.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ protected function configure()
3939
->setName('eav:attributes:restore-use-default-value')
4040
->setDescription($description)
4141
->addOption('dry-run')
42+
->addOption('force')
4243
->addOption(
4344
'entity',
4445
null,
@@ -48,23 +49,30 @@ protected function configure()
4849
);
4950
}
5051

51-
public function execute(InputInterface $input, OutputInterface $output)
52+
public function execute(InputInterface $input, OutputInterface $output): int
5253
{
5354
$isDryRun = $input->getOption('dry-run');
55+
$isForce = $input->getOption('force');
5456
$entity = $input->getOption('entity');
5557

5658
if (!in_array($entity, ['product', 'category'])) {
5759
$output->writeln('Please specify the entity with --entity. Possible options are product or category');
5860

59-
return;
61+
return 1;
6062
}
6163

62-
if (!$isDryRun && $input->isInteractive()) {
64+
if (!$isDryRun && !$isForce) {
65+
if (!$input->isInteractive()) {
66+
$output->writeln('ERROR: neither --dry-run nor --force options were supplied, and we are not running interactively.');
67+
68+
return 1; // error.
69+
}
70+
6371
$output->writeln('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.');
6472
$question = new ConfirmationQuestion('Are you sure you want to continue? [No] ', false);
6573

6674
if (!$this->getHelper('question')->ask($input, $output, $question)) {
67-
return;
75+
return 1; // error.
6876
}
6977
}
7078

@@ -101,9 +109,11 @@ public function execute(InputInterface $input, OutputInterface $output)
101109
. $result['value_id']
102110
. ' for attribute ' . $row['attribute_id'] . ' in table ' . $fullTableName
103111
);
112+
104113
if (!isset($counts[$row['attribute_id']])) {
105114
$counts[$row['attribute_id']] = 0;
106115
}
116+
107117
$counts[$row['attribute_id']]++;
108118
}
109119
}
@@ -127,5 +137,7 @@ public function execute(InputInterface $input, OutputInterface $output)
127137
$output->writeln('There were no attribute values to clean up');
128138
}
129139
}
140+
141+
return 0; // success.
130142
}
131143
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Run `bin/magento` in the Magento 2 root and look for the `eav:` commands.
1717
## Dry run
1818
Use `--dry-run` to check result without modifying data.
1919

20+
## Force
21+
Use `--force` to skip the confirmation prompt before modifying data.
22+
2023
## Installation
2124
Installation with composer:
2225

0 commit comments

Comments
 (0)