@@ -84,10 +84,10 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
8484 }
8585 }
8686
87- $ positionCurrent = $ this ->findPosition ($ desired , true );
88- $ positionDesired = $ this ->findPosition ($ desired );
89-
90- if ($ positionCurrent !== $ positionDesired ) {
87+ // $positionCurrent = $this->findPosition($desired, true);
88+ // $positionDesired = $this->findPosition($desired);
89+ // if ($positionCurrent !== $positionDesired) {
90+ if ($ desired -> isPositionReallyChanged ) {
9191 $ changedAttributes [] = 'position ' ;
9292 }
9393
@@ -245,4 +245,69 @@ public function handleColumnsPositionsChanges(array $haveNames, array $wantNames
245245 }
246246// $this->migration->addUpCode($this->recordBuilder->dropTable($this->model->getTableAlias()));
247247 }
248+
249+ public function setPositions ()
250+ {
251+ $ i = 0 ;
252+ $ haveColumns = $ this ->tableSchema ->columns ;
253+ $ onlyColumnNames = array_keys ($ this ->newColumns );
254+ foreach ($ this ->newColumns as $ columnName => $ column ) {
255+ /** @var \cebe\yii2openapi\db\ColumnSchema $column */
256+ $ column ->toPosition = [
257+ 'index ' => $ i + 1 ,
258+ 'after ' => $ i === 0 ? null : $ onlyColumnNames [$ i - 1 ],
259+ 'before ' => $ i === (count ($ onlyColumnNames ) - 1 ) ? null : $ onlyColumnNames [$ i + 1 ],
260+ ];
261+
262+ $ haveNamesOnlyColNames = array_keys ($ haveColumns );
263+ if (isset ($ haveColumns [$ columnName ])) {
264+ $ index = array_search ($ columnName , $ haveNamesOnlyColNames ) + 1 ;
265+ $ column ->fromPosition = [
266+ 'index ' => $ index ,
267+ 'after ' => $ haveNamesOnlyColNames [$ index - 2 ] ?? null ,
268+ 'before ' => $ haveNamesOnlyColNames [$ index ] ?? null ,
269+ ];
270+ }
271+
272+ $ i ++;
273+ }
274+
275+ $ takenIndices = [];
276+ foreach ($ this ->newColumns as $ columnName => $ column ) {
277+ /** @var \cebe\yii2openapi\db\ColumnSchema $column */
278+ if (!$ column ->fromPosition || !$ column ->toPosition ) {
279+ continue ;
280+ }
281+ if (
282+ is_int (array_search ([$ column ->toPosition ['index ' ], $ column ->fromPosition ['index ' ]], $ takenIndices ))
283+ ) {
284+ continue ;
285+ }
286+ if ($ column ->fromPosition === $ column ->toPosition ) {
287+ continue ;
288+ }
289+
290+ if ($ column ->fromPosition ['after ' ] === $ column ->toPosition ['after ' ]) {
291+ continue ;
292+ }
293+
294+ if ($ column ->fromPosition ['before ' ] === $ column ->toPosition ['before ' ]) {
295+ continue ;
296+ }
297+ // if (!in_array($column->fromPosition['after'], $onlyColumnNames)) {
298+ // continue;
299+ // }
300+ // if (!in_array($column->fromPosition['before'], $onlyColumnNames)) {
301+ // continue;
302+ // }
303+
304+ if (!in_array ($ column ->fromPosition ['after ' ], $ onlyColumnNames ) && !in_array ($ column ->fromPosition ['before ' ], $ onlyColumnNames )) {
305+ continue ;
306+ }
307+
308+
309+ $ column ->isPositionReallyChanged = true ;
310+ $ takenIndices [] = [$ column ->fromPosition ['index ' ], $ column ->toPosition ['index ' ]];
311+ }
312+ }
248313}
0 commit comments