Skip to content

Commit ac7a7a4

Browse files
committed
Implement - WIP
1 parent 5b785c4 commit ac7a7a4

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@ function (string $unknownColumn) {
199199

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

202+
$columnsForRename = $this->findColumnsToRename($columnsForCreate, $columnsForDrop, $this->newColumns);
203+
202204
if ($this->model->drop) {
203205
$this->newColumns = [];
204-
$wantNames = [];
205206
$columnsForCreate = [];
206207
$columnsForChange = [];
207208
$columnsForDrop = [];
@@ -614,4 +615,26 @@ protected function shouldCompareComment(ColumnSchema $desired): bool
614615
}
615616
return $comment;
616617
}
618+
619+
/**
620+
* @param array $columnsForCreate
621+
* @param array $columnsForDrop
622+
* @param $newColumns
623+
* @return string[]
624+
*/
625+
public function findColumnsToRename(array &$columnsForCreate, array &$columnsForDrop, $newColumns): array
626+
{
627+
$columnNames = [];
628+
$existingColumns = $this->tableSchema->columns;
629+
$existingColumnNames = array_flip(array_keys($existingColumns));
630+
$newColumnNames = array_flip(array_keys($newColumns));
631+
632+
foreach ($columnsForCreate as $name) {
633+
if ($existingColumnNames[$name] === $newColumnNames[$name]) {
634+
// TODO compare column
635+
}
636+
}
637+
638+
return $columnNames;
639+
}
617640
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/63_just_column_name_rename/index.yml',
5+
'generateUrls' => false,
6+
'generateModels' => false,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => true,
12+
'generateModelFaker' => false, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
13+
];
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
openapi: 3.0.3
2+
x-description-is-comment: true
3+
info:
4+
title: 'Description of a property in spec must correspond to DB TABLE COLUMN COMMENT #60'
5+
version: 1.0.0
6+
7+
components:
8+
schemas:
9+
Fruit:
10+
type: object
11+
properties:
12+
id:
13+
type: integer
14+
name:
15+
type: string
16+
description: desc with ' quote
17+
description:
18+
type: number
19+
x-db-type: double precision
20+
description: desc ' 2
21+
Animal:
22+
type: object
23+
properties:
24+
id:
25+
type: integer
26+
name:
27+
type: integer
28+
g:
29+
type: string
30+
description: desc for g
31+
g2:
32+
type: string
33+
description: changed comment on g2 col
34+
g3:
35+
type: string
36+
description: the comment on g3 col remains same
37+
g4:
38+
type: integer
39+
description: data type changes but comment remains same
40+
new_col:
41+
type: string
42+
description: new col added
43+
44+
paths:
45+
'/':
46+
get:
47+
responses:
48+
'200':
49+
description: OK

tests/unit/IssueFixTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,4 +885,24 @@ public function test25GenerateInverseRelations()
885885
]);
886886
$this->checkFiles($actualFiles, $expectedFiles);
887887
}
888+
889+
// https://github.com/php-openapi/yii2-openapi/issues/63
890+
public function test63JustColumnNameRename()
891+
{
892+
$this->assertTrue(4);
893+
894+
return;
895+
896+
897+
$testFile = Yii::getAlias("@specs/issue_fix/63_just_column_name_rename/index.php");
898+
$this->runGenerator($testFile);
899+
$this->runActualMigrations('mysql', 1);
900+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
901+
'recursive' => true,
902+
]);
903+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/63_just_column_name_rename/mysql"), [
904+
'recursive' => true,
905+
]);
906+
$this->checkFiles($actualFiles, $expectedFiles);
907+
}
888908
}

0 commit comments

Comments
 (0)