Completed
Pull Request — master (#147)
by Deven
05:23 queued 02:26
created

FixLongPmaVersions::_sanitizeVersionsInTable()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 1
1
<?php
2
3
use Phinx\Migration\AbstractMigration;
4
use Cake\ORM\TableRegistry;
5
use Cake\Log\Log;
6
7
class FixLongPmaVersions extends AbstractMigration
8
{
9
10
    /**
11
     * This migration strips the phpMyAdmin version of Debian,
12
     * Ubuntu and other suffixes in version strings
13
     *
14
     */
15
    public function up()
16
    {
17
        // Strip phpMyAdmin versions in `reports` table
18
        $count = $this->_sanitizeVersionsInTable('reports');
19
        Log::debug($count . ' reports updated');
20
21
        // Strip phpMyAdmin versions in `incidents` table
22
        $count = $this->_sanitizeVersionsInTable('incidents');
23
        Log::debug($count . ' incidents updated');
24
    }
25
26
    public function down()
27
    {
28
        // Once applied, this migration can't be reversed
29
        // but the migration itself is idempotent
30
    }
31
32
    private function _sanitizeVersionsInTable($table)
33
    {
34
        $sql = 'SELECT `id`, `pma_version` FROM `' . $table . '`';
35
        $count = 0;
36
        $incidentsTable = TableRegistry::get('Incidents');
37
        $result = $this->query($sql);
38
39
        while ($row = $result->fetch()) {
40
            $strippedVersionString
41
                = $incidentsTable->getStrippedPmaVersion(
42
                        $row['pma_version']
43
                    );
44
45
            if ($strippedVersionString !== $row['pma_version']) {
46
                $this->execute('UPDATE ' . $table . ' SET `pma_version` = \''
47
                    . $strippedVersionString . '\' WHERE `id` = \''
48
                    . $row['id'] . '\''
49
                );
50
51
                ++$count;
52
            }
53
        }
54
55
        return $count;
56
    }
57
}
58