Test Failed
Push — main ( 0cf09f...7cd423 )
by Paul
13:14 queued 06:41
created

Migrate_7_1_0::migrateDatabase()   A

Complexity

Conditions 6
Paths 18

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 16
c 2
b 1
f 1
dl 0
loc 27
rs 9.1111
cc 6
nc 18
nop 0
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
        $isDirty = false;
20
        $indexes = glsr(Database::class)->dbGetResults(
21
            glsr(Query::class)->sql("SHOW INDEXES FROM table|ratings")
22
        );
23
        $keyNames = wp_list_pluck($indexes, 'Key_name');
24
        if (!in_array('glsr_ratings_rating_type_is_approved_index', $keyNames)) {
25
            $sql = glsr(Query::class)->sql("
26
                ALTER TABLE table|ratings ADD INDEX glsr_ratings_rating_type_is_approved_index (rating,type,is_approved)
27
            ");
28
            if (false === glsr(Database::class)->dbQuery($sql)) {
29
                $isDirty = true;
30
                glsr_log()->error("The ratings table could not be altered, the [rating_type_is_approved] index was not added.");
31
            }
32
        }
33
        if (!in_array('glsr_ratings_is_flagged_index', $keyNames)) {
34
            $sql = glsr(Query::class)->sql("
35
                ALTER TABLE table|ratings ADD INDEX glsr_ratings_is_flagged_index (is_flagged)
36
            ");
37
            if (false === glsr(Database::class)->dbQuery($sql)) {
38
                $isDirty = true;
39
                glsr_log()->error("The ratings table could not be altered, the [is_flagged] index was not added.");
40
            }
41
        }
42
        if (!$isDirty) {
43
            update_option(glsr()->prefix.'db_version', '1.4');
44
        }
45
    }
46
}
47