Completed
Pull Request — master (#153)
by Deven
16:32
created

_migrateBasedOnGithubLinked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
use Migrations\AbstractMigration;
4
use Cake\Log\Log;
5
6
class FixStaleReportStates extends AbstractMigration
7
{
8
    /**
9
     * This migration changes the old report statuses to more
10
     * Github-related status
11
     *
12
     */
13
    public function up()
14
    {
15
        $this->_migrateBasedOnGithubLinked();
16
        $this->_migrateToNewStatus();
17
        $this->_migrateDuplicateReports();
18
    }
19
20
    private function _migrateBasedOnGithubLinked()
21
    {
22
        $sql = 'UPDATE `reports` SET `status` = \'resolved\''
23
            . ' WHERE `sourceforge_bug_id` IS NOT NULL AND `status` <> \'fixed\'';
24
        $rowsAffected = $this->execute($sql);
25
26
        Log::debug(
27
            $rowsAffected . ' reports are linked to an'
28
                . ' open Github issue.'
29
                . ' These have been marked to have a \'forwarded\' status.'
30
        );
31
    }
32
33
    private function _migrateToNewStatus()
34
    {
35
        $statusMap = array(
36
            'fixed' => 'resolved',
37
            'open' => 'new',
38
            'out-of-date' => 'invalid',
39
            'works-for-me' => 'invalid',
40
            'wontfix' => 'invalid'
41
        );
42
43
        foreach ($statusMap as $oldStatus => $newStatus) {
44
            $sql = 'UPDATE `reports` SET `status` = \'' . $newStatus . '\''
45
                . ' WHERE `status` = \'' . $oldStatus . '\'';
46
            $rowsAffected = $this->execute($sql);
47
48
            Log::debug(
49
                $rowsAffected . ' reports with \'' . $oldStatus . '\''
50
                    . ' were mapped to \'' . $newStatus . '\'.'
51
            );
52
        }
53
    }
54
55
    private function _migrateDuplicateReports()
56
    {
57
        // Find the original reports and set the status
58
        // of their duplicate reports same as their status
59
        $sql = 'SELECT `id`, `status` FROM `reports` WHERE'
60
            . ' `related_to` IS NULL';
61
        $origCount = 0;
62
        $result = $this->query($sql);
63
        $rowsAffected = 0;
64
65
        while ($row = $result->fetch()) {
66
            $update_sql = 'UPDATE `reports` SET `status` = \''
67
                . $row['status'] . '\' WHERE `id` = \''
68
                . $row['id'] . '\'';
69
70
            $rowsAffected += $this->execute($update_sql);
71
            $origCount++;
72
        }
73
74
        Log::debug(
75
            $rowsAffected . ' duplicate reports were mapped to '
76
                . ' same status as their ' . $origCount .' original reports'
77
        );
78
    }
79
}
80