@@ -45,17 +45,27 @@ protected function configure()
45
45
$ this
46
46
->setName ('eav:attributes:remove-unused ' )
47
47
->setDescription ('Remove unused attributes (without values or not assigned to any attribute set ' )
48
- ->addOption ('dry-run ' );
48
+ ->addOption ('dry-run ' )
49
+ ->addOption ('force ' );
49
50
}
50
51
51
- public function execute (InputInterface $ input , OutputInterface $ output )
52
+ public function execute (InputInterface $ input , OutputInterface $ output ): int
52
53
{
53
54
$ 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
+
55
64
$ output ->writeln ('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run. ' );
56
65
$ question = new ConfirmationQuestion ('Are you sure you want to continue? [No] ' , false );
66
+
57
67
if (!$ this ->getHelper ('question ' )->ask ($ input , $ output , $ question )) {
58
- return ;
68
+ return 1 ; // error.
59
69
}
60
70
}
61
71
@@ -70,35 +80,45 @@ public function execute(InputInterface $input, OutputInterface $output)
70
80
->getItems ();
71
81
$ eavAttributeTable = $ this ->resourceConnection ->getTableName ('eav_attribute ' );
72
82
$ eavEntityAttributeTable = $ this ->resourceConnection ->getTableName ('eav_entity_attribute ' );
83
+
73
84
foreach ($ attributes as $ attribute ) {
74
85
$ table = $ this ->resourceConnection ->getTableName ('catalog_product_entity_ ' . $ attribute ->getBackendType ());
75
86
/* Look for attributes that have no values set in products */
76
87
$ attributeValues = (int )$ db ->fetchOne ('SELECT COUNT(*) FROM ' . $ table . ' WHERE attribute_id = ? ' ,
77
88
[$ attribute ->getAttributeId ()]);
89
+
78
90
if ($ attributeValues === 0 ) {
79
91
$ output ->writeln ($ attribute ->getAttributeCode () . ' has ' . $ attributeValues
80
92
. ' values; deleting attribute ' );
93
+
81
94
if (!$ isDryRun ) {
82
95
$ db ->query ('DELETE FROM ' . $ eavAttributeTable . ' WHERE attribute_code = ? ' ,
83
96
$ attribute ->getAttributeCode ());
84
97
}
98
+
85
99
$ deleted ++;
86
100
} else {
87
101
/* Look for attributes that are not assigned to attribute sets */
88
102
$ attributeGroups = (int )$ db ->fetchOne ('SELECT COUNT(*) FROM ' . $ eavEntityAttributeTable
89
103
. ' WHERE attribute_id = ? ' , [$ attribute ->getAttributeId ()]);
104
+
90
105
if ($ attributeGroups === 0 ) {
91
106
$ output ->writeln ($ attribute ->getAttributeCode ()
92
107
. ' is not assigned to any attribute set; deleting attribute and its ' . $ attributeValues
93
108
. ' orphaned value(s) ' );
109
+
94
110
if (!$ isDryRun ) {
95
111
$ db ->query ('DELETE FROM ' . $ eavAttributeTable . ' WHERE attribute_code = ? ' ,
96
112
$ attribute ->getAttributeCode ());
97
113
}
114
+
98
115
$ deleted ++;
99
116
}
100
117
}
101
118
}
119
+
102
120
$ output ->writeln ('Deleted ' . $ deleted . ' attributes. ' );
121
+
122
+ return 0 ; // success.
103
123
}
104
124
}
0 commit comments