Skip to content

Commit 95db88f

Browse files
committed
Fix bugs + fix failing tests 2
1 parent 85aac91 commit 95db88f

File tree

8 files changed

+25
-23
lines changed

8 files changed

+25
-23
lines changed

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
2828
$positionDesired = $this->findPosition($desired, false, true);
2929
$positionCurrent = $this->findPosition($desired, true, true);
3030
$key = array_search('position', $changed, true);
31-
if ($key !== false) {
32-
unset($changed[$key]);
33-
}
31+
unset($changed[$key]);
3432
}
3533
$newColumn = clone $current;
3634
// $positionCurrent = $this->findPosition($desired, true);
@@ -253,8 +251,12 @@ public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $
253251
// }
254252

255253
if (array_key_exists($prevColName, $forDrop ? $this->tableSchema->columns : $this->newColumns)) {
256-
if (($prevColName === $columnNames[count($columnNames) - 1]) && !$forAlter) {
257-
return null;
254+
if ($forDrop && !$forAlter) {
255+
// if the previous column is the last one in the want names then no need for AFTER
256+
$cols = array_keys($this->newColumns);
257+
if ($prevColName === array_pop($cols)) {
258+
return null;
259+
}
258260
}
259261
return self::POS_AFTER . ' ' . $prevColName;
260262
}
@@ -328,15 +330,20 @@ public function setPositions()
328330
continue;
329331
}
330332

331-
// // check if only new columns are added without any explicit position change
333+
// check if only new columns are added without any explicit position change
332334
$namesForCreate = array_diff($wantNames, $haveNames);
333-
if ($namesForCreate && $haveNames === array_values(array_diff($wantNames, $namesForCreate))) {
335+
$wantNamesWoNewCols = array_values(array_diff($wantNames, $namesForCreate));
336+
if ($namesForCreate && $haveNames === $wantNamesWoNewCols) {
334337
continue;
335338
}
336-
337339
// check if only existing columns are deleted without any explicit position change
338340
$namesForDrop = array_diff($haveNames, $wantNames);
339-
if ($namesForDrop && $wantNames === array_values(array_diff($haveNames, $namesForDrop))) {
341+
$haveNamesWoDropCols = array_values(array_diff($haveNames, $namesForDrop));
342+
if ($namesForDrop && $wantNames === $haveNamesWoDropCols) {
343+
continue;
344+
}
345+
// check both above simultaneously
346+
if ($namesForCreate && $namesForDrop && ($wantNamesWoNewCols === $haveNamesWoDropCols)) {
340347
continue;
341348
}
342349

tests/specs/change_column_name/maria/app/migrations_maria_db/m200000_000000_change_table_column_name_changes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function up()
1313

1414
public function down()
1515
{
16-
$this->addColumn('{{%column_name_changes}}', 'updated_at', $this->datetime()->notNull()->after('name'));
16+
$this->addColumn('{{%column_name_changes}}', 'updated_at', $this->datetime()->notNull());
1717
$this->dropColumn('{{%column_name_changes}}', 'updated_at_2');
1818
}
1919
}

tests/specs/change_column_name/mysql/app/migrations_mysql_db/m200000_000000_change_table_column_name_changes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function up()
1313

1414
public function down()
1515
{
16-
$this->addColumn('{{%column_name_changes}}', 'updated_at', $this->datetime()->notNull()->after('name'));
16+
$this->addColumn('{{%column_name_changes}}', 'updated_at', $this->datetime()->notNull());
1717
$this->dropColumn('{{%column_name_changes}}', 'updated_at_2');
1818
}
1919
}

tests/specs/enum/new_column/maria/app/migrations_maria_db/m200000_000001_change_table_newcolumns.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function up()
1414

1515
public function down()
1616
{
17-
$this->addColumn('{{%newcolumns}}', 'delete_col', 'enum("FOUR", "FIVE", "SIX") NULL DEFAULT NULL AFTER id');
17+
$this->addColumn('{{%newcolumns}}', 'delete_col', 'enum("FOUR", "FIVE", "SIX") NULL DEFAULT NULL');
1818
$this->dropColumn('{{%newcolumns}}', 'new_column_x');
1919
$this->dropColumn('{{%newcolumns}}', 'new_column');
2020
}

tests/specs/enum/new_column/mysql/app/migrations_mysql_db/m200000_000001_change_table_newcolumns.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function up()
1414

1515
public function down()
1616
{
17-
$this->addColumn('{{%newcolumns}}', 'delete_col', 'enum("FOUR", "FIVE", "SIX") NULL DEFAULT NULL AFTER id');
17+
$this->addColumn('{{%newcolumns}}', 'delete_col', 'enum("FOUR", "FIVE", "SIX") NULL DEFAULT NULL');
1818
$this->dropColumn('{{%newcolumns}}', 'new_column_x');
1919
$this->dropColumn('{{%newcolumns}}', 'new_column');
2020
}

tests/specs/new_column_position/maria/app/migrations_maria_db/m200000_000003_change_table_dropfirsttwocols.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public function up()
1414
public function down()
1515
{
1616
$this->addColumn('{{%dropfirsttwocols}}', 'name', $this->text()->null()->defaultValue(null)->first());
17-
$this->addColumn('{{%dropfirsttwocols}}', 'address', $this->text()->null()->defaultValue(null));
17+
$this->addColumn('{{%dropfirsttwocols}}', 'address', $this->text()->null()->defaultValue(null)->after('name'));
1818
}
1919
}

tests/specs/new_column_position/mysql/app/migrations_mysql_db/m200000_000003_change_table_dropfirsttwocols.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public function up()
1414
public function down()
1515
{
1616
$this->addColumn('{{%dropfirsttwocols}}', 'name', $this->text()->null()->first());
17-
$this->addColumn('{{%dropfirsttwocols}}', 'address', $this->text()->null());
17+
$this->addColumn('{{%dropfirsttwocols}}', 'address', $this->text()->null()->after('name'));
1818
}
1919
}

tests/unit/IssueFixTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public function up()
536536
537537
public function down()
538538
{
539-
$this->addColumn('{{%fruits}}', 'colour', $this->text()->notNull()->after('description'));
539+
$this->addColumn('{{%fruits}}', 'colour', $this->text()->notNull());
540540
$this->addColumn('{{%fruits}}', 'size', $this->text()->notNull());
541541
}
542542
}
@@ -761,8 +761,8 @@ public function up()
761761
public function down()
762762
{
763763
$this->addColumn('{{%fruits}}', 'description', $this->text()->null());
764-
$this->addColumn('{{%fruits}}', 'colour', $this->text()->null());
765-
$this->addColumn('{{%fruits}}', 'size', $this->text()->null());
764+
$this->addColumn('{{%fruits}}', 'colour', $this->text()->null()->after('description'));
765+
$this->addColumn('{{%fruits}}', 'size', $this->text()->null()->after('colour'));
766766
$this->addColumn('{{%fruits}}', 'col_6', $this->text()->null());
767767
}
768768
}
@@ -1103,11 +1103,6 @@ public function test58MoveLast2Col2PosUp()
11031103
'description' => 'text null',
11041104
'colour' => 'text null',
11051105
'size' => 'text null',
1106-
// 'col_6' => 'text null',
1107-
// 'col_7' => 'text null',
1108-
// 'col_8' => 'text null',
1109-
// 'col_9' => 'text null',
1110-
11111106
];
11121107

11131108
$schema = <<<YAML

0 commit comments

Comments
 (0)