Skip to content

Commit e54defe

Browse files
committed
Fix bug
1 parent 84c9655 commit e54defe

File tree

2 files changed

+146
-8
lines changed

2 files changed

+146
-8
lines changed

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $
258258
return null;
259259
}
260260
}
261+
if ($forAlter && $forDrop) {
262+
if (!array_key_exists($prevColName, $this->newColumns)) {
263+
return null;
264+
}
265+
}
261266
return self::POS_AFTER . ' ' . $prevColName;
262267
}
263268
return null;

tests/unit/IssueFixTest.php

Lines changed: 141 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,6 @@ private function for58($schema, $expected, $columns = [
430430
$actual = file_get_contents(Yii::getAlias('@app') . '/migrations_' . $dbStr . '_db/m200000_000000_change_table_fruits.php');
431431
$this->assertSame($expected, $actual);
432432
$this->runActualMigrations($dbStr, 1);
433-
434433
$deleteTable();
435434
}
436435
FileHelper::unlink($tmpConfigFile);
@@ -769,7 +768,7 @@ public function down()
769768

770769
PHP;
771770

772-
$this->for58($schema, $expected, $columns/*, ['Mysql']*/);
771+
$this->for58($schema, $expected, $columns);
773772
}
774773

775774
public function test58DeleteFirst4Col()
@@ -832,7 +831,7 @@ public function down()
832831

833832
PHP;
834833

835-
$this->for58($schema, $expected, $columns/*, ['Mysql']*/);
834+
$this->for58($schema, $expected, $columns);
836835
}
837836

838837
// ------------ Add
@@ -1219,7 +1218,7 @@ public function down()
12191218

12201219
PHP;
12211220

1222-
$this->for58($schema, $expected, $columns/*, ['Mysql']*/);
1221+
$this->for58($schema, $expected, $columns);
12231222
}
12241223

12251224
// ----------- Miscellaneous
@@ -1290,7 +1289,7 @@ public function down()
12901289

12911290
PHP;
12921291

1293-
$this->for58($schema, $expected, $columns/*, ['Mysql']*/);
1292+
$this->for58($schema, $expected, $columns);
12941293
}
12951294

12961295
public function test58Add1Del1ColAtSamePosition()
@@ -1357,7 +1356,141 @@ public function down()
13571356
$this->for58($schema, $expected, $columns);
13581357
}
13591358

1360-
// TODO add tests cases:
1361-
// add 1 and del 1 col at different position
1362-
// add 2 and del 1 col at different positions
1359+
public function test58Add3Del2ColAtDiffPos()
1360+
{
1361+
$columns = [
1362+
'id' => 'pk',
1363+
'name' => 'bool null',
1364+
'description' => 'bool null',
1365+
'colour' => 'bool null',
1366+
'size' => 'bool null',
1367+
];
1368+
1369+
$schema = <<<YAML
1370+
openapi: 3.0.3
1371+
info:
1372+
title: 'test58MoveColumns'
1373+
version: 1.0.0
1374+
components:
1375+
schemas:
1376+
Fruit:
1377+
type: object
1378+
properties:
1379+
id:
1380+
type: integer
1381+
col_6:
1382+
type: boolean
1383+
name:
1384+
type: boolean
1385+
col_7:
1386+
type: boolean
1387+
col_8:
1388+
type: boolean
1389+
size:
1390+
type: boolean
1391+
paths:
1392+
'/':
1393+
get:
1394+
responses:
1395+
'200':
1396+
description: OK
1397+
YAML;
1398+
1399+
$expected = <<<'PHP'
1400+
<?php
1401+
1402+
/**
1403+
* Table for Fruit
1404+
*/
1405+
class m200000_000000_change_table_fruits extends \yii\db\Migration
1406+
{
1407+
public function up()
1408+
{
1409+
$this->addColumn('{{%fruits}}', 'col_6', $this->boolean()->null()->defaultValue(null)->after('id'));
1410+
$this->addColumn('{{%fruits}}', 'col_7', $this->boolean()->null()->defaultValue(null)->after('name'));
1411+
$this->addColumn('{{%fruits}}', 'col_8', $this->boolean()->null()->defaultValue(null)->after('col_7'));
1412+
$this->dropColumn('{{%fruits}}', 'colour');
1413+
$this->dropColumn('{{%fruits}}', 'description');
1414+
}
1415+
1416+
public function down()
1417+
{
1418+
$this->addColumn('{{%fruits}}', 'description', $this->tinyInteger(1)->null()->defaultValue(null)->after('name'));
1419+
$this->addColumn('{{%fruits}}', 'colour', $this->tinyInteger(1)->null()->defaultValue(null)->after('description'));
1420+
$this->dropColumn('{{%fruits}}', 'col_8');
1421+
$this->dropColumn('{{%fruits}}', 'col_7');
1422+
$this->dropColumn('{{%fruits}}', 'col_6');
1423+
}
1424+
}
1425+
1426+
PHP;
1427+
1428+
$this->for58($schema, $expected, $columns);
1429+
}
1430+
1431+
// This test fails. See description of https://github.com/php-openapi/yii2-openapi/pull/59
1432+
// public function test58Add3Del2Move3ColAtDiffPos()
1433+
// {
1434+
// $columns = [
1435+
// 'id' => 'pk',
1436+
// 'name' => 'bool null',
1437+
// 'description' => 'bool null',
1438+
// 'colour' => 'bool null',
1439+
// 'size' => 'bool null',
1440+
// 'col_6' => 'bool null',
1441+
// ];
1442+
//
1443+
// $schema = <<<YAML
1444+
//openapi: 3.0.3
1445+
//info:
1446+
// title: 'test58MoveColumns'
1447+
// version: 1.0.0
1448+
//components:
1449+
// schemas:
1450+
// Fruit:
1451+
// type: object
1452+
// properties:
1453+
// id:
1454+
// type: integer
1455+
// size:
1456+
// type: boolean
1457+
// col_9:
1458+
// type: boolean
1459+
// name:
1460+
// type: boolean
1461+
// col_7:
1462+
// type: boolean
1463+
// description:
1464+
// type: boolean
1465+
// col_8:
1466+
// type: boolean
1467+
//paths:
1468+
// '/':
1469+
// get:
1470+
// responses:
1471+
// '200':
1472+
// description: OK
1473+
//YAML;
1474+
//
1475+
// $expected = <<<'PHP'
1476+
//<?php
1477+
//
1478+
///**
1479+
// * Table for Fruit
1480+
// */
1481+
//class m200000_000000_change_table_fruits extends \yii\db\Migration
1482+
//{
1483+
// public function up()
1484+
// {
1485+
// }
1486+
//
1487+
// public function down()
1488+
// {
1489+
// }
1490+
//}
1491+
//
1492+
//PHP;
1493+
//
1494+
// $this->for58($schema, $expected, $columns);
1495+
// }
13631496
}

0 commit comments

Comments
 (0)