@@ -31,14 +31,6 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
31
31
unset($ changed [$ key ]);
32
32
}
33
33
$ newColumn = clone $ current ;
34
- // $positionCurrent = $this->findPosition($desired, true);
35
- // $positionDesired = $this->findPosition($desired);
36
- // if ($positionCurrent === $positionDesired) {
37
- // $positionCurrent = $positionDesired = null;
38
- // } # else {
39
- // $position = $positionDesired;
40
- // $newColumn->position = $position;
41
- // }
42
34
foreach ($ changed as $ attr ) {
43
35
$ newColumn ->$ attr = $ desired ->$ attr ;
44
36
}
@@ -82,10 +74,7 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
82
74
}
83
75
}
84
76
85
- // $positionCurrent = $this->findPosition($desired, true);
86
- // $positionDesired = $this->findPosition($desired);
87
- // if ($positionCurrent !== $positionDesired) {
88
- if ($ desired ->isPositionChanged ) {
77
+ if (property_exists ($ desired , 'isPositionChanged ' ) && $ desired ->isPositionChanged ) {
89
78
$ changedAttributes [] = 'position ' ;
90
79
}
91
80
@@ -226,6 +215,8 @@ public function setColumnsPositions()
226
215
$ haveColumns = $ this ->tableSchema ->columns ;
227
216
$ wantNames = array_keys ($ this ->newColumns );
228
217
$ haveNames = array_keys ($ haveColumns );
218
+
219
+ // Part 1/2 compute from and to position
229
220
foreach ($ this ->newColumns as $ name => $ column ) {
230
221
/** @var \cebe\yii2openapi\db\ColumnSchema $column */
231
222
$ column ->toPosition = [
@@ -246,31 +237,32 @@ public function setColumnsPositions()
246
237
$ i ++;
247
238
}
248
239
240
+ // Part 2/2 compute is position is really changed
241
+
242
+ // check if only new columns are added without any explicit position change
243
+ $ namesForCreate = array_diff ($ wantNames , $ haveNames );
244
+ $ wantNamesWoNewCols = array_values (array_diff ($ wantNames , $ namesForCreate ));
245
+ if ($ namesForCreate && $ haveNames === $ wantNamesWoNewCols ) {
246
+ return ;
247
+ }
248
+ // check if only existing columns are deleted without any explicit position change
249
+ $ namesForDrop = array_diff ($ haveNames , $ wantNames );
250
+ $ haveNamesWoDropCols = array_values (array_diff ($ haveNames , $ namesForDrop ));
251
+ if ($ namesForDrop && $ wantNames === $ haveNamesWoDropCols ) {
252
+ return ;
253
+ }
254
+ // check both above simultaneously
255
+ if ($ namesForCreate && $ namesForDrop && ($ wantNamesWoNewCols === $ haveNamesWoDropCols )) {
256
+ return ;
257
+ }
258
+
249
259
$ takenIndices = [];
250
260
foreach ($ this ->newColumns as $ column ) {
251
261
/** @var \cebe\yii2openapi\db\ColumnSchema $column */
252
262
253
263
if (!$ column ->fromPosition || !$ column ->toPosition ) {
254
264
continue ;
255
265
}
256
-
257
- // check if only new columns are added without any explicit position change
258
- $ namesForCreate = array_diff ($ wantNames , $ haveNames );
259
- $ wantNamesWoNewCols = array_values (array_diff ($ wantNames , $ namesForCreate ));
260
- if ($ namesForCreate && $ haveNames === $ wantNamesWoNewCols ) {
261
- continue ;
262
- }
263
- // check if only existing columns are deleted without any explicit position change
264
- $ namesForDrop = array_diff ($ haveNames , $ wantNames );
265
- $ haveNamesWoDropCols = array_values (array_diff ($ haveNames , $ namesForDrop ));
266
- if ($ namesForDrop && $ wantNames === $ haveNamesWoDropCols ) {
267
- continue ;
268
- }
269
- // check both above simultaneously
270
- if ($ namesForCreate && $ namesForDrop && ($ wantNamesWoNewCols === $ haveNamesWoDropCols )) {
271
- continue ;
272
- }
273
-
274
266
if (is_int (array_search ([$ column ->toPosition ['index ' ], $ column ->fromPosition ['index ' ]], $ takenIndices ))) {
275
267
continue ;
276
268
}
@@ -285,42 +277,4 @@ public function setColumnsPositions()
285
277
$ takenIndices [] = [$ column ->fromPosition ['index ' ], $ column ->toPosition ['index ' ]];
286
278
}
287
279
}
288
-
289
- public function checkAfterPosition ($ column )
290
- {
291
- if ($ column ->fromPosition ['after ' ] === $ column ->toPosition ['after ' ]
292
- ) {
293
- $ afterColName = $ column ->toPosition ['after ' ];
294
- $ afterCol = $ this ->newColumns [$ afterColName ] ?? null ;
295
- if ($ afterCol ) {
296
- if ($ this ->checkAfterPosition ($ afterCol )) {
297
- return true ;
298
- } else {
299
- return false ;
300
- }
301
- } else {
302
- return true ;
303
- }
304
- }
305
- return false ;
306
- }
307
-
308
- public function checkBeforePosition ($ column )
309
- {
310
- if ($ column ->fromPosition ['before ' ] === $ column ->toPosition ['before ' ]
311
- ) {
312
- $ beforeColName = $ column ->toPosition ['before ' ];
313
- $ beforeCol = $ this ->newColumns [$ beforeColName ] ?? null ;
314
- if ($ beforeCol ) {
315
- if ($ this ->checkBeforePosition ($ beforeCol )) {
316
- return true ;
317
- } else {
318
- return false ;
319
- }
320
- } else {
321
- return true ;
322
- }
323
- }
324
- return false ;
325
- }
326
280
}
0 commit comments