@@ -23,16 +23,32 @@ final class MysqlMigrationBuilder extends BaseMigrationBuilder
23
23
*/
24
24
protected function buildColumnChanges (ColumnSchema $ current , ColumnSchema $ desired , array $ changed ):void
25
25
{
26
+ $ positionCurrent = $ positionDesired = null ;
27
+ if (in_array ('position ' , $ changed , true )) {
28
+ $ positionCurrent = $ this ->findPosition ($ current , true );
29
+ $ positionDesired = $ this ->findPosition ($ desired );
30
+ $ key = array_search ('position ' , $ changed , true );
31
+ if ($ key !== false ) {
32
+ unset($ changed [$ key ]);
33
+ }
34
+ }
26
35
$ newColumn = clone $ current ;
27
- $ position = $ this ->findPosition ($ desired );
36
+ // $positionCurrent = $this->findPosition($desired, true);
37
+ // $positionDesired = $this->findPosition($desired);
38
+ // if ($positionCurrent === $positionDesired) {
39
+ // $positionCurrent = $positionDesired = null;
40
+ // } # else {
41
+ // $position = $positionDesired;
42
+ // $newColumn->position = $position;
43
+ // }
28
44
foreach ($ changed as $ attr ) {
29
45
$ newColumn ->$ attr = $ desired ->$ attr ;
30
46
}
31
47
if (static ::isEnum ($ newColumn )) {
32
48
$ newColumn ->dbType = 'enum ' ; // TODO this is concretely not correct
33
49
}
34
- $ this ->migration ->addUpCode ($ this ->recordBuilder ->alterColumn ($ this ->model ->getTableAlias (), $ newColumn , $ position ))
35
- ->addDownCode ($ this ->recordBuilder ->alterColumn ($ this ->model ->getTableAlias (), $ current , $ position ));
50
+ $ this ->migration ->addUpCode ($ this ->recordBuilder ->alterColumn ($ this ->model ->getTableAlias (), $ newColumn , $ positionDesired ))
51
+ ->addDownCode ($ this ->recordBuilder ->alterColumn ($ this ->model ->getTableAlias (), $ current , $ positionCurrent ));
36
52
}
37
53
38
54
protected function compareColumns (ColumnSchema $ current , ColumnSchema $ desired ):array
@@ -67,6 +83,14 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
67
83
}
68
84
}
69
85
}
86
+
87
+ $ positionCurrent = $ this ->findPosition ($ desired , true );
88
+ $ positionDesired = $ this ->findPosition ($ desired );
89
+
90
+ if ($ positionCurrent !== $ positionDesired ) {
91
+ $ changedAttributes [] = 'position ' ;
92
+ }
93
+
70
94
return $ changedAttributes ;
71
95
}
72
96
@@ -168,31 +192,8 @@ public function checkOrder()
168
192
{
169
193
}
170
194
171
- // TODO
172
- public function handleColumnsPositionsChanges (array $ haveNames , array $ wantNames )
173
- {
174
- $ indices = [];
175
- if ($ haveNames !== $ wantNames ) {
176
- foreach ($ wantNames as $ key => $ name ) {
177
- if ($ name !== $ haveNames [$ key ]) {
178
- $ indices [] = $ key ;
179
- }
180
- }
181
- }
182
- for ($ i = 0 ; $ i < count ($ indices ) / 2 ; $ i ++) {
183
- $ this ->migration ->addUpCode ($ this ->recordBuilder ->alterColumn ());
184
- }
185
- }
186
-
187
-
188
195
/**
189
- * TODO move this method to MysqlMigrationBuilder
190
- * Only for MySQL and MariaDB
191
- * Given a column, compute its previous column name present in OpenAPI schema
192
- * @return ?string
193
- * `null` if column is added at last
194
- * 'FIRST' if column is added at first position
195
- * 'AFTER <columnName>' if column is added in between e.g. if 'email' is added after 'username' then 'AFTER username'
196
+ * {@inheritDoc}
196
197
*/
197
198
public function findPosition (ColumnSchema $ column , bool $ forDrop = false ): ?string
198
199
{
@@ -218,4 +219,30 @@ public function findPosition(ColumnSchema $column, bool $forDrop = false): ?stri
218
219
219
220
return null ;
220
221
}
222
+
223
+
224
+ // TODO
225
+ public function handleColumnsPositionsChanges (array $ haveNames , array $ wantNames )
226
+ {
227
+ $ indices = [];
228
+ if ($ haveNames !== $ wantNames ) {
229
+ foreach ($ wantNames as $ key => $ name ) {
230
+ if ($ name !== $ haveNames [$ key ]) {
231
+ $ indices [] = $ key ;
232
+ }
233
+ }
234
+ }
235
+ for ($ i = 0 ; $ i < count ($ indices ) / 2 ; $ i ++) {
236
+ $ this ->migration ->addUpCode ($ this ->recordBuilder ->alterColumn (
237
+ $ this ->model ->getTableAlias (),
238
+ $ this ->newColumns [$ wantNames [$ indices [$ i ]]],
239
+ $ this ->findPosition ($ this ->newColumns [$ wantNames [$ indices [$ i ]]])
240
+ ))->addDownCode ($ this ->recordBuilder ->alterColumn (
241
+ $ this ->model ->getTableAlias (),
242
+ $ this ->tableSchema ->columns [$ wantNames [$ indices [$ i ]]],
243
+ $ this ->findPosition ($ this ->tableSchema ->columns [$ wantNames [$ indices [$ i ]]], true )
244
+ ));
245
+ }
246
+ // $this->migration->addUpCode($this->recordBuilder->dropTable($this->model->getTableAlias()));
247
+ }
221
248
}
0 commit comments