FixLongPmaVersions   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 48
rs 10
c 2
b 0
f 0
wmc 5

3 Methods

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