@@ -31,14 +31,6 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
3131 unset($ changed [$ key ]);
3232 }
3333 $ 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- // }
4234 foreach ($ changed as $ attr ) {
4335 $ newColumn ->$ attr = $ desired ->$ attr ;
4436 }
@@ -82,10 +74,7 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
8274 }
8375 }
8476
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 ) {
8978 $ changedAttributes [] = 'position ' ;
9079 }
9180
@@ -226,6 +215,8 @@ public function setColumnsPositions()
226215 $ haveColumns = $ this ->tableSchema ->columns ;
227216 $ wantNames = array_keys ($ this ->newColumns );
228217 $ haveNames = array_keys ($ haveColumns );
218+
219+ // Part 1/2 compute from and to position
229220 foreach ($ this ->newColumns as $ name => $ column ) {
230221 /** @var \cebe\yii2openapi\db\ColumnSchema $column */
231222 $ column ->toPosition = [
@@ -246,31 +237,32 @@ public function setColumnsPositions()
246237 $ i ++;
247238 }
248239
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+
249259 $ takenIndices = [];
250260 foreach ($ this ->newColumns as $ column ) {
251261 /** @var \cebe\yii2openapi\db\ColumnSchema $column */
252262
253263 if (!$ column ->fromPosition || !$ column ->toPosition ) {
254264 continue ;
255265 }
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-
274266 if (is_int (array_search ([$ column ->toPosition ['index ' ], $ column ->fromPosition ['index ' ]], $ takenIndices ))) {
275267 continue ;
276268 }
@@ -285,42 +277,4 @@ public function setColumnsPositions()
285277 $ takenIndices [] = [$ column ->fromPosition ['index ' ], $ column ->toPosition ['index ' ]];
286278 }
287279 }
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- }
326280}
0 commit comments