Passed
Push — master ( 2da942...d633d7 )
by Paul
15:03 queued 05:40
created

Migrate_5_9_0::migrateDatabase()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 16
ccs 8
cts 10
cp 0.8
rs 9.9666
cc 3
nc 3
nop 0
crap 3.072
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Migrations;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Database\CountManager;
7
use GeminiLabs\SiteReviews\Database\Query;
8
use GeminiLabs\SiteReviews\Database\SqlSchema;
9
use GeminiLabs\SiteReviews\Install;
10
11
class Migrate_5_9_0
12
{
13
    /**
14
     * @return bool
15
     */
16 7
    public function migrateDatabase()
17
    {
18 7
        $table = glsr(SqlSchema::class)->table('ratings');
19 7
        if ($this->isDatabaseVersionUpdated()) {
20 6
            return true;
21
        }
22 1
        glsr(Database::class)->dbQuery(glsr(Query::class)->sql("
23 1
            ALTER TABLE {$table}
24
            ADD terms tinyint(1) NOT NULL DEFAULT '1'
25
            AFTER url
26
        "));
27 1
        if ($this->isDatabaseVersionUpdated()) {
28 1
            return true;
29
        }
30
        glsr_log()->error(sprintf('Database table [%s] could not be altered, column [terms] not added.', $table));
31
        return false;
32
    }
33
34
    /**
35
     * @return void
36
     */
37 7
    public function repairDatabase()
38
    {
39 7
        require_once ABSPATH.'/wp-admin/includes/plugin.php';
40 7
        if (is_plugin_active_for_network(plugin_basename(glsr()->file))) {
41
            foreach (glsr(Install::class)->sites() as $siteId) {
42
                switch_to_blog($siteId);
43
                $this->install();
44
                restore_current_blog();
45
            }
46
            return;
47
        }
48 7
        $this->install();
49 7
    }
50
51
    /**
52
     * @return bool
53
     */
54 7
    public function run()
55
    {
56 7
        $this->repairDatabase(); // fix orphaned rows and foreign indexes
57 7
        glsr(CountManager::class)->recalculate();
58 7
        return $this->migrateDatabase();
59
    }
60
61
    /**
62
     * @return void
63
     */
64 7
    protected function install()
65
    {
66 7
        glsr(SqlSchema::class)->createTables();
67 7
        glsr(SqlSchema::class)->addForeignConstraints();
68 7
        glsr(Database::class)->deleteInvalidPostAssignments();
69 7
        glsr(Database::class)->deleteInvalidTermAssignments();
70 7
        glsr(Database::class)->deleteInvalidUserAssignments();
71 7
    }
72
73
    /**
74
     * @return bool
75
     */
76 7
    protected function isDatabaseVersionUpdated()
77
    {
78 7
        $table = glsr(SqlSchema::class)->table('ratings');
79 7
        if (glsr(SqlSchema::class)->columnExists($table, 'terms')) {
80 7
            if (!glsr(Database::class)->version('1.1')) {
81
                update_option(glsr()->prefix.'db_version', '1.1');
82
            }
83 7
            return true;
84
        }
85 1
        return false;
86
    }
87
88
}
89