Skip to content

Commit edbd140

Browse files
committed
Add more tests + refactor setColumnsPositions() to remove unwanted migrations
1 parent 815907e commit edbd140

File tree

3 files changed

+217
-7
lines changed

3 files changed

+217
-7
lines changed

src/lib/migrations/MigrationRecordBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ public function alterColumnTypeFromDb(
158158
ColumnSchema $column,
159159
bool $addUsing = false,
160160
?string $position = null
161-
) :string
162-
{
161+
) :string {
163162
if (property_exists($column, 'xDbType') && is_string($column->xDbType) && !empty($column->xDbType)) {
164163
$converter = $this->columnToCode($tableAlias, $column, true, false, true, true, $position);
165164
return sprintf(

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public function setColumnsPositions()
256256
return;
257257
}
258258

259-
$takenIndices = [];
259+
$takenIndices = $redundantIndices = []; # $redundantIndices are the unwanted ones which are created by moving of one or more columns. Example: if a column is moved from 2nd to 8th position then we will consider only one column is moved ignoring index/position change(-1) of 4rd to 8th column (4->3, 5->4 ...). So migration for this unwanted indices changes won't be generated
260260
foreach ($this->newColumns as $column) {
261261
/** @var \cebe\yii2openapi\db\ColumnSchema $column */
262262

@@ -275,6 +275,33 @@ public function setColumnsPositions()
275275

276276
$column->isPositionChanged = true;
277277
$takenIndices[] = [$column->fromPosition['index'], $column->toPosition['index']];
278+
279+
// -------
280+
if (($column->fromPosition['before'] !== $column->toPosition['before']) &&
281+
($column->fromPosition['after'] !== $column->toPosition['after'])
282+
) {
283+
$redundantIndices[] = [$column->fromPosition['index'], $column->toPosition['index']];
284+
}
285+
}
286+
287+
foreach ($this->newColumns as $column) {
288+
/** @var \cebe\yii2openapi\db\ColumnSchema $column */
289+
290+
if (!isset($column->toPosition['index'], $column->fromPosition['index'])) {
291+
continue;
292+
}
293+
$condition = (abs($column->toPosition['index'] - $column->fromPosition['index']) === count($redundantIndices));
294+
if (($column->fromPosition['before'] === $column->toPosition['before'])
295+
&& $condition
296+
) {
297+
$column->isPositionChanged = false;
298+
continue;
299+
}
300+
if (($column->fromPosition['after'] === $column->toPosition['after'])
301+
&& $condition
302+
) {
303+
$column->isPositionChanged = false;
304+
}
278305
}
279306
}
280307
}

tests/unit/IssueFixTest.php

