FixLongPmaVersions::_sanitizeVersionsInTable()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 25
rs 9.7666
c 2
b 0
f 0
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