Skip to content

Commit 6fa477f

Browse files
committed
Fix bugs + fix failing tests + add more tests
1 parent b58bf2e commit 6fa477f

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

src/lib/migrations/MigrationRecordBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ public function alterColumnType(
135135
ColumnSchema $column,
136136
bool $addUsing = false,
137137
?string $position = null
138-
):string
139-
{
138+
):string {
140139
if (property_exists($column, 'xDbType') && is_string($column->xDbType) && !empty($column->xDbType)) {
141140
$converter = $this->columnToCode($tableAlias, $column, false, false, true, true, $position);
142141
return sprintf(

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ public function setPositions()
278278
if (!$column->fromPosition || !$column->toPosition) {
279279
continue;
280280
}
281+
281282
if (
282283
is_int(array_search([$column->toPosition['index'], $column->fromPosition['index']], $takenIndices))
283284
) {
@@ -287,19 +288,69 @@ public function setPositions()
287288
continue;
288289
}
289290

290-
if ($column->fromPosition['after'] === $column->toPosition['after']) {
291+
if (!in_array($column->fromPosition['after'], $onlyColumnNames) && !in_array($column->fromPosition['before'], $onlyColumnNames)) { // deleted column
291292
continue;
292293
}
293294

294-
if ($column->fromPosition['before'] === $column->toPosition['before']) {
295+
if ($column->fromPosition['index'] === $column->toPosition['index']) {
295296
continue;
296297
}
297298

298-
if (!in_array($column->fromPosition['after'], $onlyColumnNames) && !in_array($column->fromPosition['before'], $onlyColumnNames)) {
299+
if ($this->checkAfterPosition($column)) {
300+
continue;
301+
}
302+
if ($this->checkBeforePosition($column)) {
299303
continue;
300304
}
305+
306+
// if ($column->fromPosition['before'] === $column->toPosition['before']
307+
//// && $column->fromPosition['index'] !== $column->toPosition['index']
308+
// ) {
309+
// continue;
310+
// }
311+
301312
$column->isPositionReallyChanged = true;
302313
$takenIndices[] = [$column->fromPosition['index'], $column->toPosition['index']];
303314
}
304315
}
316+
317+
public function checkAfterPosition($column)
318+
{
319+
if ($column->fromPosition['after'] === $column->toPosition['after']
320+
// && $column->fromPosition['index'] !== $column->toPosition['index']
321+
) {
322+
$afterColName = $column->toPosition['after'];
323+
$afterCol = $this->newColumns[$afterColName] ?? null;
324+
if ($afterCol) {
325+
if ($this->checkAfterPosition($afterCol)) {
326+
return true;
327+
} else {
328+
return false;
329+
}
330+
} else {
331+
return true;
332+
}
333+
}
334+
return false;
335+
}
336+
337+
public function checkBeforePosition($column)
338+
{
339+
if ($column->fromPosition['before'] === $column->toPosition['before']
340+
// && $column->fromPosition['index'] !== $column->toPosition['index']
341+
) {
342+
$beforeColName = $column->toPosition['before'];
343+
$beforeCol = $this->newColumns[$beforeColName] ?? null;
344+
if ($beforeCol) {
345+
if ($this->checkBeforePosition($beforeCol)) {
346+
return true;
347+
} else {
348+
return false;
349+
}
350+
} else {
351+
return true;
352+
}
353+
}
354+
return false;
355+
}
305356
}

tests/unit/IssueFixTest.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ private function for58($schema, $expected, $columns = [
400400
'description' => 'text not null',
401401
'colour' => 'text not null',
402402
'size' => 'text not null',
403-
])
403+
], $dbs = ['Mysql', 'Mariadb'])
404404
{
405405
$deleteTable = function () {
406406
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
@@ -420,7 +420,7 @@ private function for58($schema, $expected, $columns = [
420420
$tmpConfigFile = Yii::getAlias("@runtime") . "/tmp-config.php";
421421
file_put_contents($tmpConfigFile, '<?php return ' . var_export($config, true) . ';');
422422

423-
foreach (['Mysql', 'Mariadb'] as $db) {
423+
foreach ($dbs as $db) {
424424
$this->{"changeDbTo$db"}();
425425
$deleteTable();
426426
$createTable();
@@ -1040,10 +1040,10 @@ public function test58MoveColumns()
10401040
'description' => 'text null',
10411041
'colour' => 'text null',
10421042
'size' => 'text null',
1043-
'col_6' => 'text null',
1044-
'col_7' => 'text null',
1045-
'col_8' => 'text null',
1046-
'col_9' => 'text null',
1043+
// 'col_6' => 'text null',
1044+
// 'col_7' => 'text null',
1045+
// 'col_8' => 'text null',
1046+
// 'col_9' => 'text null',
10471047

10481048
];
10491049

@@ -1060,22 +1060,13 @@ public function test58MoveColumns()
10601060
id:
10611061
type: integer
10621062
colour:
1063-
type: string
1063+
type: string
10641064
size:
10651065
type: string
10661066
name:
10671067
type: string
10681068
description:
1069-
type: string
1070-
col_6:
1071-
type: string
1072-
col_7:
1073-
type: string
1074-
col_8:
1075-
type: string
1076-
col_9:
1077-
type: string
1078-
1069+
type: string
10791070
paths:
10801071
'/':
10811072
get:
@@ -1094,19 +1085,19 @@ class m200000_000000_change_table_fruits extends \yii\db\Migration
10941085
{
10951086
public function up()
10961087
{
1097-
$this->alterColumn('{{%fruits}}', 'colour', $this->text()->notNull()->after('id'));
1098-
$this->alterColumn('{{%fruits}}', 'size', $this->text()->notNull()->after('colour'));
1088+
$this->alterColumn('{{%fruits}}', 'colour', $this->text()->null()->after('id'));
1089+
$this->alterColumn('{{%fruits}}', 'size', $this->text()->null()->after('colour'));
10991090
}
11001091
11011092
public function down()
11021093
{
1103-
$this->alterColumn('{{%fruits}}', 'size', $this->text()->notNull()->after('colour'));
1104-
$this->alterColumn('{{%fruits}}', 'colour', $this->text()->notNull()->after('description'));
1094+
$this->alterColumn('{{%fruits}}', 'size', $this->text()->null()->after('colour'));
1095+
$this->alterColumn('{{%fruits}}', 'colour', $this->text()->null()->after('description'));
11051096
}
11061097
}
11071098

11081099
PHP;
11091100

1110-
$this->for58($schema, $expected, $columns);
1101+
$this->for58($schema, $expected, $columns, ['Mysql']);
11111102
}
11121103
}

0 commit comments

Comments
 (0)