Passed
Pull Request — master (#30)
by Mathieu
17:27 queued 07:27
created

MigrationModel::createSqliteMigrationTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate\Migrations;
6
7
use Suricate\DBObject;
8
9
class MigrationModel extends DBObject
10
{
11
    protected $tableName = 'suricate_migrations';
12
    protected $tableIndex = 'name';
13
14
    public function __construct()
15
    {
16
        parent::__construct();
17
18
        $this->dbVariables = [
19
            'name',
20
            'date_added',
21
        ];
22
23
        $this->readOnlyVariables = ['date_added'];
24
    }
25
26
    public function setDBConfig(string $config): self
27
    {
28
        $this->DBConfig = $config;
29
        return $this;
30
    }
31
32
    public function createMigrationTable(): int
33
    {
34
        $this->connectDB();
35
        if ($this->dbLink !== false) {
0 ignored issues
show
introduced by
The condition $this->dbLink !== false is always true.
Loading history...
36
37
            $dbParameters = $this->dbLink->getConfigParameters();
38
            switch ($dbParameters['type']) {
39
                case 'mysql':
40
                    if (!$this->doesMigrationTableExists()) {
41
                        $this->createMysqlMigrationTable();
42
                        return 1;
43
                    }
44
                    return -1;
45
46
                case 'sqlite':
47
                    if (!$this->doesMigrationTableExists()) {
48
                        $this->createSqliteMigrationTable();
49
                        return 1;
50
                    }
51
                    return -1;
52
            }
53
        }
54
55
        return 0;
56
    }
57
58
    private function createMysqlMigrationTable()
59
    {
60
        $sql = <<<EOD
61
        CREATE TABLE IF NOT EXISTS `{$this->tableName}` (
62
            `name` varchar(255) NOT NULL UNIQUE,
63
            `date_added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
64
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 
65
EOD;
66
67
        $this->dbLink->query($sql);
68
    }
69
70
    private function createSqliteMigrationTable()
71
    {
72
        // FIXME: test
73
        $sql = <<<EOD
74
        CREATE TABLE IF NOT EXISTS "{$this->tableName}" (
75
            "name"	TEXT NOT NULL UNIQUE,
76
            "date_added" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
77
        );
78
EOD;
79
        $this->dbLink->query($sql);
80
81
        return $sql;
82
    }
83
84
    private function doesMigrationTableExists(): bool
85
    {
86
        $dbParameters = $this->dbLink->getConfigParameters();
87
        $dbType = $dbParameters['type'];
88
89
        if ($dbType === 'sqlite') {
90
            $sql = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=:table";
91
            $sqlParams = [
92
                "table" => $this->getTableName()
93
            ];
94
95
            $res = $this->dbLink->query($sql, $sqlParams)->fetchColumn();
96
97
            return ((int) $res) === 1;
98
        }
99
100
101
        if ($dbType === 'mysql') {
102
            $sql = "SELECT count(*) FROM information_schema.tables " .
103
                " WHERE table_schema=:schema " .
104
                " AND table_name =:tableName ";
105
106
            $sqlParams = [
107
                "schema" => $dbParameters['database'],
108
                "tableName" => $this->getTableName()
109
            ];
110
            $res = $this->dbLink->query($sql, $sqlParams)->fetchColumn();
111
112
            return ((int) $res) === 1;
113
        }
114
115
        return true;
116
    }
117
}
118