Skip to content

Commit

Permalink
Fix bug, add test, fix failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
SOHELAHMED7 committed Dec 10, 2024
1 parent dd46d78 commit 36c576e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/lib/migrations/BaseMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function (string $unknownColumn) {

$columnsForChange = array_intersect($wantNames, $haveNames);

$columnsForRename = $this->handleColumnsRename($columnsForCreate, $columnsForDrop, $this->newColumns);
$this->handleColumnsRename($columnsForCreate, $columnsForDrop, $this->newColumns);

if ($this->model->drop) {
$this->newColumns = [];
Expand Down Expand Up @@ -625,6 +625,9 @@ public function handleColumnsRename(array &$columnsForCreate, array &$columnsFor
{
$keys = [];
$existingColumns = $this->tableSchema->columns;
if (count($existingColumns) !== count($newColumns)) {
return;
}
$existingColumnNames = array_keys($existingColumns);
$newColumnNames = array_flip(array_keys($newColumns));
foreach ($columnsForCreate as $key => $column) {
Expand All @@ -633,7 +636,7 @@ public function handleColumnsRename(array &$columnsForCreate, array &$columnsFor
if ($previousColumnName) {
$current = $existingColumns[$previousColumnName];
$desired = $newColumns[$column->name];
$changedAttributes = $this->compareColumns($current, $desired);
$changedAttributes = $this->compareColumns(clone $current, clone $desired);
if (empty($changedAttributes)) {
$keys[] = $key;
$dropKeyOut = null;
Expand All @@ -645,6 +648,8 @@ public function handleColumnsRename(array &$columnsForCreate, array &$columnsFor
// existing column name should be removed from $columnsForDrop
unset($columnsForDrop[$dropKeyOut]);

// TODO check in `required` and `x-index`

// Create ALTER COLUMN NAME query
$this->migration->addUpCode($this->recordBuilder->renameColumn($this->model->tableAlias, $previousColumnName, $column->name))
->addDownCode($this->recordBuilder->renameColumn($this->model->tableAlias, $column->name, $previousColumnName));
Expand All @@ -654,7 +659,7 @@ public function handleColumnsRename(array &$columnsForCreate, array &$columnsFor

// new column name should be removed from $columnsForCreate
foreach ($keys as $key) {
unset($columnsForCreate[$key], $columnsForDrop[$previousColumnName]);
unset($columnsForCreate[$key]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Table for Fruit
*/
class m200000_000000_change_table_fruits extends \yii\db\Migration
{
public function up()
{
$this->renameColumn('{{%fruits}}', 'name', 'name_2');
$this->renameColumn('{{%fruits}}', 'description', 'description_2');
}

public function down()
{
$this->renameColumn('{{%fruits}}', 'description_2', 'description');
$this->renameColumn('{{%fruits}}', 'name_2', 'name');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Table for Fruit
*/
class m200000_000000_change_table_fruits extends \yii\db\Migration
{
public function safeUp()
{
$this->renameColumn('{{%fruits}}', 'name', 'name_2');
$this->renameColumn('{{%fruits}}', 'description', 'description_2');
}

public function safeDown()
{
$this->renameColumn('{{%fruits}}', 'description_2', 'description');
$this->renameColumn('{{%fruits}}', 'name_2', 'name');
}
}
40 changes: 31 additions & 9 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,9 @@ public function test25GenerateInverseRelations()
// https://github.com/php-openapi/yii2-openapi/issues/63
public function test63JustColumnNameRename()
{
$testFile = Yii::getAlias("@specs/issue_fix/63_just_column_name_rename/index.php");

// MySQL
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
'id' => 'pk',
Expand All @@ -897,17 +900,36 @@ public function test63JustColumnNameRename()
'colour' => 'text',
])->execute();

$testFile = Yii::getAlias("@specs/issue_fix/63_just_column_name_rename/index.php");
$this->runGenerator($testFile);
// $this->runActualMigrations('mysql', 1);
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
// 'recursive' => true,
// ]);
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/63_just_column_name_rename/mysql"), [
// 'recursive' => true,
// ]);
// $this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('mysql', 1);
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
'recursive' => true,
]);
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/63_just_column_name_rename/mysql"), [
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();

// PgSQL
$this->changeDbToPgsql();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
'id' => 'pk',
'name' => 'text',
'description' => 'text',
'colour' => 'text',
])->execute();
$this->runGenerator($testFile, 'pgsql');
$this->runActualMigrations('pgsql', 1);
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
'recursive' => true,
'except' => ['migrations_mysql_db']
]);
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/63_just_column_name_rename/pgsql"), [
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
}
}

0 comments on commit 36c576e

Please sign in to comment.