Completed
Pull Request — master (#147)
by Deven
03:17
created

FixLongPmaVersions   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 10 1
A down() 0 5 1
B _sanitizeVersionsInTable() 0 25 3
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