Completed
Pull Request — master (#586)
by Sean
03:25
created

MigrateDeploymentStatusTask::run()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 38
rs 4.909
cc 9
eloc 23
nc 8
nop 1
1
<?php
2
3
class MigrateDeploymentStatusTask extends BuildTask {
4
5
	public function run($request) {
6
		$log = function($message) {
7
			$message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message;
8
			echo Director::is_cli() ? ($message . PHP_EOL) : ($message . '<br>');
9
		};
10
11
		foreach (DB::query('SELECT "ID", "Status", "State" FROM "DNDeployment"') as $record) {
12
			// This record looks to be already migrated
13
			if (empty($record['Status'])) {
14
				continue;
15
			}
16
17
			$obj = DNDeployment::get()->byId($record['ID']);
18
			if (!$obj) {
19
				$logFn(sprintf('Failed to get DNDeployment object for record ID %s', $record['ID']));
0 ignored issues
show
Bug introduced by
The variable $logFn does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
20
				continue;
21
			}
22
			if ($record['Status'] === 'Queued') {
23
				$obj->State = 'Queued';
24
			} elseif ($record['Status'] === 'Started') {
25
				$obj->State = 'Deploying';
26
			} elseif ($record['Status'] === 'Finished') {
27
				$obj->State = 'Completed';
28
			} elseif ($record['Status'] === 'Failed') {
29
				$obj->State = 'Failed';
30
			}
31
			$obj->write();
32
33
			// Blank out the old column, as it has been successfully migrated.
34
			// Note that the field no longer exists on the DNDeployment object, so we have to
35
			// manually run the update on the database.
36
			DB::query(sprintf('UPDATE "DNDeployment" SET "Status" = NULL WHERE "ID" = %s', $record['ID']));
37
38
			$log(sprintf('DNDeployment record %s has been successfully migrated', $record['ID']));
39
		}
40
41
		$log('Finished. Please delete the DNDeployment.Status column from the database');
42
	}
43
44
}
45