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