@@ -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}
0 commit comments