1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\Support\Database; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Illuminate\Database\Migrations\Migration as IlluminateMigration; |
9
|
|
|
use Illuminate\Database\Schema\Builder; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Migration |
13
|
|
|
* |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class Migration extends IlluminateMigration |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Properties |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The table name. |
25
|
|
|
* |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
protected $table; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The table prefix. |
32
|
|
|
* |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected $prefix; |
36
|
|
|
|
37
|
|
|
/* ----------------------------------------------------------------- |
38
|
|
|
| Getters & Setters |
39
|
|
|
| ----------------------------------------------------------------- |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get a schema builder instance for the connection. |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Database\Schema\Builder |
46
|
|
|
*/ |
47
|
|
|
protected function getSchemaBuilder(): Builder |
48
|
|
|
{ |
49
|
|
|
/** @var \Illuminate\Database\DatabaseManager $db */ |
50
|
|
|
$db = app()->make('db'); |
51
|
|
|
|
52
|
|
|
return $db->connection($this->hasConnection() ? $this->getConnection() : null) |
53
|
|
|
->getSchemaBuilder(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the migration connection name. |
58
|
|
|
* |
59
|
|
|
* @param string $connection |
60
|
|
|
* |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function setConnection($connection) |
64
|
|
|
{ |
65
|
|
|
$this->connection = $connection; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the prefixed table name. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getTableName() |
76
|
|
|
{ |
77
|
|
|
return $this->hasPrefix() |
78
|
|
|
? $this->prefix.$this->table |
79
|
|
|
: $this->table; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set the table name. |
84
|
|
|
* |
85
|
|
|
* @param string $table |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function setTable($table) |
90
|
|
|
{ |
91
|
|
|
$this->table = $table; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the prefix name. |
98
|
|
|
* |
99
|
|
|
* @param string $prefix |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function setPrefix($prefix) |
104
|
|
|
{ |
105
|
|
|
$this->prefix = $prefix; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/* ----------------------------------------------------------------- |
111
|
|
|
| Main Methods |
112
|
|
|
| ----------------------------------------------------------------- |
113
|
|
|
*/ |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Migrate to database. |
117
|
|
|
*/ |
118
|
|
|
abstract public function up(): void; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Rollback the migration. |
122
|
|
|
*/ |
123
|
|
|
public function down(): void |
124
|
|
|
{ |
125
|
|
|
$this->getSchemaBuilder()->dropIfExists($this->getTableName()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Create Table Schema. |
130
|
|
|
* |
131
|
|
|
* @param \Closure $blueprint |
132
|
|
|
*/ |
133
|
|
|
protected function createSchema(Closure $blueprint): void |
134
|
|
|
{ |
135
|
|
|
$this->getSchemaBuilder()->create($this->getTableName(), $blueprint); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Modify a table on the schema. |
140
|
|
|
* |
141
|
|
|
* @param \Closure $callback |
142
|
|
|
*/ |
143
|
|
|
protected function table(Closure $callback): void |
144
|
|
|
{ |
145
|
|
|
$this->getSchemaBuilder()->table($this->getTableName(), $callback); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/* ----------------------------------------------------------------- |
149
|
|
|
| Check Methods |
150
|
|
|
| ----------------------------------------------------------------- |
151
|
|
|
*/ |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Check if connection exists. |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
protected function hasConnection(): bool |
159
|
|
|
{ |
160
|
|
|
return $this->isNotEmpty($this->getConnection()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Check if table has prefix. |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
protected function hasPrefix(): bool |
169
|
|
|
{ |
170
|
|
|
return $this->isNotEmpty($this->prefix); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Check if the value is not empty. |
175
|
|
|
* |
176
|
|
|
* @param string|null $value |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
private function isNotEmpty($value): bool |
181
|
|
|
{ |
182
|
|
|
return ! (is_null($value) || empty($value)); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|