Complex classes like Migration 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Migration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class Migration extends Component implements MigrationInterface |
||
47 | { |
||
48 | use SchemaBuilderTrait; |
||
49 | |||
50 | /** |
||
51 | * @var Connection|array|string the DB connection object or the application component ID of the DB connection |
||
52 | * that this migration should work with. Starting from version 2.0.2, this can also be a configuration array |
||
53 | * for creating the object. |
||
54 | * |
||
55 | * Note that when a Migration object is created by the `migrate` command, this property will be overwritten |
||
56 | * by the command. If you do not want to use the DB connection provided by the command, you may override |
||
57 | * the [[init()]] method like the following: |
||
58 | * |
||
59 | * ```php |
||
60 | * public function init() |
||
61 | * { |
||
62 | * $this->db = 'db2'; |
||
63 | * parent::init(); |
||
64 | * } |
||
65 | * ``` |
||
66 | */ |
||
67 | public $db = 'db'; |
||
68 | |||
69 | /** |
||
70 | * @var int max number of characters of the SQL outputted. Useful for reduction of long statements and making |
||
71 | * console output more compact. |
||
72 | * @since 2.0.13 |
||
73 | */ |
||
74 | public $maxSqlOutputLength; |
||
75 | |||
76 | /** |
||
77 | * @var bool indicates whether the console output should be compacted. |
||
78 | * If this is set to true, the individual commands ran within the migration will not be output to the console. |
||
79 | * Default is false, in other words the output is fully verbose by default. |
||
80 | * @since 2.0.13 |
||
81 | */ |
||
82 | public $compact = false; |
||
83 | |||
84 | /** |
||
85 | * Initializes the migration. |
||
86 | * This method will set [[db]] to be the 'db' application component, if it is `null`. |
||
87 | */ |
||
88 | 21 | public function init() |
|
89 | { |
||
90 | 21 | parent::init(); |
|
91 | 21 | $this->db = Instance::ensure($this->db, Connection::class); |
|
92 | 21 | $this->db->getSchema()->refresh(); |
|
93 | 21 | $this->db->enableSlaves = false; |
|
94 | 21 | } |
|
95 | |||
96 | /** |
||
97 | * @inheritdoc |
||
98 | * @since 2.0.6 |
||
99 | */ |
||
100 | 7 | protected function getDb() |
|
101 | { |
||
102 | 7 | return $this->db; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * This method contains the logic to be executed when applying this migration. |
||
107 | * Child classes may override this method to provide actual migration logic. |
||
108 | * @return bool return a false value to indicate the migration fails |
||
109 | * and should not proceed further. All other return values mean the migration succeeds. |
||
110 | */ |
||
111 | 1 | public function up() |
|
112 | { |
||
113 | 1 | $transaction = $this->db->beginTransaction(); |
|
114 | try { |
||
115 | 1 | if ($this->safeUp() === false) { |
|
116 | $transaction->rollBack(); |
||
117 | return false; |
||
118 | } |
||
119 | 1 | $transaction->commit(); |
|
120 | } catch (\Throwable $e) { |
||
121 | $this->printException($e); |
||
122 | $transaction->rollBack(); |
||
123 | return false; |
||
124 | } |
||
125 | |||
126 | 1 | return null; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * This method contains the logic to be executed when removing this migration. |
||
131 | * The default implementation throws an exception indicating the migration cannot be removed. |
||
132 | * Child classes may override this method if the corresponding migrations can be removed. |
||
133 | * @return bool return a false value to indicate the migration fails |
||
134 | * and should not proceed further. All other return values mean the migration succeeds. |
||
135 | */ |
||
136 | public function down() |
||
137 | { |
||
138 | $transaction = $this->db->beginTransaction(); |
||
139 | try { |
||
140 | if ($this->safeDown() === false) { |
||
141 | $transaction->rollBack(); |
||
142 | return false; |
||
143 | } |
||
144 | $transaction->commit(); |
||
145 | } catch (\Throwable $e) { |
||
146 | $this->printException($e); |
||
147 | $transaction->rollBack(); |
||
148 | return false; |
||
149 | } |
||
150 | |||
151 | return null; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param \Throwable $e |
||
156 | */ |
||
157 | private function printException($e) |
||
162 | |||
163 | /** |
||
164 | * This method contains the logic to be executed when applying this migration. |
||
165 | * This method differs from [[up()]] in that the DB logic implemented here will |
||
166 | * be enclosed within a DB transaction. |
||
167 | * Child classes may implement this method instead of [[up()]] if the DB logic |
||
168 | * needs to be within a transaction. |
||
169 | * |
||
170 | * Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples, |
||
171 | * please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html). If this is the case, |
||
172 | * you should still implement `up()` and `down()`, instead. |
||
173 | * |
||
174 | * @return bool return a false value to indicate the migration fails |
||
175 | * and should not proceed further. All other return values mean the migration succeeds. |
||
176 | */ |
||
177 | public function safeUp() |
||
180 | |||
181 | /** |
||
182 | * This method contains the logic to be executed when removing this migration. |
||
183 | * This method differs from [[down()]] in that the DB logic implemented here will |
||
184 | * be enclosed within a DB transaction. |
||
185 | * Child classes may implement this method instead of [[down()]] if the DB logic |
||
186 | * needs to be within a transaction. |
||
187 | * |
||
188 | * Note: Not all DBMS support transactions. And some DB queries cannot be put into a transaction. For some examples, |
||
189 | * please refer to [implicit commit](http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html). If this is the case, |
||
190 | * you should still implement `up()` and `down()`, instead. |
||
191 | * |
||
192 | * @return bool return a false value to indicate the migration fails |
||
193 | * and should not proceed further. All other return values mean the migration succeeds. |
||
194 | */ |
||
195 | public function safeDown() |
||
198 | |||
199 | /** |
||
200 | * Executes a SQL statement. |
||
201 | * This method executes the specified SQL statement using [[db]]. |
||
202 | * @param string $sql the SQL statement to be executed |
||
203 | * @param array $params input parameters (name => value) for the SQL execution. |
||
204 | * See [[Command::execute()]] for more details. |
||
205 | */ |
||
206 | public function execute($sql, $params = []) |
||
217 | |||
218 | /** |
||
219 | * Creates and executes an INSERT SQL statement. |
||
220 | * The method will properly escape the column names, and bind the values to be inserted. |
||
221 | * @param string $table the table that new rows will be inserted into. |
||
222 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
223 | */ |
||
224 | public function insert($table, $columns) |
||
230 | |||
231 | /** |
||
232 | * Creates and executes an batch INSERT SQL statement. |
||
233 | * The method will properly escape the column names, and bind the values to be inserted. |
||
234 | * @param string $table the table that new rows will be inserted into. |
||
235 | * @param array $columns the column names. |
||
236 | * @param array $rows the rows to be batch inserted into the table |
||
237 | */ |
||
238 | public function batchInsert($table, $columns, $rows) |
||
244 | |||
245 | /** |
||
246 | * Creates and executes an UPDATE SQL statement. |
||
247 | * The method will properly escape the column names and bind the values to be updated. |
||
248 | * @param string $table the table to be updated. |
||
249 | * @param array $columns the column data (name => value) to be updated. |
||
250 | * @param array|string $condition the conditions that will be put in the WHERE part. Please |
||
251 | * refer to [[Query::where()]] on how to specify conditions. |
||
252 | * @param array $params the parameters to be bound to the query. |
||
253 | */ |
||
254 | public function update($table, $columns, $condition = '', $params = []) |
||
260 | |||
261 | /** |
||
262 | * Creates and executes a DELETE SQL statement. |
||
263 | * @param string $table the table where the data will be deleted from. |
||
264 | * @param array|string $condition the conditions that will be put in the WHERE part. Please |
||
265 | * refer to [[Query::where()]] on how to specify conditions. |
||
266 | * @param array $params the parameters to be bound to the query. |
||
267 | */ |
||
268 | public function delete($table, $condition = '', $params = []) |
||
274 | |||
275 | /** |
||
276 | * Builds and executes a SQL statement for creating a new DB table. |
||
277 | * |
||
278 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
279 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
280 | * stands for the column type which can contain an abstract DB type. |
||
281 | * |
||
282 | * The [[QueryBuilder::getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
283 | * |
||
284 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
285 | * put into the generated SQL. |
||
286 | * |
||
287 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
288 | * @param array $columns the columns (name => definition) in the new table. |
||
289 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
290 | */ |
||
291 | 7 | public function createTable($table, $columns, $options = null) |
|
302 | |||
303 | /** |
||
304 | * Builds and executes a SQL statement for renaming a DB table. |
||
305 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
306 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
307 | */ |
||
308 | public function renameTable($table, $newName) |
||
314 | |||
315 | /** |
||
316 | * Builds and executes a SQL statement for dropping a DB table. |
||
317 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
318 | */ |
||
319 | 7 | public function dropTable($table) |
|
325 | |||
326 | /** |
||
327 | * Builds and executes a SQL statement for truncating a DB table. |
||
328 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
329 | */ |
||
330 | public function truncateTable($table) |
||
336 | |||
337 | /** |
||
338 | * Builds and executes a SQL statement for adding a new DB column. |
||
339 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
340 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
341 | * @param string $type the column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
342 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
343 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
344 | */ |
||
345 | public function addColumn($table, $column, $type) |
||
354 | |||
355 | /** |
||
356 | * Builds and executes a SQL statement for dropping a DB column. |
||
357 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
358 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
359 | */ |
||
360 | public function dropColumn($table, $column) |
||
366 | |||
367 | /** |
||
368 | * Builds and executes a SQL statement for renaming a column. |
||
369 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
370 | * @param string $name the old name of the column. The name will be properly quoted by the method. |
||
371 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
372 | */ |
||
373 | public function renameColumn($table, $name, $newName) |
||
379 | |||
380 | /** |
||
381 | * Builds and executes a SQL statement for changing the definition of a column. |
||
382 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
383 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
384 | * @param string $type the new column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
385 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
386 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
387 | */ |
||
388 | public function alterColumn($table, $column, $type) |
||
397 | |||
398 | /** |
||
399 | * Builds and executes a SQL statement for creating a primary key. |
||
400 | * The method will properly quote the table and column names. |
||
401 | * @param string $name the name of the primary key constraint. |
||
402 | * @param string $table the table that the primary key constraint will be added to. |
||
403 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
404 | */ |
||
405 | public function addPrimaryKey($name, $table, $columns) |
||
411 | |||
412 | /** |
||
413 | * Builds and executes a SQL statement for dropping a primary key. |
||
414 | * @param string $name the name of the primary key constraint to be removed. |
||
415 | * @param string $table the table that the primary key constraint will be removed from. |
||
416 | */ |
||
417 | public function dropPrimaryKey($name, $table) |
||
423 | |||
424 | /** |
||
425 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
426 | * The method will properly quote the table and column names. |
||
427 | * @param string $name the name of the foreign key constraint. |
||
428 | * @param string $table the table that the foreign key constraint will be added to. |
||
429 | * @param string|array $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array. |
||
430 | * @param string $refTable the table that the foreign key references to. |
||
431 | * @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array. |
||
432 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
433 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
434 | */ |
||
435 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
441 | |||
442 | /** |
||
443 | * Builds a SQL statement for dropping a foreign key constraint. |
||
444 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
445 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
446 | */ |
||
447 | public function dropForeignKey($name, $table) |
||
453 | |||
454 | /** |
||
455 | * Builds and executes a SQL statement for creating a new index. |
||
456 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
457 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
458 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them |
||
459 | * by commas or use an array. Each column name will be properly quoted by the method. Quoting will be skipped for column names that |
||
460 | * include a left parenthesis "(". |
||
461 | * @param bool $unique whether to add UNIQUE constraint on the created index. |
||
462 | */ |
||
463 | 6 | public function createIndex($name, $table, $columns, $unique = false) |
|
469 | |||
470 | /** |
||
471 | * Builds and executes a SQL statement for dropping an index. |
||
472 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
473 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
474 | */ |
||
475 | public function dropIndex($name, $table) |
||
481 | |||
482 | /** |
||
483 | * Builds and execute a SQL statement for adding comment to column. |
||
484 | * |
||
485 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
486 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
487 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
488 | * @since 2.0.8 |
||
489 | */ |
||
490 | public function addCommentOnColumn($table, $column, $comment) |
||
496 | |||
497 | /** |
||
498 | * Builds a SQL statement for adding comment to table. |
||
499 | * |
||
500 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
501 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
502 | * @since 2.0.8 |
||
503 | */ |
||
504 | public function addCommentOnTable($table, $comment) |
||
510 | |||
511 | /** |
||
512 | * Builds and execute a SQL statement for dropping comment from column. |
||
513 | * |
||
514 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
515 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
516 | * @since 2.0.8 |
||
517 | */ |
||
518 | public function dropCommentFromColumn($table, $column) |
||
524 | |||
525 | /** |
||
526 | * Builds a SQL statement for dropping comment from table. |
||
527 | * |
||
528 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
529 | * @since 2.0.8 |
||
530 | */ |
||
531 | public function dropCommentFromTable($table) |
||
537 | |||
538 | /** |
||
539 | * Prepares for a command to be executed, and outputs to the console |
||
540 | * |
||
541 | * @param string $description the description for the command, to be output to the console. |
||
542 | * @return float the time before the command is executed, for the time elapsed to be calculated. |
||
543 | * @since 2.0.13 |
||
544 | */ |
||
545 | 7 | protected function beginCommand($description) |
|
552 | |||
553 | /** |
||
554 | * Finalizes after the command has been executed, and outputs to the console the time elapsed |
||
555 | * |
||
556 | * @param float the time before the command was executed. |
||
557 | * @since 2.0.13 |
||
558 | */ |
||
559 | 7 | protected function endCommand($time) |
|
565 | } |
||
566 |