Total Complexity | 64 |
Total Lines | 357 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like DropUnusedDatabaseObjectsTask often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DropUnusedDatabaseObjectsTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class DropUnusedDatabaseObjectsTask extends BuildTask |
||
20 | { |
||
21 | use BuildTaskTools; |
||
22 | |||
23 | protected $title = "Drop unused database objects"; |
||
24 | protected $description = 'Drop unused tables and fields from your db by comparing current database tables with your dataobjects.'; |
||
25 | private static $segment = 'DropUnusedDatabaseObjectsTask'; |
||
26 | |||
27 | public function run($request) |
||
28 | { |
||
29 | // This can be very long |
||
30 | Environment::setTimeLimitMax(0); |
||
31 | |||
32 | $this->request = $request; |
||
33 | |||
34 | $this->addOption("tables", "Clean unused tables", true); |
||
35 | $this->addOption("fields", "Clean unused fields", true); |
||
36 | $this->addOption("reorder", "Reorder fields", true); |
||
37 | $this->addOption("go", "Tick this to proceed", false); |
||
38 | |||
39 | $options = $this->askOptions(); |
||
40 | |||
41 | $tables = $options['tables']; |
||
42 | $fields = $options['fields']; |
||
43 | $reorder = $options['reorder']; |
||
44 | $go = $options['go']; |
||
45 | |||
46 | if (!$go) { |
||
47 | echo ('Previewing what this task is about to do.'); |
||
48 | } else { |
||
49 | echo ("Let's clean this up!"); |
||
50 | } |
||
51 | echo ('<hr/>'); |
||
52 | if ($tables) { |
||
53 | $this->removeTables($request, $go); |
||
54 | } |
||
55 | if ($fields) { |
||
56 | $this->removeFields($request, $go); |
||
57 | } |
||
58 | if ($reorder) { |
||
59 | $this->reorderFields($request, $go); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | protected function reorderFields($request, $go = false) |
||
64 | { |
||
65 | $conn = DB::get_conn(); |
||
66 | $schema = DB::get_schema(); |
||
67 | $dataObjectSchema = DataObject::getSchema(); |
||
68 | $classes = $this->getClassesWithTables(); |
||
69 | $tableList = $schema->tableList(); |
||
70 | |||
71 | $this->message('<h2>Fields order</h2>'); |
||
72 | |||
73 | foreach ($classes as $class) { |
||
74 | /** @var SilverStripe\ORM\DataObject $singl */ |
||
75 | $singl = $class::singleton(); |
||
76 | $baseClass = $singl->baseClass(); |
||
77 | $table = $dataObjectSchema->tableName($class); |
||
78 | $lcTable = strtolower($table); |
||
79 | |||
80 | // It does not exist in the list, no need to worry about |
||
81 | if (!isset($tableList[$lcTable])) { |
||
82 | continue; |
||
83 | } |
||
84 | |||
85 | $fields = $dataObjectSchema->databaseFields($class); |
||
86 | $baseFields = $dataObjectSchema->databaseFields($baseClass); |
||
87 | |||
88 | $realFields = $fields; |
||
89 | if ($baseClass != $class) { |
||
90 | foreach ($baseFields as $k => $v) { |
||
91 | if ($k == "ID") { |
||
92 | continue; |
||
93 | } |
||
94 | unset($realFields[$k]); |
||
95 | } |
||
96 | |||
97 | // When extending multiple classes it's a mess to track, eg SubsitesVirtualPage |
||
98 | if (isset($realFields['VersionID'])) { |
||
99 | unset($realFields['VersionID']); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // We must pass the regular table name |
||
104 | $list = $schema->fieldList($table); |
||
105 | |||
106 | $fields_keys = array_keys($realFields); |
||
107 | $list_keys = array_keys($list); |
||
108 | |||
109 | if (json_encode($fields_keys) == json_encode($list_keys)) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | $fieldsThatNeedToMove = []; |
||
114 | foreach ($fields_keys as $k => $v) { |
||
115 | if (!isset($list_keys[$k])) { |
||
116 | continue; // not sure why |
||
117 | } |
||
118 | if ($list_keys[$k] != $v) { |
||
119 | $fieldsThatNeedToMove[] = $v; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | if ($go) { |
||
124 | $this->message("$table: moving " . implode(", ", $fieldsThatNeedToMove)); |
||
125 | |||
126 | // $conn->transactionStart(); |
||
127 | // fields contains the right order (the one from the codebase) |
||
128 | $after = "first"; |
||
129 | foreach ($fields_keys as $k => $v) { |
||
130 | if (isset($list_keys[$k]) && $list_keys[$k] != $v) { |
||
131 | $col = $v; |
||
132 | $def = $list[$v] ?? null; |
||
133 | if (!$def) { |
||
134 | // This happens when extending another model |
||
135 | $this->message("Ignore $v that has no definition", "error"); |
||
136 | continue; |
||
137 | } |
||
138 | // you CANNOT combine multiple columns reordering in a single ALTER TABLE statement. |
||
139 | $sql = "ALTER TABLE `$table` MODIFY `$col` $def $after"; |
||
140 | $this->message($sql); |
||
141 | try { |
||
142 | $conn->query($sql); |
||
143 | } catch (Exception $e) { |
||
144 | $this->message($e->getMessage(), "error"); |
||
145 | } |
||
146 | } |
||
147 | $after = "after $v"; |
||
148 | } |
||
149 | // $conn->transactionEnd(); |
||
150 | } else { |
||
151 | $this->message("$table: would move " . implode(", ", $fieldsThatNeedToMove)); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | protected function removeFields($request, $go = false) |
||
157 | { |
||
158 | $conn = DB::get_conn(); |
||
159 | $schema = DB::get_schema(); |
||
160 | $dataObjectSchema = DataObject::getSchema(); |
||
161 | $classes = $this->getClassesWithTables(); |
||
162 | $tableList = $schema->tableList(); |
||
163 | |||
164 | $this->message('<h2>Fields</h2>'); |
||
165 | |||
166 | $empty = true; |
||
167 | |||
168 | foreach ($classes as $class) { |
||
169 | /** @var SilverStripe\ORM\DataObject $singl */ |
||
170 | $singl = $class::singleton(); |
||
171 | $baseClass = $singl->baseClass(); |
||
172 | $table = $dataObjectSchema->tableName($baseClass); |
||
173 | $lcTable = strtolower($table); |
||
174 | |||
175 | // It does not exist in the list, no need to worry about |
||
176 | if (!isset($tableList[$lcTable])) { |
||
177 | continue; |
||
178 | } |
||
179 | $toDrop = []; |
||
180 | |||
181 | $fields = $dataObjectSchema->databaseFields($class); |
||
182 | // We must pass the regular table name |
||
183 | $list = $schema->fieldList($table); |
||
184 | // We can compare DataObject schema with actual schema |
||
185 | foreach ($list as $fieldName => $type) { |
||
186 | /// Never drop ID |
||
187 | if ($fieldName == 'ID') { |
||
188 | continue; |
||
189 | } |
||
190 | if (!isset($fields[$fieldName])) { |
||
191 | $toDrop[] = $fieldName; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | if (!empty($toDrop)) { |
||
196 | $empty = false; |
||
197 | if ($go) { |
||
198 | $this->dropColumns($table, $toDrop); |
||
199 | $this->message("Dropped " . implode(',', $toDrop) . " for $table", "obsolete"); |
||
200 | } else { |
||
201 | $this->message("Would drop " . implode(',', $toDrop) . " for $table", "obsolete"); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | // Localised fields support |
||
206 | if ($singl->hasExtension("\\TractorCow\\Fluent\\Extension\\FluentExtension")) { |
||
207 | $toDrop = []; |
||
208 | $localeTable = $table . '_Localised'; |
||
209 | $localeFields = $singl->getLocalisedFields($baseClass); |
||
210 | $localeList = $schema->fieldList($localeTable); |
||
211 | foreach ($localeList as $fieldName => $type) { |
||
212 | /// Never drop locale fields |
||
213 | if (in_array($fieldName, ['ID', 'RecordID', 'Locale'])) { |
||
214 | continue; |
||
215 | } |
||
216 | if (!isset($localeFields[$fieldName])) { |
||
217 | $toDrop[] = $fieldName; |
||
218 | } |
||
219 | } |
||
220 | if (!empty($toDrop)) { |
||
221 | $empty = false; |
||
222 | if ($go) { |
||
223 | $this->dropColumns($localeTable, $toDrop); |
||
224 | $this->message("Dropped " . implode(',', $toDrop) . " for $localeTable", "obsolete"); |
||
225 | } else { |
||
226 | $this->message("Would drop " . implode(',', $toDrop) . " for $localeTable", "obsolete"); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | |||
232 | if ($empty) { |
||
233 | $this->message("No fields to remove", "repaired"); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | protected function removeTables($request, $go = false) |
||
238 | { |
||
239 | $conn = DB::get_conn(); |
||
240 | $schema = DB::get_schema(); |
||
241 | $dataObjectSchema = DataObject::getSchema(); |
||
242 | $classes = $this->getClassesWithTables(); |
||
243 | $allDataObjects = array_values($this->getValidDataObjects()); |
||
244 | $tableList = $schema->tableList(); |
||
245 | $tablesToRemove = $tableList; |
||
246 | |||
247 | $this->message('<h2>Tables</h2>'); |
||
248 | |||
249 | foreach ($classes as $class) { |
||
250 | /** @var SilverStripe\ORM\DataObject $singl */ |
||
251 | $singl = $class::singleton(); |
||
252 | $table = $dataObjectSchema->tableName($class); |
||
253 | $lcTable = strtolower($table); |
||
254 | |||
255 | // It does not exist in the list, keep to remove later |
||
256 | if (!isset($tableList[$lcTable])) { |
||
257 | continue; |
||
258 | } |
||
259 | |||
260 | self::removeFromArray($lcTable, $tablesToRemove); |
||
261 | // Remove from the list versioned tables |
||
262 | if ($singl->hasExtension(Versioned::class)) { |
||
263 | self::removeFromArray($lcTable . '_live', $tablesToRemove); |
||
264 | self::removeFromArray($lcTable . '_versions', $tablesToRemove); |
||
265 | } |
||
266 | // Remove from the list fluent tables |
||
267 | if ($singl->hasExtension("\\TractorCow\\Fluent\\Extension\\FluentExtension")) { |
||
268 | self::removeFromArray($lcTable . '_localised', $tablesToRemove); |
||
269 | self::removeFromArray($lcTable . '_localised_live', $tablesToRemove); |
||
270 | self::removeFromArray($lcTable . '_localised_versions', $tablesToRemove); |
||
271 | } |
||
272 | |||
273 | // Relations |
||
274 | $hasMany = $class::config()->has_many; |
||
275 | if (!empty($hasMany)) { |
||
276 | foreach ($hasMany as $rel => $obj) { |
||
277 | self::removeFromArray($lcTable . '_' . strtolower($rel), $tablesToRemove); |
||
278 | } |
||
279 | } |
||
280 | // We catch relations without own classes later on |
||
281 | $manyMany = $class::config()->many_many; |
||
282 | if (!empty($manyMany)) { |
||
283 | foreach ($manyMany as $rel => $obj) { |
||
284 | self::removeFromArray($lcTable . '_' . strtolower($rel), $tablesToRemove); |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | //at this point, we should only have orphans table in dbTables var |
||
290 | foreach ($tablesToRemove as $lcTable => $table) { |
||
291 | // Remove many_many tables without own base table |
||
292 | if (strpos($table, '_') !== false) { |
||
293 | $parts = explode('_', $table); |
||
294 | $potentialClass = $parts[0]; |
||
295 | $potentialRelation = $parts[1]; |
||
296 | foreach ($allDataObjects as $dataObjectClass) { |
||
297 | $classParts = explode('\\', $dataObjectClass); |
||
298 | $tableClass = end($classParts); |
||
299 | if ($tableClass == $potentialClass) { |
||
300 | $manyManyRelations = $dataObjectClass::config()->many_many; |
||
301 | if (isset($manyManyRelations[$potentialRelation])) { |
||
302 | unset($tablesToRemove[$lcTable]); |
||
303 | continue 2; |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | } |
||
308 | if ($go) { |
||
309 | DB::query('DROP TABLE `' . $table . '`'); |
||
310 | $this->message("Dropped $table", 'obsolete'); |
||
311 | } else { |
||
312 | $this->message("Would drop $table", 'obsolete'); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | if (empty($tablesToRemove)) { |
||
317 | $this->message("No table to remove", "repaired"); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @return array |
||
323 | */ |
||
324 | protected function getClassesWithTables() |
||
327 | } |
||
328 | |||
329 | public static function removeFromArray($val, &$arr) |
||
330 | { |
||
331 | if (isset($arr[$val])) { |
||
332 | unset($arr[$val]); |
||
333 | } |
||
334 | } |
||
335 | |||
336 | public function dropColumns($table, $columns) |
||
337 | { |
||
338 | switch (get_class(DB::get_conn())) { |
||
339 | case \SilverStripe\SQLite\SQLite3Database::class: |
||
340 | case 'SQLite3Database': |
||
341 | $this->sqlLiteDropColumns($table, $columns); |
||
342 | break; |
||
343 | default: |
||
344 | $this->sqlDropColumns($table, $columns); |
||
345 | break; |
||
346 | } |
||
347 | } |
||
348 | |||
349 | public function sqlDropColumns($table, $columns) |
||
352 | } |
||
353 | |||
354 | public function sqlLiteDropColumns($table, $columns) |
||
376 | } |
||
377 | } |
||
378 | } |
||
379 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths