|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\Migrations\V2; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Database\Schema\AbstractTable; |
|
8
|
|
|
use Cycle\Migrations\CapsuleInterface; |
|
9
|
|
|
use Cycle\Migrations\Exception\BlueprintException; |
|
10
|
|
|
use Cycle\Migrations\OperationInterface; |
|
11
|
|
|
use Cycle\Migrations\Operation; |
|
12
|
|
|
|
|
13
|
|
|
final class TableBlueprint |
|
14
|
|
|
{ |
|
15
|
|
|
protected string $tableName = ''; |
|
16
|
|
|
private CapsuleInterface $capsule; |
|
17
|
|
|
private array $operations = []; |
|
18
|
|
|
private bool $executed = false; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(string $tableName, CapsuleInterface $capsule) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->tableName = $tableName; |
|
23
|
|
|
$this->capsule = $capsule; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getSchema(): AbstractTable |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->capsule->getSchema($this->tableName); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function addColumns(array $columns): self |
|
32
|
|
|
{ |
|
33
|
|
|
foreach ($columns as $name => $column) { |
|
34
|
|
|
$columnParser = new ColumnParser($column); |
|
35
|
|
|
$this->addOperation( |
|
36
|
|
|
new Operation\Column\Add( |
|
37
|
|
|
$this->tableName, |
|
38
|
|
|
$name, |
|
39
|
|
|
$columnParser->getType(), |
|
40
|
|
|
$columnParser->getOptions() |
|
41
|
|
|
) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function alterColumns(array $columns): self |
|
49
|
|
|
{ |
|
50
|
|
|
foreach ($columns as $name => $column) { |
|
51
|
|
|
$columnParser = new ColumnParser($column); |
|
52
|
|
|
$this->addOperation( |
|
53
|
|
|
new Operation\Column\Alter( |
|
54
|
|
|
$this->tableName, |
|
55
|
|
|
$name, |
|
56
|
|
|
$columnParser->getType(), |
|
57
|
|
|
$columnParser->getOptions() |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function renameColumn(string $name, string $newName): self |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->addOperation( |
|
68
|
|
|
new Operation\Column\Rename($this->tableName, $name, $newName) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function dropColumns(array $columnsName = []): self |
|
73
|
|
|
{ |
|
74
|
|
|
foreach ($columnsName as $columnName) { |
|
75
|
|
|
$this->addOperation( |
|
76
|
|
|
new Operation\Column\Drop($this->tableName, $columnName) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function addIndexes(array $indexes): self |
|
84
|
|
|
{ |
|
85
|
|
|
foreach ($indexes as $index) { |
|
86
|
|
|
$indexParser = new IndexParser($index, $this->tableName); |
|
87
|
|
|
|
|
88
|
|
|
$this->addOperation( |
|
89
|
|
|
new Operation\Index\Add( |
|
90
|
|
|
$this->tableName, |
|
91
|
|
|
$indexParser->getField(), |
|
92
|
|
|
$indexParser->getOptions() |
|
93
|
|
|
) |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function alterIndexes(array $indexes): self |
|
101
|
|
|
{ |
|
102
|
|
|
foreach ($indexes as $index) { |
|
103
|
|
|
$indexParser = new IndexParser($index, $this->tableName); |
|
104
|
|
|
|
|
105
|
|
|
$this->addOperation( |
|
106
|
|
|
new Operation\Index\Alter( |
|
107
|
|
|
$this->tableName, |
|
108
|
|
|
$indexParser->getField(), |
|
109
|
|
|
$indexParser->getOptions() |
|
110
|
|
|
) |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @example [['email'], ['phone', 'created_at']] - drop two indexes |
|
119
|
|
|
* @param array $indexes |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
|
|
public function dropIndexesByColumns(array $indexes): self |
|
123
|
|
|
{ |
|
124
|
|
|
foreach ($indexes as $columns) { |
|
125
|
|
|
$this->addOperation( |
|
126
|
|
|
new Operation\Index\Drop($this->tableName, $columns) |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function addForeignKeys(array $foreignKeys): self |
|
134
|
|
|
{ |
|
135
|
|
|
foreach ($foreignKeys as $foreignKey) { |
|
136
|
|
|
$fkParser = new ForeignKeyParser($foreignKey, $this->tableName); |
|
137
|
|
|
|
|
138
|
|
|
$this->addOperation( |
|
139
|
|
|
new Operation\ForeignKey\Add( |
|
140
|
|
|
$this->tableName, |
|
141
|
|
|
$fkParser->getInnerKeys(), |
|
142
|
|
|
$fkParser->getOuterTable(), |
|
143
|
|
|
$fkParser->getOuterKeys(), |
|
144
|
|
|
$fkParser->getOptions() |
|
145
|
|
|
) |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @example [['email'], ['phone', 'created_at']] - drop two foreignKeys |
|
154
|
|
|
* @param array $foreignKeys |
|
155
|
|
|
* @return $this |
|
156
|
|
|
*/ |
|
157
|
|
|
public function dropForeignKeysByColumns(array $foreignKeys): self |
|
158
|
|
|
{ |
|
159
|
|
|
foreach ($foreignKeys as $columns) { |
|
160
|
|
|
$this->addOperation( |
|
161
|
|
|
new Operation\ForeignKey\Drop($this->tableName, $columns) |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function setPrimaryKeys(array $keys): self |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->addOperation( |
|
171
|
|
|
new Operation\Table\PrimaryKeys($this->tableName, $keys) |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function create(): void |
|
176
|
|
|
{ |
|
177
|
|
|
$this->addOperation( |
|
178
|
|
|
new Operation\Table\Create($this->tableName) |
|
179
|
|
|
); |
|
180
|
|
|
|
|
181
|
|
|
$this->execute(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function update(): void |
|
185
|
|
|
{ |
|
186
|
|
|
$this->addOperation( |
|
187
|
|
|
new Operation\Table\Update($this->tableName) |
|
188
|
|
|
); |
|
189
|
|
|
|
|
190
|
|
|
$this->execute(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function rename(string $newName): void |
|
194
|
|
|
{ |
|
195
|
|
|
$this->addOperation( |
|
196
|
|
|
new Operation\Table\Rename($this->tableName, $newName) |
|
197
|
|
|
); |
|
198
|
|
|
|
|
199
|
|
|
$this->execute(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function drop(): void |
|
203
|
|
|
{ |
|
204
|
|
|
$this->addOperation( |
|
205
|
|
|
new Operation\Table\Drop($this->tableName) |
|
206
|
|
|
); |
|
207
|
|
|
|
|
208
|
|
|
$this->execute(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function truncate(): void |
|
212
|
|
|
{ |
|
213
|
|
|
$this->addOperation( |
|
214
|
|
|
new Operation\Table\Truncate($this->tableName) |
|
215
|
|
|
); |
|
216
|
|
|
|
|
217
|
|
|
$this->execute(); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function addOperation(OperationInterface $operation): self |
|
221
|
|
|
{ |
|
222
|
|
|
$this->operations[] = $operation; |
|
223
|
|
|
|
|
224
|
|
|
return $this; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
private function execute(): void |
|
228
|
|
|
{ |
|
229
|
|
|
if ($this->executed) { |
|
230
|
|
|
throw new BlueprintException('Only one create/update/rename/drop is allowed per blueprint.'); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
$this->capsule->execute($this->operations); |
|
234
|
|
|
$this->executed = true; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|