Lines changed: 188 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,14 +1273,10 @@ public function up()
12731273
$this->addColumn('{{%fruits}}', 'col_6', $this->boolean()->null()->defaultValue(null));
12741274
$this->dropColumn('{{%fruits}}', 'size');
12751275
$this->alterColumn('{{%fruits}}', 'colour', $this->tinyInteger(1)->null()->defaultValue(null)->after('id'));
1276-
$this->alterColumn('{{%fruits}}', 'name', $this->tinyInteger(1)->null()->defaultValue(null)->after('colour'));
1277-
$this->alterColumn('{{%fruits}}', 'description', $this->tinyInteger(1)->null()->defaultValue(null)->after('name'));
12781276
}
12791277
12801278
public function down()
12811279
{
1282-
$this->alterColumn('{{%fruits}}', 'description', $this->tinyInteger(1)->null()->defaultValue(null)->after('name'));
1283-
$this->alterColumn('{{%fruits}}', 'name', $this->tinyInteger(1)->null()->defaultValue(null)->after('id'));
12841280
$this->alterColumn('{{%fruits}}', 'colour', $this->tinyInteger(1)->null()->defaultValue(null)->after('description'));
12851281
$this->addColumn('{{%fruits}}', 'size', $this->tinyInteger(1)->null()->defaultValue(null));
12861282
$this->dropColumn('{{%fruits}}', 'col_6');
@@ -1493,4 +1489,192 @@ public function down()
14931489
//
14941490
// $this->for58($schema, $expected, $columns);
14951491
// }
1492+
1493+
public function test58MoveAColAndChangeItsDataType()
1494+
{
1495+
$columns = [
1496+
'id' => 'pk',
1497+
'name' => 'bool null',
1498+
'description' => 'bool null',
1499+
'colour' => 'bool null',
1500+
'size' => 'bool null',
1501+
];
1502+
1503+
$schema = <<<YAML
1504+
openapi: 3.0.3
1505+
info:
1506+
title: 'test58MoveColumns'
1507+
version: 1.0.0
1508+
components:
1509+
schemas:
1510+
Fruit:
1511+
type: object
1512+
properties:
1513+
id:
1514+
type: integer
1515+
description:
1516+
type: boolean
1517+
colour:
1518+
type: integer
1519+
name:
1520+
type: boolean
1521+
size:
1522+
type: boolean
1523+
paths:
1524+
'/':
1525+
get:
1526+
responses:
1527+
'200':
1528+
description: OK
1529+
YAML;
1530+
1531+
$expected = <<<'PHP'
1532+
<?php
1533+
1534+
/**
1535+
* Table for Fruit
1536+
*/
1537+
class m200000_000000_change_table_fruits extends \yii\db\Migration
1538+
{
1539+
public function up()
1540+
{
1541+
$this->alterColumn('{{%fruits}}', 'colour', $this->integer()->null()->defaultValue(null));
1542+
$this->alterColumn('{{%fruits}}', 'name', $this->tinyInteger(1)->null()->defaultValue(null)->after('colour'));
1543+
}
1544+
1545+
public function down()
1546+
{
1547+
$this->alterColumn('{{%fruits}}', 'name', $this->tinyInteger(1)->null()->defaultValue(null)->after('id'));
1548+
$this->alterColumn('{{%fruits}}', 'colour', $this->tinyInteger(1)->null()->defaultValue(null));
1549+
}
1550+
}
1551+
1552+
PHP;
1553+
1554+
$this->for58($schema, $expected, $columns);
1555+
}
1556+
1557+
public function test58MoveAColDownwards()
1558+
{
1559+
$columns = [
1560+
'id' => 'pk',
1561+
'name' => 'bool null',
1562+
'description' => 'bool null',
1563+
'colour' => 'bool null',
1564+
'size' => 'bool null',
1565+
];
1566+
1567+
$schema = <<<YAML
1568+
openapi: 3.0.3
1569+
info:
1570+
title: 'test58MoveColumns'
1571+
version: 1.0.0
1572+
components:
1573+
schemas:
1574+
Fruit:
1575+
type: object
1576+
properties:
1577+
id:
1578+
type: integer
1579+
description:
1580+
type: boolean
1581+
colour:
1582+
type: boolean
1583+
name:
1584+
type: boolean
1585+
size:
1586+
type: boolean
1587+
paths:
1588+
'/':
1589+
get:
1590+
responses:
1591+
'200':
1592+
description: OK
1593+
YAML;
1594+
1595+
$expected = <<<'PHP'
1596+
<?php
1597+
1598+
/**
1599+
* Table for Fruit
1600+
*/
1601+
class m200000_000000_change_table_fruits extends \yii\db\Migration
1602+
{
1603+
public function up()
1604+
{
1605+
$this->alterColumn('{{%fruits}}', 'name', $this->tinyInteger(1)->null()->defaultValue(null)->after('colour'));
1606+
}
1607+
1608+
public function down()
1609+
{
1610+
$this->alterColumn('{{%fruits}}', 'name', $this->tinyInteger(1)->null()->defaultValue(null)->after('id'));
1611+
}
1612+
}
1613+
1614+
PHP;
1615+
1616+
$this->for58($schema, $expected, $columns);
1617+
}
1618+
1619+
public function test58MoveAColUpwards()
1620+
{
1621+
$columns = [
1622+
'id' => 'pk',
1623+
'name' => 'bool null',
1624+
'description' => 'bool null',
1625+
'colour' => 'bool null',
1626+
'size' => 'bool null',
1627+
];
1628+
1629+
$schema = <<<YAML
1630+
openapi: 3.0.3
1631+
info:
1632+
title: 'test58MoveColumns'
1633+
version: 1.0.0
1634+
components:
1635+
schemas:
1636+
Fruit:
1637+
type: object
1638+
properties:
1639+
id:
1640+
type: integer
1641+
colour:
1642+
type: boolean
1643+
name:
1644+
type: boolean
1645+
description:
1646+
type: boolean
1647+
size:
1648+
type: boolean
1649+
paths:
1650+
'/':
1651+
get:
1652+
responses:
1653+
'200':
1654+
description: OK
1655+
YAML;
1656+
1657+
$expected = <<<'PHP'
1658+
<?php
1659+
1660+
/**
1661+
* Table for Fruit
1662+
*/
1663+
class m200000_000000_change_table_fruits extends \yii\db\Migration
1664+
{
1665+
public function up()
1666+
{
1667+
$this->alterColumn('{{%fruits}}', 'colour', $this->tinyInteger(1)->null()->defaultValue(null)->after('id'));
1668+
}
1669+
1670+
public function down()
1671+
{
1672+
$this->alterColumn('{{%fruits}}', 'colour', $this->tinyInteger(1)->null()->defaultValue(null)->after('description'));
1673+
}
1674+
}
1675+
1676+
PHP;
1677+
1678+
$this->for58($schema, $expected, $columns);
1679+
}
14961680
}

0 commit comments

Comments
 (0)