| Conditions | 6 |
| Paths | 6 |
| Total Lines | 39 |
| Code Lines | 22 |
| Lines | 4 |
| Ratio | 10.26 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | public function run($request) { |
||
| 6 | View Code Duplication | $log = function($message) { |
|
| 7 | $message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message; |
||
| 8 | echo $message . PHP_EOL; |
||
| 9 | }; |
||
| 10 | |||
| 11 | if (!Director::is_cli()) { |
||
| 12 | $log('This command can only run via CLI'); |
||
| 13 | return; |
||
| 14 | } |
||
| 15 | |||
| 16 | $columns = DB::query('SHOW COLUMNS FROM "DNDeployment"')->column(); |
||
| 17 | if (!in_array('Branch', $columns)) { |
||
| 18 | $log('Migration has already been run'); |
||
| 19 | return; |
||
| 20 | } |
||
| 21 | |||
| 22 | if (!in_array('RefName', $columns)) { |
||
| 23 | $log('RefName column doesn\'t exist. Has dev/build been run?'); |
||
| 24 | return; |
||
| 25 | } |
||
| 26 | |||
| 27 | foreach (DB::query('SELECT "ID", "Branch" FROM "DNDeployment"') as $record) { |
||
| 28 | // This record looks to be already migrated or doesn't have a value to begin with. Skip. |
||
| 29 | if (empty($record['Branch'])) { |
||
| 30 | continue; |
||
| 31 | } |
||
| 32 | |||
| 33 | DB::query(sprintf('UPDATE "DNDeployment" SET "RefName" = \'%s\' WHERE "ID" = %s', $record['Branch'], $record['ID'])); |
||
| 34 | |||
| 35 | // Blank out the old column, as it has been successfully migrated. |
||
| 36 | DB::query(sprintf('UPDATE "DNDeployment" SET "Branch" = NULL WHERE "ID" = %s', $record['ID'])); |
||
| 37 | |||
| 38 | $log(sprintf('DNDeployment record %s has been successfully migrated', $record['ID'])); |
||
| 39 | } |
||
| 40 | |||
| 41 | DB::query('ALTER TABLE "DNDeployment" DROP COLUMN "Branch"'); |
||
| 42 | $log('Finished'); |
||
| 43 | } |
||
| 44 | |||
| 46 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.