-
Notifications
You must be signed in to change notification settings - Fork 0
/
MigrationManager.php
106 lines (91 loc) · 3.33 KB
/
MigrationManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
if (defined("__migrationmanager_php__")) return;
define("__migrationmanager_php__", true);
class MigrationManager
{
private $db;
function __construct()
{
$this->db = createDB();
}
function __destruct()
{
$this->db->close();
unset($this->db);
}
public function migrationList()
{
$data = array();
$q = "SHOW TABLES LIKE 'migrations'";
$res = $this->db->query($q);
$tableExists = ($this->db->numRows($res) > 0);
if($tableExists)
{
$q = " SELECT * FROM migrations ";
$res = $this->db->query($q);
if ($this->db->numRows($res) > 0) {
while ($row = $this->db->fetchArray($res)) {
$data[] = array(
'id' => $row['id'],
'migration' => $row['migration'],
'batch' => $row['batch'],
'created_at' => $row['created_at'],
);
}
}
}
return $data;
}
public function maxMigrationBatch()
{
$data = 0;
$q = " SELECT max(batch) as max_batch FROM migrations";
$row = $this->db->querySingleRow($q);
if(!empty($row))
$data = $row['max_batch'];
return $data;
}
public function maxMigrationList()
{
$data = array();
$q = " SELECT * FROM migrations WHERE batch = (SELECT MAX(batch) AS max_batch FROM migrations)";
$res = $this->db->query($q);
if($this->db->numRows($res)>0)
{
while ($row = $this->db->fetchArray($res))
{
$data[] = array(
'id' => $row['id'],
'migration' => $row['migration'],
'batch' => $row['batch'],
'created_at' => $row['created_at'],
);
}
}
return $data;
}
public function migrationSetting($migration, $batch)
{
// Check if the migrations table exists
$q = "SHOW TABLES LIKE 'migrations'";
$res = $this->db->query($q);
$tableExists = ($this->db->numRows($res) > 0);
// If the migrations table doesn't exist, create it
if(!$tableExists) {
$q = " CREATE TABLE migrations (
id INT AUTO_INCREMENT PRIMARY KEY,
migration VARCHAR(255) NOT NULL,
batch INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
); ";
$this->db->execQuery($q);
}
$q = " SELECT max(batch) as max_batch FROM migrations ";
$row = $this->db->querySingleRow($q);
$h = new XwQueryHelper();
$h->add("migration", $migration, true);
$h->add("batch", $batch, false);
$q = $h->getInsertQuery('migrations');
$this->db->execQuery($q);
}
}