-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractMigrate.php
executable file
·110 lines (98 loc) · 2.29 KB
/
AbstractMigrate.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
107
108
109
110
<?php
declare(strict_types=1);
namespace MaplePHP\Query;
//use MaplePHP\Query\Create;
use MaplePHP\Query\Interfaces\MigrateInterface;
use Exception;
abstract class AbstractMigrate implements MigrateInterface
{
protected $mig;
protected $table;
abstract protected function migrateTable();
public function __construct(string $table, ?string $prefix = null)
{
/*
if (is_null($prefix)) {
$prefix = getenv("MYSQL_PREFIX");
if ($prefix === false) {
throw new Exception("Table prefix is required!", 1);
}
}
*/
$this->mig = new Create($table, $prefix);
$this->table = $table;
}
/**
* Get build data and
* @return Create
*/
public function getBuild(): Create
{
$this->migrateTable();
return $this->mig;
}
/**
* Get build data and
* @return string
*/
public function getTable(): string
{
return $this->table;
}
/**
* Will drop table when method execute is triggered
* @return array
*/
public function drop(): array
{
$this->mig->drop();
return $this->mig->execute();
}
/**
* Get migration data
* @return array
*/
public function getData(): array
{
$this->migrateTable();
return $this->mig->getColumns();
}
/**
* Read migration changes (before executing)
* @return string
*/
public function read(): string
{
$this->mig->auto();
$this->migrateTable();
return $this->mig->build();
}
/**
* Will create/alter all table
* @return array
*/
public function create(): array
{
$this->mig->auto();
$this->migrateTable();
return $this->mig->execute();
}
/**
* Get message
* @param array $error
* @param string $success
* @return string
*/
public function getMessage(array $error, string $success = "Success!"): string
{
if (count($error) > 0) {
$sqlMessage = "";
foreach ($error as $key => $val) {
$sqlMessage .= "{$key}: {$val}\n";
}
} else {
$sqlMessage = $success;
}
return $sqlMessage;
}
}