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 |
||
39 | class Migration extends Component implements MigrationInterface |
||
40 | { |
||
41 | use SchemaBuilderTrait; |
||
42 | |||
43 | /** |
||
44 | * @var Connection|array|string the DB connection object or the application component ID of the DB connection |
||
45 | * that this migration should work with. Starting from version 2.0.2, this can also be a configuration array |
||
46 | * for creating the object. |
||
47 | * |
||
48 | * Note that when a Migration object is created by the `migrate` command, this property will be overwritten |
||
49 | * by the command. If you do not want to use the DB connection provided by the command, you may override |
||
50 | * the [[init()]] method like the following: |
||
51 | * |
||
52 | * ```php |
||
53 | * public function init() |
||
54 | * { |
||
55 | * $this->db = 'db2'; |
||
56 | * parent::init(); |
||
57 | * } |
||
58 | * ``` |
||
59 | */ |
||
60 | public $db = 'db'; |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Initializes the migration. |
||
65 | * This method will set [[db]] to be the 'db' application component, if it is `null`. |
||
66 | */ |
||
67 | 13 | public function init() |
|
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | * @since 2.0.6 |
||
78 | */ |
||
79 | 1 | protected function getDb() |
|
83 | |||
84 | /** |
||
85 | * This method contains the logic to be executed when applying this migration. |
||
86 | * Child classes may override this method to provide actual migration logic. |
||
87 | * @return boolean return a false value to indicate the migration fails |
||
88 | * and should not proceed further. All other return values mean the migration succeeds. |
||
89 | */ |
||
90 | public function up() |
||
110 | |||
111 | /** |
||
112 | * This method contains the logic to be executed when removing this migration. |
||
113 | * The default implementation throws an exception indicating the migration cannot be removed. |
||
114 | * Child classes may override this method if the corresponding migrations can be removed. |
||
115 | * @return boolean return a false value to indicate the migration fails |
||
116 | * and should not proceed further. All other return values mean the migration succeeds. |
||
117 | */ |
||
118 | public function down() |
||
138 | |||
139 | /** |
||
140 | * This method contains the logic to be executed when applying this migration. |
||
141 | * This method differs from [[up()]] in that the DB logic implemented here will |
||
142 | * be enclosed within a DB transaction. |
||
143 | * Child classes may implement this method instead of [[up()]] if the DB logic |
||
144 | * needs to be within a transaction. |
||
145 | * @return boolean return a false value to indicate the migration fails |
||
146 | * and should not proceed further. All other return values mean the migration succeeds. |
||
147 | */ |
||
148 | public function safeUp() |
||
151 | |||
152 | /** |
||
153 | * This method contains the logic to be executed when removing this migration. |
||
154 | * This method differs from [[down()]] in that the DB logic implemented here will |
||
155 | * be enclosed within a DB transaction. |
||
156 | * Child classes may implement this method instead of [[down()]] if the DB logic |
||
157 | * needs to be within a transaction. |
||
158 | * @return boolean return a false value to indicate the migration fails |
||
159 | * and should not proceed further. All other return values mean the migration succeeds. |
||
160 | */ |
||
161 | public function safeDown() |
||
164 | |||
165 | /** |
||
166 | * Executes a SQL statement. |
||
167 | * This method executes the specified SQL statement using [[db]]. |
||
168 | * @param string $sql the SQL statement to be executed |
||
169 | * @param array $params input parameters (name => value) for the SQL execution. |
||
170 | * See [[Command::execute()]] for more details. |
||
171 | */ |
||
172 | public function execute($sql, $params = []) |
||
179 | |||
180 | /** |
||
181 | * Creates and executes an INSERT SQL statement. |
||
182 | * The method will properly escape the column names, and bind the values to be inserted. |
||
183 | * @param string $table the table that new rows will be inserted into. |
||
184 | * @param array $columns the column data (name => value) to be inserted into the table. |
||
185 | */ |
||
186 | public function insert($table, $columns) |
||
193 | |||
194 | /** |
||
195 | * Creates and executes an batch INSERT SQL statement. |
||
196 | * The method will properly escape the column names, and bind the values to be inserted. |
||
197 | * @param string $table the table that new rows will be inserted into. |
||
198 | * @param array $columns the column names. |
||
199 | * @param array $rows the rows to be batch inserted into the table |
||
200 | */ |
||
201 | public function batchInsert($table, $columns, $rows) |
||
208 | |||
209 | /** |
||
210 | * Creates and executes an UPDATE SQL statement. |
||
211 | * The method will properly escape the column names and bind the values to be updated. |
||
212 | * @param string $table the table to be updated. |
||
213 | * @param array $columns the column data (name => value) to be updated. |
||
214 | * @param array|string $condition the conditions that will be put in the WHERE part. Please |
||
215 | * refer to [[Query::where()]] on how to specify conditions. |
||
216 | * @param array $params the parameters to be bound to the query. |
||
217 | */ |
||
218 | public function update($table, $columns, $condition = '', $params = []) |
||
225 | |||
226 | /** |
||
227 | * Creates and executes a DELETE SQL statement. |
||
228 | * @param string $table the table where the data will be deleted from. |
||
229 | * @param array|string $condition the conditions that will be put in the WHERE part. Please |
||
230 | * refer to [[Query::where()]] on how to specify conditions. |
||
231 | * @param array $params the parameters to be bound to the query. |
||
232 | */ |
||
233 | public function delete($table, $condition = '', $params = []) |
||
240 | |||
241 | /** |
||
242 | * Builds and executes a SQL statement for creating a new DB table. |
||
243 | * |
||
244 | * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), |
||
245 | * where name stands for a column name which will be properly quoted by the method, and definition |
||
246 | * stands for the column type which can contain an abstract DB type. |
||
247 | * |
||
248 | * The [[QueryBuilder::getColumnType()]] method will be invoked to convert any abstract type into a physical one. |
||
249 | * |
||
250 | * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly |
||
251 | * put into the generated SQL. |
||
252 | * |
||
253 | * @param string $table the name of the table to be created. The name will be properly quoted by the method. |
||
254 | * @param array $columns the columns (name => definition) in the new table. |
||
255 | * @param string $options additional SQL fragment that will be appended to the generated SQL. |
||
256 | */ |
||
257 | 1 | public function createTable($table, $columns, $options = null) |
|
269 | |||
270 | /** |
||
271 | * Builds and executes a SQL statement for renaming a DB table. |
||
272 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
273 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
274 | */ |
||
275 | public function renameTable($table, $newName) |
||
282 | |||
283 | /** |
||
284 | * Builds and executes a SQL statement for dropping a DB table. |
||
285 | * @param string $table the table to be dropped. The name will be properly quoted by the method. |
||
286 | */ |
||
287 | 1 | public function dropTable($table) |
|
294 | |||
295 | /** |
||
296 | * Builds and executes a SQL statement for truncating a DB table. |
||
297 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
298 | */ |
||
299 | public function truncateTable($table) |
||
306 | |||
307 | /** |
||
308 | * Builds and executes a SQL statement for adding a new DB column. |
||
309 | * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. |
||
310 | * @param string $column the name of the new column. The name will be properly quoted by the method. |
||
311 | * @param string $type the column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
312 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
313 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
314 | */ |
||
315 | public function addColumn($table, $column, $type) |
||
325 | |||
326 | /** |
||
327 | * Builds and executes a SQL statement for dropping a DB column. |
||
328 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
329 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
330 | */ |
||
331 | public function dropColumn($table, $column) |
||
338 | |||
339 | /** |
||
340 | * Builds and executes a SQL statement for renaming a column. |
||
341 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
342 | * @param string $name the old name of the column. The name will be properly quoted by the method. |
||
343 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
344 | */ |
||
345 | public function renameColumn($table, $name, $newName) |
||
352 | |||
353 | /** |
||
354 | * Builds and executes a SQL statement for changing the definition of a column. |
||
355 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
356 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
357 | * @param string $type the new column type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert abstract column type (if any) |
||
358 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
359 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
360 | */ |
||
361 | public function alterColumn($table, $column, $type) |
||
371 | |||
372 | /** |
||
373 | * Builds and executes a SQL statement for creating a primary key. |
||
374 | * The method will properly quote the table and column names. |
||
375 | * @param string $name the name of the primary key constraint. |
||
376 | * @param string $table the table that the primary key constraint will be added to. |
||
377 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
378 | */ |
||
379 | public function addPrimaryKey($name, $table, $columns) |
||
386 | |||
387 | /** |
||
388 | * Builds and executes a SQL statement for dropping a primary key. |
||
389 | * @param string $name the name of the primary key constraint to be removed. |
||
390 | * @param string $table the table that the primary key constraint will be removed from. |
||
391 | */ |
||
392 | public function dropPrimaryKey($name, $table) |
||
399 | |||
400 | /** |
||
401 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
402 | * The method will properly quote the table and column names. |
||
403 | * @param string $name the name of the foreign key constraint. |
||
404 | * @param string $table the table that the foreign key constraint will be added to. |
||
405 | * @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. |
||
406 | * @param string $refTable the table that the foreign key references to. |
||
407 | * @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. |
||
408 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
409 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
410 | */ |
||
411 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
418 | |||
419 | /** |
||
420 | * Builds a SQL statement for dropping a foreign key constraint. |
||
421 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
422 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
423 | */ |
||
424 | public function dropForeignKey($name, $table) |
||
431 | |||
432 | /** |
||
433 | * Builds and executes a SQL statement for creating a new index. |
||
434 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
435 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
436 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, please separate them |
||
437 | * by commas or use an array. Each column name will be properly quoted by the method. Quoting will be skipped for column names that |
||
438 | * include a left parenthesis "(". |
||
439 | * @param boolean $unique whether to add UNIQUE constraint on the created index. |
||
440 | */ |
||
441 | public function createIndex($name, $table, $columns, $unique = false) |
||
448 | |||
449 | /** |
||
450 | * Builds and executes a SQL statement for dropping an index. |
||
451 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
452 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
453 | */ |
||
454 | public function dropIndex($name, $table) |
||
461 | |||
462 | /** |
||
463 | * Builds and execute a SQL statement for adding comment to column |
||
464 | * |
||
465 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
466 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
467 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
468 | * @since 2.0.8 |
||
469 | */ |
||
470 | public function addCommentOnColumn($table, $column, $comment) |
||
477 | |||
478 | /** |
||
479 | * Builds a SQL statement for adding comment to table |
||
480 | * |
||
481 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
482 | * @param string $comment the text of the comment to be added. The comment will be properly quoted by the method. |
||
483 | * @since 2.0.8 |
||
484 | */ |
||
485 | public function addCommentOnTable($table, $comment) |
||
492 | |||
493 | /** |
||
494 | * Builds and execute a SQL statement for dropping comment from column |
||
495 | * |
||
496 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
497 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
498 | * @since 2.0.8 |
||
499 | */ |
||
500 | public function dropCommentFromColumn($table, $column) |
||
507 | |||
508 | /** |
||
509 | * Builds a SQL statement for dropping comment from table |
||
510 | * |
||
511 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
512 | * @since 2.0.8 |
||
513 | */ |
||
514 | public function dropCommentFromTable($table) |
||
521 | } |
||
522 |