Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SOHELAHMED7 committed Oct 14, 2024
1 parent 84c9655 commit e54defe
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/lib/migrations/MysqlMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $
return null;
}
}
if ($forAlter && $forDrop) {
if (!array_key_exists($prevColName, $this->newColumns)) {
return null;
}
}
return self::POS_AFTER . ' ' . $prevColName;
}
return null;
Expand Down
149 changes: 141 additions & 8 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ private function for58($schema, $expected, $columns = [
$actual = file_get_contents(Yii::getAlias('@app') . '/migrations_' . $dbStr . '_db/m200000_000000_change_table_fruits.php');
$this->assertSame($expected, $actual);
$this->runActualMigrations($dbStr, 1);

$deleteTable();
}
FileHelper::unlink($tmpConfigFile);
Expand Down Expand Up @@ -769,7 +768,7 @@ public function down()

PHP;

$this->for58($schema, $expected, $columns/*, ['Mysql']*/);
$this->for58($schema, $expected, $columns);
}

public function test58DeleteFirst4Col()
Expand Down Expand Up @@ -832,7 +831,7 @@ public function down()

PHP;

$this->for58($schema, $expected, $columns/*, ['Mysql']*/);
$this->for58($schema, $expected, $columns);
}

// ------------ Add
Expand Down Expand Up @@ -1219,7 +1218,7 @@ public function down()

PHP;

$this->for58($schema, $expected, $columns/*, ['Mysql']*/);
$this->for58($schema, $expected, $columns);
}

// ----------- Miscellaneous
Expand Down Expand Up @@ -1290,7 +1289,7 @@ public function down()

PHP;

$this->for58($schema, $expected, $columns/*, ['Mysql']*/);
$this->for58($schema, $expected, $columns);
}

public function test58Add1Del1ColAtSamePosition()
Expand Down Expand Up @@ -1357,7 +1356,141 @@ public function down()
$this->for58($schema, $expected, $columns);
}

// TODO add tests cases:
// add 1 and del 1 col at different position
// add 2 and del 1 col at different positions
public function test58Add3Del2ColAtDiffPos()
{
$columns = [
'id' => 'pk',
'name' => 'bool null',
'description' => 'bool null',
'colour' => 'bool null',
'size' => 'bool null',
];

$schema = <<<YAML
openapi: 3.0.3
info:
title: 'test58MoveColumns'
version: 1.0.0
components:
schemas:
Fruit:
type: object
properties:
id:
type: integer
col_6:
type: boolean
name:
type: boolean
col_7:
type: boolean
col_8:
type: boolean
size:
type: boolean
paths:
'/':
get:
responses:
'200':
description: OK
YAML;

$expected = <<<'PHP'
<?php
/**
* Table for Fruit
*/
class m200000_000000_change_table_fruits extends \yii\db\Migration
{
public function up()
{
$this->addColumn('{{%fruits}}', 'col_6', $this->boolean()->null()->defaultValue(null)->after('id'));
$this->addColumn('{{%fruits}}', 'col_7', $this->boolean()->null()->defaultValue(null)->after('name'));
$this->addColumn('{{%fruits}}', 'col_8', $this->boolean()->null()->defaultValue(null)->after('col_7'));
$this->dropColumn('{{%fruits}}', 'colour');
$this->dropColumn('{{%fruits}}', 'description');
}
public function down()
{
$this->addColumn('{{%fruits}}', 'description', $this->tinyInteger(1)->null()->defaultValue(null)->after('name'));
$this->addColumn('{{%fruits}}', 'colour', $this->tinyInteger(1)->null()->defaultValue(null)->after('description'));
$this->dropColumn('{{%fruits}}', 'col_8');
$this->dropColumn('{{%fruits}}', 'col_7');
$this->dropColumn('{{%fruits}}', 'col_6');
}
}

PHP;

$this->for58($schema, $expected, $columns);
}

// This test fails. See description of https://github.com/php-openapi/yii2-openapi/pull/59
// public function test58Add3Del2Move3ColAtDiffPos()
// {
// $columns = [
// 'id' => 'pk',
// 'name' => 'bool null',
// 'description' => 'bool null',
// 'colour' => 'bool null',
// 'size' => 'bool null',
// 'col_6' => 'bool null',
// ];
//
// $schema = <<<YAML
//openapi: 3.0.3
//info:
// title: 'test58MoveColumns'
// version: 1.0.0
//components:
// schemas:
// Fruit:
// type: object
// properties:
// id:
// type: integer
// size:
// type: boolean
// col_9:
// type: boolean
// name:
// type: boolean
// col_7:
// type: boolean
// description:
// type: boolean
// col_8:
// type: boolean
//paths:
// '/':
// get:
// responses:
// '200':
// description: OK
//YAML;
//
// $expected = <<<'PHP'
//<?php
//
///**
// * Table for Fruit
// */
//class m200000_000000_change_table_fruits extends \yii\db\Migration
//{
// public function up()
// {
// }
//
// public function down()
// {
// }
//}
//
//PHP;
//
// $this->for58($schema, $expected, $columns);
// }
}

0 comments on commit e54defe

Please sign in to comment.