@@ -41,7 +41,7 @@ public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $
4141	/** 
4242	 * Clean up duplicate categories 
4343	 */ 
44- 	private  function  cleanupDuplicateCategories (IOutput $ output
44+ 	private  function  cleanupDuplicateCategories (IOutput $ output:  void  {
4545		$ outputinfo ('Starting cleanup of duplicate vcategory records... ' );
4646
4747		// Find all categories, ordered to identify duplicates 
@@ -74,6 +74,8 @@ private function cleanupDuplicateCategories(IOutput $output) {
7474
7575			$ outputinfo ("Found duplicate: keeping ID  $ keepId, removing ID  $ categoryId );
7676
77+ 			$ this cleanupDuplicateAssignments ($ output$ categoryId$ keepId
78+ 
7779			// Update object references 
7880			$ updateQb$ this connection ->getQueryBuilder ();
7981			$ updateQbupdate ('vcategory_to_object ' )
@@ -103,4 +105,41 @@ private function cleanupDuplicateCategories(IOutput $output) {
103105			$ outputinfo ("Duplicate cleanup completed - processed  $ duplicateCount duplicates " );
104106		}
105107	}
108+ 
109+ 	/** 
110+ 	 * Clean up duplicate assignments 
111+ 	 * That will delete rows with $categoryId when there is the same row with $keepId 
112+ 	 */ 
113+ 	private  function  cleanupDuplicateAssignments (IOutput $ outputint  $ categoryIdint  $ keepIdvoid  {
114+ 		$ selectQb$ this connection ->getQueryBuilder ();
115+ 		$ selectQbselect ('o1.* ' )
116+ 			->from ('vcategory_to_object ' , 'o1 ' )
117+ 			->join (
118+ 				'o1 ' , 'vcategory_to_object ' , 'o2 ' ,
119+ 				$ selectQbexpr ()->andX (
120+ 					$ selectQbexpr ()->eq ('o1.type ' , 'o2.type ' ),
121+ 					$ selectQbexpr ()->eq ('o1.objid ' , 'o2.objid ' ),
122+ 				)
123+ 			)
124+ 			->where ($ selectQbexpr ()->eq ('o1.categoryid ' , $ selectQbcreateNamedParameter ($ categoryId
125+ 			->andWhere ($ selectQbexpr ()->eq ('o2.categoryid ' , $ selectQbcreateNamedParameter ($ keepId
126+ 
127+ 		$ deleteQb$ this connection ->getQueryBuilder ();
128+ 		$ deleteQbdelete ('vcategory_to_object ' )
129+ 			->where ($ deleteQbexpr ()->eq ('objid ' , $ deleteQbcreateParameter ('objid ' )))
130+ 			->andWhere ($ deleteQbexpr ()->eq ('categoryid ' , $ deleteQbcreateParameter ('categoryid ' )))
131+ 			->andWhere ($ deleteQbexpr ()->eq ('type ' , $ deleteQbcreateParameter ('type ' )));
132+ 
133+ 		$ duplicatedAssignments$ selectQbexecuteQuery ();
134+ 		$ count0 ;
135+ 		while  ($ row$ duplicatedAssignmentsfetch ()) {
136+ 			$ deleteQb
137+ 				->setParameters ($ row
138+ 				->executeStatement ();
139+ 			$ count
140+ 		}
141+ 		if  ($ count0 ) {
142+ 			$ outputinfo (" - Deleted  $ count duplicate category assignments for  $ categoryId and  $ keepId );
143+ 		}
144+ 	}
106145}
0 commit comments