Test Failed
Push — develop ( 33b521...cdbd76 )
by Paul
10:16 queued 21s
created

TableStats::removeInvalidRows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database\Tables;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Database\Query;
7
use GeminiLabs\SiteReviews\Database\Tables\AbstractTable;
8
9
class TableStats extends AbstractTable
10
{
11
    public string $name = 'stats';
12
13
    public function addForeignConstraints(): void
14
    {
15
        $this->addForeignConstraint('rating_id', $this->table('ratings'), 'ID');
16
    }
17
18
    public function dropForeignConstraints(): void
19
    {
20
        $this->dropForeignConstraint('rating_id', $this->table('ratings'));
21
    }
22
23
    public function removeInvalidRows(): void
24
    {
25
        glsr(Database::class)->dbSafeQuery(
26
            glsr(Query::class)->sql("
27
                DELETE t
28
                FROM {$this->tablename} AS t
29
                LEFT JOIN table|ratings AS r ON (r.ID = t.rating_id)
30
                WHERE r.ID IS NULL
31
            ")
32
        );
33
    }
34
35
    /**
36
     * WordPress codex says there must be two spaces between PRIMARY KEY and the key definition.
37
     *
38
     * @see https://codex.wordpress.org/Creating_Tables_with_Plugins
39
     */
40
    public function structure(): string
41
    {
42
        return glsr(Query::class)->sql("
43
            CREATE TABLE {$this->tablename} (
44
                ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
45
                rating_id bigint(20) unsigned NOT NULL,
46
                continent varchar(10) NOT NULL,
47
                country varchar(10) NOT NULL,
48
                region varchar(10) NOT NULL,
49
                city varchar(250) NOT NULL,
50
                PRIMARY KEY  (ID),
51
                UNIQUE KEY glsr_stats_rating_id_unique (rating_id)
52
            ) ENGINE=InnoDB {$this->db->get_charset_collate()};
53
        ");
54
    }
55
}
56