Completed
Pull Request — master (#862)
by Sean
03:40
created

MigrateDeploymentBranchTask   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 43
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 39 6
1
<?php
2
3
class MigrateDeploymentBranchTask extends BuildTask {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	public function run($request) {
6
		$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
45
}
46