@@ -5,8 +5,7 @@ discard block |
||
| 5 | 5 | /** |
| 6 | 6 | * Table class |
| 7 | 7 | */ |
| 8 | -trait Formatter |
|
| 9 | -{ |
|
| 8 | +trait Formatter { |
|
| 10 | 9 | |
| 11 | 10 | /** |
| 12 | 11 | * @var array column enclosing literal |
@@ -41,15 +40,13 @@ discard block |
||
| 41 | 40 | * @author Tarun Mukherjee (https://github.com/tmukherjee13) |
| 42 | 41 | */ |
| 43 | 42 | |
| 44 | - public function getTableName($table) |
|
| 45 | - { |
|
| 43 | + public function getTableName($table) { |
|
| 46 | 44 | return '{{%' . str_replace($this->db->tablePrefix, '', $table->name) . '}}'; |
| 47 | 45 | } |
| 48 | 46 | |
| 49 | 47 | |
| 50 | 48 | |
| 51 | - public function prepareInsert($rows, $columns) |
|
| 52 | - { |
|
| 49 | + public function prepareInsert($rows, $columns) { |
|
| 53 | 50 | |
| 54 | 51 | |
| 55 | 52 | return '$this->batchInsert("{{%test}}", ' . $rows . ', ' . $columns . ');'; |
@@ -60,8 +57,7 @@ discard block |
||
| 60 | 57 | * @param string $data the column string|$trim the literal to trim |
| 61 | 58 | * @return string |
| 62 | 59 | */ |
| 63 | - public function prepareColumns($data, $trim = ',') |
|
| 64 | - { |
|
| 60 | + public function prepareColumns($data, $trim = ',') { |
|
| 65 | 61 | return self::columnFormat($data, $trim); |
| 66 | 62 | } |
| 67 | 63 | |
@@ -70,8 +66,7 @@ discard block |
||
| 70 | 66 | * @param array $data the data array |
| 71 | 67 | * @return string |
| 72 | 68 | */ |
| 73 | - public function prepareData($data = []) |
|
| 74 | - { |
|
| 69 | + public function prepareData($data = []) { |
|
| 75 | 70 | self::$rows = ''; |
| 76 | 71 | foreach ($data as $row) { |
| 77 | 72 | $rows = ''; |
@@ -91,8 +86,7 @@ discard block |
||
| 91 | 86 | * @param string $data the column string|$trim the literal to trim |
| 92 | 87 | * @return string |
| 93 | 88 | */ |
| 94 | - public function dataFormat($data, $trim = ',') |
|
| 95 | - { |
|
| 89 | + public function dataFormat($data, $trim = ',') { |
|
| 96 | 90 | if (null !== $trim) { |
| 97 | 91 | $data = rtrim($data, $trim); |
| 98 | 92 | } |
@@ -104,8 +98,7 @@ discard block |
||
| 104 | 98 | * @param string $data the column string|$trim the literal to trim |
| 105 | 99 | * @return string |
| 106 | 100 | */ |
| 107 | - public function columnFormat($data, $trim = ',') |
|
| 108 | - { |
|
| 101 | + public function columnFormat($data, $trim = ',') { |
|
| 109 | 102 | if (null !== $trim) { |
| 110 | 103 | $data = rtrim($data, $trim); |
| 111 | 104 | } |
@@ -122,8 +115,7 @@ discard block |
||
| 122 | 115 | * @author Tarun Mukherjee (https://github.com/tmukherjee13) |
| 123 | 116 | */ |
| 124 | 117 | |
| 125 | - public function getColType($col) |
|
| 126 | - { |
|
| 118 | + public function getColType($col) { |
|
| 127 | 119 | |
| 128 | 120 | if ($col->isPrimaryKey && $col->autoIncrement) { |
| 129 | 121 | return 'pk'; |
@@ -135,11 +127,14 @@ discard block |
||
| 135 | 127 | } |
| 136 | 128 | if ($col->defaultValue != null && 'timestamp' != $col->dbType) { |
| 137 | 129 | $result .= " DEFAULT '{$col->defaultValue}'"; |
| 138 | - } elseif ($col->defaultValue == 'CURRENT_TIMESTAMP' && 'timestamp' == $col->dbType) { |
|
| 130 | + } |
|
| 131 | + elseif ($col->defaultValue == 'CURRENT_TIMESTAMP' && 'timestamp' == $col->dbType) { |
|
| 139 | 132 | $result .= " DEFAULT {$col->defaultValue}"; |
| 140 | - } elseif ($col->defaultValue != null && 'timestamp' == $col->dbType) { |
|
| 133 | + } |
|
| 134 | + elseif ($col->defaultValue != null && 'timestamp' == $col->dbType) { |
|
| 141 | 135 | $result .= " DEFAULT '{$col->defaultValue}'"; |
| 142 | - } elseif ($col->allowNull) { |
|
| 136 | + } |
|
| 137 | + elseif ($col->allowNull) { |
|
| 143 | 138 | $result .= ' DEFAULT NULL'; |
| 144 | 139 | } |
| 145 | 140 | return $result; |
@@ -167,19 +162,22 @@ discard block |
||
| 167 | 162 | * @author Tarun Mukherjee (https://github.com/tmukherjee13) |
| 168 | 163 | */ |
| 169 | 164 | |
| 170 | - public function formatCol($col) |
|
| 171 | - { |
|
| 165 | + public function formatCol($col) { |
|
| 172 | 166 | $decorator = []; |
| 173 | 167 | if ($col->isPrimaryKey && $col->autoIncrement) { |
| 174 | 168 | $decorator[] = 'primaryKey'; |
| 175 | - } elseif (in_array($col->type, self::$colTypes)) { |
|
| 169 | + } |
|
| 170 | + elseif (in_array($col->type, self::$colTypes)) { |
|
| 176 | 171 | $decorator[] = "{$this->modifyColType($col->dbType)}"; |
| 177 | - } elseif ($col->type == 'decimal') { |
|
| 172 | + } |
|
| 173 | + elseif ($col->type == 'decimal') { |
|
| 178 | 174 | $decorator[] = "{$col->dbType}"; |
| 179 | - } else { |
|
| 175 | + } |
|
| 176 | + else { |
|
| 180 | 177 | if (!empty($col->size) && $col->size == 1 && $col->type != 'char') { |
| 181 | 178 | $column = "boolean"; |
| 182 | - } else { |
|
| 179 | + } |
|
| 180 | + else { |
|
| 183 | 181 | $column = "{$col->type}"; |
| 184 | 182 | if (!empty($col->size)) { |
| 185 | 183 | $column .= "({$col->size})"; |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | throw new Exception("The 'db' option must refer to the application component ID of a DB connection."); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - $path = (string)Yii::getAlias($this->migrationPath); |
|
| 92 | + $path = (string) Yii::getAlias($this->migrationPath); |
|
| 93 | 93 | |
| 94 | 94 | |
| 95 | 95 | if (!is_dir($path)) { |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | */ |
| 185 | 185 | protected function getCreateTableSql($table) |
| 186 | 186 | { |
| 187 | - $row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->quoteTableName($table->fullName))->queryOne(); |
|
| 187 | + $row = $this->db->createCommand('SHOW CREATE TABLE '.$this->quoteTableName($table->fullName))->queryOne(); |
|
| 188 | 188 | if (isset($row['Create Table'])) { |
| 189 | 189 | $sql = $row['Create Table']; |
| 190 | 190 | } else { |
@@ -210,11 +210,11 @@ discard block |
||
| 210 | 210 | public function actionTable(array $tables) |
| 211 | 211 | { |
| 212 | 212 | |
| 213 | - if ($this->confirm('Create the migration ' . "?")) { |
|
| 213 | + if ($this->confirm('Create the migration '."?")) { |
|
| 214 | 214 | |
| 215 | 215 | foreach ($tables as $key => $tableName) { |
| 216 | 216 | |
| 217 | - $this->class = 'create_table_' . $tableName; |
|
| 217 | + $this->class = 'create_table_'.$tableName; |
|
| 218 | 218 | |
| 219 | 219 | try { |
| 220 | 220 | |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | $this->table = $this->getTableName($table); |
| 240 | 240 | |
| 241 | 241 | foreach ($table->columns as $col) { |
| 242 | - $up .= "\t\t\t" . '\'' . $col->name . '\'=>"' . $this->getColType($col) . '",' . "\n"; |
|
| 242 | + $up .= "\t\t\t".'\''.$col->name.'\'=>"'.$this->getColType($col).'",'."\n"; |
|
| 243 | 243 | if ($col->isPrimaryKey) { |
| 244 | 244 | // Add column to composite primary key array |
| 245 | 245 | $compositePrimaryKeyCols[] = $col->name; |
@@ -248,7 +248,7 @@ discard block |
||
| 248 | 248 | |
| 249 | 249 | $ukeys = $this->db->schema->findUniqueIndexes($table); |
| 250 | 250 | if (!empty($ukeys)) { |
| 251 | - $indexArr =[]; |
|
| 251 | + $indexArr = []; |
|
| 252 | 252 | foreach ($ukeys as $key => $value) { |
| 253 | 253 | $indexKey = $key; |
| 254 | 254 | foreach ($value as $id => $field) { |
@@ -256,15 +256,15 @@ discard block |
||
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | $indexStr = implode(',', $indexArr); |
| 259 | - $up .= "\t\t" . '$this->createIndex(\'idx_' . $indexKey . "', '" . $this->getTableName($table) . "', '$indexStr', TRUE);\n"; |
|
| 259 | + $up .= "\t\t".'$this->createIndex(\'idx_'.$indexKey."', '".$this->getTableName($table)."', '$indexStr', TRUE);\n"; |
|
| 260 | 260 | |
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | if (!empty($table->foreignKeys)): |
| 264 | 264 | |
| 265 | 265 | foreach ($table->foreignKeys as $fkName => $fk) { |
| 266 | - $addForeignKeys .= "\t\t" . '$this->addForeignKey("' . $fkName . '", "' . $table->name . '", "' . $fk['column'] . '","' . $fk['table'] . '","' . $fk['ref_column'] . '", "' . $fk['delete'] . '", "' . $fk['update'] . '");' . "\n"; |
|
| 267 | - $dropForeignKeys .= '$this->dropForeignKey(' . "'$fkName', '$table->name');"; |
|
| 266 | + $addForeignKeys .= "\t\t".'$this->addForeignKey("'.$fkName.'", "'.$table->name.'", "'.$fk['column'].'","'.$fk['table'].'","'.$fk['ref_column'].'", "'.$fk['delete'].'", "'.$fk['update'].'");'."\n"; |
|
| 267 | + $dropForeignKeys .= '$this->dropForeignKey('."'$fkName', '$table->name');"; |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | endif; |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | |
| 274 | 274 | $fields = $this->parseFields(); |
| 275 | 275 | |
| 276 | - $this->prepareFile(['up' => '', 'down' => '', 'foreignKeys' => $table->foreignKeys, 'fields' => $fields]) . "\n\n"; |
|
| 276 | + $this->prepareFile(['up' => '', 'down' => '', 'foreignKeys' => $table->foreignKeys, 'fields' => $fields])."\n\n"; |
|
| 277 | 277 | |
| 278 | 278 | } |
| 279 | 279 | |
@@ -289,11 +289,11 @@ discard block |
||
| 289 | 289 | public function actionData(array $tables) |
| 290 | 290 | { |
| 291 | 291 | |
| 292 | - if ($this->confirm('Create the migration ' . "?", true)) { |
|
| 292 | + if ($this->confirm('Create the migration '."?", true)) { |
|
| 293 | 293 | foreach ($tables as $key => $args) { |
| 294 | 294 | |
| 295 | 295 | $table = $args; |
| 296 | - $this->class = 'insert_data_into_' . $table; |
|
| 296 | + $this->class = 'insert_data_into_'.$table; |
|
| 297 | 297 | $this->table = $table; |
| 298 | 298 | $this->templateFile = '@tmukherjee13/migration/views/dataTemplate.php'; |
| 299 | 299 | |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | |
| 322 | 322 | $pcolumns = ''; |
| 323 | 323 | foreach ($columns->columns as $column) { |
| 324 | - $pcolumns .= "'" . $column->name . "',"; |
|
| 324 | + $pcolumns .= "'".$column->name."',"; |
|
| 325 | 325 | } |
| 326 | 326 | foreach ($data as $row) { |
| 327 | 327 | array_push($prepared_data, $row); |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | { |
| 354 | 354 | |
| 355 | 355 | $schema = $args; |
| 356 | - $this->class = 'dump_database_' . $schema; |
|
| 356 | + $this->class = 'dump_database_'.$schema; |
|
| 357 | 357 | |
| 358 | 358 | // $tables = $this->db->schema->getTableNames($schema); |
| 359 | 359 | |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | $generateTables = []; |
| 369 | 369 | |
| 370 | 370 | foreach ($tables as $table) { |
| 371 | - if ($table->name === $this->db->tablePrefix . $this->migrationTable) { |
|
| 371 | + if ($table->name === $this->db->tablePrefix.$this->migrationTable) { |
|
| 372 | 372 | continue; |
| 373 | 373 | } |
| 374 | 374 | $generateTables[] = $table->name; |
@@ -380,7 +380,7 @@ discard block |
||
| 380 | 380 | |
| 381 | 381 | public function getFileName() |
| 382 | 382 | { |
| 383 | - return 'm' . gmdate('ymd_His') . '_' . $this->class; |
|
| 383 | + return 'm'.gmdate('ymd_His').'_'.$this->class; |
|
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | public function setFileName() |
@@ -391,9 +391,9 @@ discard block |
||
| 391 | 391 | public function prepareFile($data) |
| 392 | 392 | { |
| 393 | 393 | if ($this->useDataMigrationFolder) { |
| 394 | - FileHelper::createDirectory($this->migrationPath . DIRECTORY_SEPARATOR . $this->dataMigrationFolder); |
|
| 394 | + FileHelper::createDirectory($this->migrationPath.DIRECTORY_SEPARATOR.$this->dataMigrationFolder); |
|
| 395 | 395 | } |
| 396 | - $file = $this->migrationPath . DIRECTORY_SEPARATOR . ($this->useDataMigrationFolder ? $this->dataMigrationFolder . DIRECTORY_SEPARATOR : null) . $this->getFileName() . '.php'; |
|
| 396 | + $file = $this->migrationPath.DIRECTORY_SEPARATOR.($this->useDataMigrationFolder ? $this->dataMigrationFolder.DIRECTORY_SEPARATOR : null).$this->getFileName().'.php'; |
|
| 397 | 397 | try { |
| 398 | 398 | |
| 399 | 399 | $data['table'] = $this->table; |
@@ -10,8 +10,7 @@ discard block |
||
| 10 | 10 | use yii\helpers\Console; |
| 11 | 11 | use yii\helpers\FileHelper; |
| 12 | 12 | |
| 13 | -class MigrationController extends MigrateController |
|
| 14 | -{ |
|
| 13 | +class MigrationController extends MigrateController { |
|
| 15 | 14 | use \tmukherjee13\migration\console\components\Formatter; |
| 16 | 15 | |
| 17 | 16 | /** |
@@ -70,16 +69,14 @@ discard block |
||
| 70 | 69 | /** |
| 71 | 70 | * @inheritdoc |
| 72 | 71 | */ |
| 73 | - public function options($actionID) |
|
| 74 | - { |
|
| 72 | + public function options($actionID) { |
|
| 75 | 73 | return array_merge(parent::options($actionID), ['migrationTable', 'db']); |
| 76 | 74 | } |
| 77 | 75 | |
| 78 | 76 | /** |
| 79 | 77 | * @inheritdoc |
| 80 | 78 | */ |
| 81 | - public function beforeAction($action) |
|
| 82 | - { |
|
| 79 | + public function beforeAction($action) { |
|
| 83 | 80 | |
| 84 | 81 | if (parent::beforeAction($action)) { |
| 85 | 82 | if (is_string($this->db)) { |
@@ -113,8 +110,7 @@ discard block |
||
| 113 | 110 | * Collects the foreign key column details for the given table. |
| 114 | 111 | * @param TableSchema $table the table metadata |
| 115 | 112 | */ |
| 116 | - protected function findConstraints($table) |
|
| 117 | - { |
|
| 113 | + protected function findConstraints($table) { |
|
| 118 | 114 | $sql = <<<SQL |
| 119 | 115 | SELECT |
| 120 | 116 | kcu.constraint_name, |
@@ -182,20 +178,19 @@ discard block |
||
| 182 | 178 | * @param TableSchema $table the table metadata |
| 183 | 179 | * @return string $sql the result of 'SHOW CREATE TABLE' |
| 184 | 180 | */ |
| 185 | - protected function getCreateTableSql($table) |
|
| 186 | - { |
|
| 181 | + protected function getCreateTableSql($table) { |
|
| 187 | 182 | $row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->quoteTableName($table->fullName))->queryOne(); |
| 188 | 183 | if (isset($row['Create Table'])) { |
| 189 | 184 | $sql = $row['Create Table']; |
| 190 | - } else { |
|
| 185 | + } |
|
| 186 | + else { |
|
| 191 | 187 | $row = array_values($row); |
| 192 | 188 | $sql = $row[1]; |
| 193 | 189 | } |
| 194 | 190 | return $sql; |
| 195 | 191 | } |
| 196 | 192 | |
| 197 | - public function quoteTableName($name) |
|
| 198 | - { |
|
| 193 | + public function quoteTableName($name) { |
|
| 199 | 194 | return strpos($name, '`') !== false ? $name : "`$name`"; |
| 200 | 195 | } |
| 201 | 196 | |
@@ -207,8 +202,7 @@ discard block |
||
| 207 | 202 | * @author Tarun Mukherjee (https://github.com/tmukherjee13) |
| 208 | 203 | */ |
| 209 | 204 | |
| 210 | - public function actionTable(array $tables) |
|
| 211 | - { |
|
| 205 | + public function actionTable(array $tables) { |
|
| 212 | 206 | |
| 213 | 207 | if ($this->confirm('Create the migration ' . "?")) { |
| 214 | 208 | |
@@ -286,8 +280,7 @@ discard block |
||
| 286 | 280 | * @param array $tables the list of tables |
| 287 | 281 | * @return integer|null |
| 288 | 282 | */ |
| 289 | - public function actionData(array $tables) |
|
| 290 | - { |
|
| 283 | + public function actionData(array $tables) { |
|
| 291 | 284 | |
| 292 | 285 | if ($this->confirm('Create the migration ' . "?", true)) { |
| 293 | 286 | foreach ($tables as $key => $args) { |
@@ -329,7 +322,8 @@ discard block |
||
| 329 | 322 | |
| 330 | 323 | if (empty($prepared_data)) { |
| 331 | 324 | $this->stdout("\nTable '{$table->name}' doesn't contain any data.\n\n", Console::FG_RED); |
| 332 | - } else { |
|
| 325 | + } |
|
| 326 | + else { |
|
| 333 | 327 | $pcolumns = $this->prepareColumns($pcolumns); |
| 334 | 328 | $prows = $this->prepareData($prepared_data); |
| 335 | 329 | |
@@ -349,8 +343,7 @@ discard block |
||
| 349 | 343 | * @param string $args the schema name |
| 350 | 344 | * @return integer |
| 351 | 345 | */ |
| 352 | - public function actionSchema($args) |
|
| 353 | - { |
|
| 346 | + public function actionSchema($args) { |
|
| 354 | 347 | |
| 355 | 348 | $schema = $args; |
| 356 | 349 | $this->class = 'dump_database_' . $schema; |
@@ -378,18 +371,15 @@ discard block |
||
| 378 | 371 | |
| 379 | 372 | } |
| 380 | 373 | |
| 381 | - public function getFileName() |
|
| 382 | - { |
|
| 374 | + public function getFileName() { |
|
| 383 | 375 | return 'm' . gmdate('ymd_His') . '_' . $this->class; |
| 384 | 376 | } |
| 385 | 377 | |
| 386 | - public function setFileName() |
|
| 387 | - { |
|
| 378 | + public function setFileName() { |
|
| 388 | 379 | $this->fileName = $this->getFileName(); |
| 389 | 380 | } |
| 390 | 381 | |
| 391 | - public function prepareFile($data) |
|
| 392 | - { |
|
| 382 | + public function prepareFile($data) { |
|
| 393 | 383 | if ($this->useDataMigrationFolder) { |
| 394 | 384 | FileHelper::createDirectory($this->migrationPath . DIRECTORY_SEPARATOR . $this->dataMigrationFolder); |
| 395 | 385 | } |
@@ -412,8 +402,7 @@ discard block |
||
| 412 | 402 | /** |
| 413 | 403 | * @inheritdoc |
| 414 | 404 | */ |
| 415 | - protected function getMigrationHistory($limit) |
|
| 416 | - { |
|
| 405 | + protected function getMigrationHistory($limit) { |
|
| 417 | 406 | if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) { |
| 418 | 407 | $this->createMigrationHistoryTable(); |
| 419 | 408 | } |
@@ -433,8 +422,7 @@ discard block |
||
| 433 | 422 | /** |
| 434 | 423 | * @inheritdoc |
| 435 | 424 | */ |
| 436 | - protected function addMigrationHistory($version) |
|
| 437 | - { |
|
| 425 | + protected function addMigrationHistory($version) { |
|
| 438 | 426 | $command = $this->db->createCommand(); |
| 439 | 427 | $command->insert($this->migrationTable, [ |
| 440 | 428 | 'version' => $version, |
@@ -445,8 +433,7 @@ discard block |
||
| 445 | 433 | /** |
| 446 | 434 | * @inheritdoc |
| 447 | 435 | */ |
| 448 | - protected function removeMigrationHistory($version) |
|
| 449 | - { |
|
| 436 | + protected function removeMigrationHistory($version) { |
|
| 450 | 437 | $command = $this->db->createCommand(); |
| 451 | 438 | $command->delete($this->migrationTable, [ |
| 452 | 439 | 'version' => $version, |
@@ -456,8 +443,7 @@ discard block |
||
| 456 | 443 | /** |
| 457 | 444 | * @inheritdoc |
| 458 | 445 | */ |
| 459 | - protected function parseFields() |
|
| 460 | - { |
|
| 446 | + protected function parseFields() { |
|
| 461 | 447 | $fields = []; |
| 462 | 448 | |
| 463 | 449 | foreach ($this->fields as $column => $schema) { |