Test Failed
Push — main ( e58605...0cf09f )
by Paul
13:40 queued 06:59
created

Migrate_7_1_0   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 24
c 1
b 0
f 1
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B migrateDatabase() 0 31 8
A run() 0 4 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Migrations;
4
5
use GeminiLabs\SiteReviews\Contracts\MigrateContract;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Database\Query;
8
9
class Migrate_7_1_0 implements MigrateContract
10
{
11
    public function run(): bool
12
    {
13
        $this->migrateDatabase();
14
        return true;
15
    }
16
17
    public function migrateDatabase(): void
18
    {
19
        $indexedColumns = [
20
            'is_approved',
21
            'is_flagged',
22
        ];
23
        $indexes = glsr(Database::class)->dbGetResults(
24
            glsr(Query::class)->sql("SHOW INDEXES FROM table|ratings")
25
        );
26
        $isDirty = false;
27
        foreach ($indexedColumns as $column) {
28
            $indexExists = false;
29
            foreach ($indexes as $index) {
30
                if ("glsr_ratings_{$column}_index" === $index->Key_name && $column === $index->Column_name) {
31
                    $indexExists = true;
32
                    break;
33
                }
34
            }
35
            if ($indexExists) {
36
                continue;
37
            }
38
            $sql = glsr(Query::class)->sql("
39
                ALTER TABLE table|ratings ADD INDEX glsr_ratings_{$column}_index ({$column})
40
            ");
41
            if (false === glsr(Database::class)->dbQuery($sql)) {
42
                $isDirty = true;
43
                glsr_log()->error("The ratings table could not be altered, the [is_approved] index was not added.");
44
            }
45
        }
46
        if (!$isDirty) {
47
            update_option(glsr()->prefix.'db_version', '1.4');
48
        }
49
    }
50
}
51