Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
08e30ab
Test
SOHELAHMED7 Dec 21, 2022
e367a47
Merge branch '100-timestamp-migrations-do-not-work-in-mariadb' of git…
SOHELAHMED7 Dec 21, 2022
ec097d7
Test 2
SOHELAHMED7 Dec 21, 2022
03ca98b
test 3
SOHELAHMED7 Dec 21, 2022
7dee6b9
Merge pull request #1 from SOHELAHMED7/100-timestamp-migrations-do-no…
SOHELAHMED7 Dec 21, 2022
6c90780
Merge branch 'master' of https://github.com/SOHELAHMED7/yii2-openapi …
SOHELAHMED7 Dec 21, 2022
984c7b3
Test
SOHELAHMED7 Dec 21, 2022
96257f6
Cleanup
SOHELAHMED7 Dec 21, 2022
d3ecd74
test
SOHELAHMED7 Dec 21, 2022
c809394
Fix issue in setting default expression
SOHELAHMED7 Dec 21, 2022
3d4d71c
Fix issue: decimal considered as string instead of double/float
SOHELAHMED7 Dec 21, 2022
a9c6ec2
Fix enum issue for MySQL, MariaDB and partially for PgSQL #111
SOHELAHMED7 Dec 22, 2022
89f98c2
WIP
SOHELAHMED7 Dec 23, 2022
b047f7d
undo WIP
SOHELAHMED7 Dec 23, 2022
6cea7f2
Fix issue: CREATE/DROP enum in PGSQL up/down migration not in correct…
SOHELAHMED7 Dec 23, 2022
6a9a680
Fix style
SOHELAHMED7 Dec 23, 2022
c60403f
Fix quote issue in migration down code for MySQL and Maria + add more…
SOHELAHMED7 Dec 23, 2022
fcc53a2
Fix multiple issue + add more tests for eunm in Pgsql + other DB
SOHELAHMED7 Dec 23, 2022
130c69a
Fix bug in column change detection in Pgsql
SOHELAHMED7 Dec 24, 2022
1b0bc2e
Fix failing tests
SOHELAHMED7 Dec 24, 2022
a037827
Fix enum related issues + add more tests for it for all 3 DBs
SOHELAHMED7 Dec 24, 2022
dae8e4c
Implement Enum change value migration - WIP
SOHELAHMED7 Dec 24, 2022
e67e8e1
WIP
SOHELAHMED7 Dec 24, 2022
ded2d2b
Complete enum tests + add stub for enum values change test
SOHELAHMED7 Dec 27, 2022
af614eb
Remove unwanted files
SOHELAHMED7 Dec 27, 2022
39d2ff0
Add docs in README
SOHELAHMED7 Dec 27, 2022
5badea4
Restore tests that were deleted in https://github.com/cebe/yii2-opena…
SOHELAHMED7 Dec 27, 2022
72c2a79
Add enh.
SOHELAHMED7 Dec 27, 2022
a9e0312
Merge pull request #2 from SOHELAHMED7/timestamp-enum-x-db-type-bug-fix
SOHELAHMED7 Dec 28, 2022
c9322ad
test
SOHELAHMED7 Dec 28, 2022
a8d3b61
Undo test
SOHELAHMED7 Dec 28, 2022
c0563e7
Fix issue: defaultValue change detection for expression was not worki…
SOHELAHMED7 Dec 28, 2022
921f6ed
Merge pull request #3 from SOHELAHMED7/json-issue-fix
SOHELAHMED7 Dec 29, 2022
20e960a
test to trigger CI
SOHELAHMED7 Dec 29, 2022
4066e93
test to trigger CI 2
SOHELAHMED7 Dec 29, 2022
ca72322
test to trigger CI 3
SOHELAHMED7 Dec 29, 2022
672ae45
test to trigger CI 4
SOHELAHMED7 Dec 29, 2022
7da7317
Fix style
SOHELAHMED7 Dec 29, 2022
53d4de4
test to trigger CI 5
SOHELAHMED7 Dec 29, 2022
0041f7b
Fix style
SOHELAHMED7 Dec 29, 2022
a2d0c80
Fix https://github.com/cebe/yii2-openapi/issues/104
SOHELAHMED7 Dec 30, 2022
1f1e71f
remove unwanted files
SOHELAHMED7 Dec 30, 2022
86ae33d
Merge pull request #4 from SOHELAHMED7/104-remove-id-from-rules-metho…
SOHELAHMED7 Dec 30, 2022
8c074a9
Fix https://github.com/cebe/yii2-openapi/issues/115
SOHELAHMED7 Dec 30, 2022
512a11e
Merge pull request #5 from SOHELAHMED7/115-default-in-openapi-schema-…
SOHELAHMED7 Dec 30, 2022
91273e8
test
SOHELAHMED7 Dec 30, 2022
0ac648d
Add tests for this issue
SOHELAHMED7 Dec 30, 2022
d7e57a0
remove unwanted space from README
SOHELAHMED7 Dec 30, 2022
0eae0f4
Merge pull request #6 from SOHELAHMED7/114-errors-in-migrations-in-ca…
SOHELAHMED7 Dec 30, 2022
4aad200
Merge branch 'cebe:master' into master
SOHELAHMED7 Dec 30, 2022
220d1ee
Test
SOHELAHMED7 Dec 30, 2022
c73c16d
Fix https://github.com/cebe/yii2-openapi/issues/125
SOHELAHMED7 Dec 30, 2022
a6fc5b1
Run actual migrations
SOHELAHMED7 Dec 31, 2022
09a5fb8
Add a test for float issue in PgSQL
SOHELAHMED7 Dec 31, 2022
67ad00c
test
SOHELAHMED7 Dec 31, 2022
2e382e4
Undo test
SOHELAHMED7 Dec 31, 2022
a8d8788
WIP
SOHELAHMED7 Dec 31, 2022
433e7f0
Fix https://github.com/cebe/yii2-openapi/issues/126
SOHELAHMED7 Jan 2, 2023
ce56996
Merge pull request #8 from SOHELAHMED7/126-dont-use-second-db-pgtestd…
SOHELAHMED7 Jan 2, 2023
60dd357
Merge branches 'master' and '107-migrations-are-generated-with-syntax…
SOHELAHMED7 Jan 2, 2023
d0b83eb
Remove unwanted line
SOHELAHMED7 Jan 2, 2023
9e341e9
Initial commit of this branch
SOHELAHMED7 Jan 2, 2023
4da1db2
Fix https://github.com/cebe/yii2-openapi/issues/127
SOHELAHMED7 Jan 2, 2023
2f58a9e
Fix quote issue in enum in PgSQL
SOHELAHMED7 Jan 2, 2023
e8f1df1
Fix quote issue in enum in PgSQL 2
SOHELAHMED7 Jan 2, 2023
eaf2464
Merge pull request #9 from SOHELAHMED7/127-quote-issue-to-preserve-th…
SOHELAHMED7 Jan 3, 2023
a37e2ca
Merge branches 'master' and '107-migrations-are-generated-with-syntax…
SOHELAHMED7 Jan 3, 2023
a6c20ef
Fix failing tests
SOHELAHMED7 Jan 3, 2023
3fff89a
Fix https://github.com/cebe/yii2-openapi/issues/107
SOHELAHMED7 Jan 3, 2023
1cdb53b
Merge pull request #7 from SOHELAHMED7/107-migrations-are-generated-w…
SOHELAHMED7 Jan 3, 2023
8aa5697
Initial commit of this branch to create PR
SOHELAHMED7 Jan 3, 2023
dcbde27
Add tests
SOHELAHMED7 Jan 3, 2023
123e67a
Implement quote in enum type names
SOHELAHMED7 Jan 3, 2023
d4527f5
Implement enum with table name
SOHELAHMED7 Jan 3, 2023
d4f6f2d
Adjust tests for enum with table name
SOHELAHMED7 Jan 4, 2023
e2fc2e9
Merge pull request #10 from SOHELAHMED7/128-change-enum-name-generati…
SOHELAHMED7 Jan 4, 2023
8250c09
Fix errors in https://github.com/SOHELAHMED7/yii2-openapi/pull/10
SOHELAHMED7 Jan 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix enum related issues + add more tests for it for all 3 DBs
  • Loading branch information
SOHELAHMED7 committed Dec 24, 2022
commit a037827df3a544010a791dc34dc63f8792363255
40 changes: 24 additions & 16 deletions src/lib/migrations/BaseMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,6 @@ function (string $unknownColumn) {
// do not adjust existing primary keys
continue;
}
// TODO this section is not concretely correct
// type can be one of: char, string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
// timestamp, time, date, binary, and money.
// `enum` is not a part of it
// also real DB type required enum values in MySQL and new `TYPE` in PgSQL
if (!empty($current->enumValues)) {
$current->type = 'enum';
$current->dbType = 'enum';
}
if (!empty($desired->enumValues)) {
$desired->type = 'enum';
$desired->dbType = 'enum';
}
// section end
$changedAttributes = $this->compareColumns($current, $desired);
if (empty($changedAttributes)) {
continue;
Expand Down Expand Up @@ -440,9 +426,11 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema):
}

// create enum if relevant
if (ApiGenerator::isPostgres() && !empty($columnSchema->enumValues)) {
if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {
$allEnumValues = $columnSchema->enumValues;
$allEnumValues = array_map(function ($aValue) {return "'$aValue'";}, $allEnumValues);
$allEnumValues = array_map(function ($aValue) {
return "'$aValue'";
}, $allEnumValues);
Yii::$app->db->createCommand(
'CREATE TYPE enum_'.$columnSchema->name.' AS ENUM('.implode(', ', $allEnumValues).')'
)->execute();
Expand All @@ -466,4 +454,24 @@ public function newColStr(\cebe\yii2openapi\db\ColumnSchema $columnSchema): stri
$ctc = new ColumnToCode(\Yii::$app->db->schema, $columnSchema, false, false, true);
return ColumnToCode::undoEscapeQuotes($ctc->getCode());
}

public static function isEnum(\yii\db\ColumnSchema $columnSchema): bool
{
if (!empty($columnSchema->enumValues) && is_array($columnSchema->enumValues)) {
return true;
}
return false;
}

public static function isEnumValuesChanged(
\yii\db\ColumnSchema $current,
\yii\db\ColumnSchema $desired
): bool
{
if (static::isEnum($current) && static::isEnum($desired) &&
$current->enumValues !== $desired->enumValues) {
return true;
}
return false;
}
}
16 changes: 2 additions & 14 deletions src/lib/migrations/MysqlMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
foreach ($changed as $attr) {
$newColumn->$attr = $desired->$attr;
}
if (!empty($newColumn->enumValues)) {
$newColumn->dbType = 'enum';
if (static::isEnum($newColumn)) {
$newColumn->dbType = 'enum'; // TODO this is concretely not correct
}
$this->migration->addUpCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $newColumn))
->addDownCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $current));
Expand Down Expand Up @@ -119,12 +119,6 @@ public function modifyCurrent(ColumnSchema $current): void
if ($current->phpType === 'integer' && $current->defaultValue !== null) {
$current->defaultValue = (int)$current->defaultValue;
}

// TODO this is not concretely correct, reason is in BaseMigrationBuilder
if (!empty($current->enumValues)) {
$current->type = 'enum';
$current->dbType = 'enum';
}
}

public function modifyDesired(ColumnSchema $desired): void
Expand All @@ -138,12 +132,6 @@ public function modifyDesired(ColumnSchema $desired): void
$desired->precision = $decimalAttributes['precision'];
$desired->scale = $decimalAttributes['scale'];
}

// TODO this is not concretely correct, reason is in BaseMigrationBuilder
if (!empty($desired->enumValues)) {
$desired->type = 'enum';
$desired->dbType = 'enum';
}
}

public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSchema $desired): void
Expand Down
22 changes: 6 additions & 16 deletions src/lib/migrations/PostgresMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function buildColumnsCreation(array $columns):void
{
foreach ($columns as $column) {
$tableName = $this->model->getTableAlias();
if ($column->dbType === 'enum') {
if (static::isEnum($column)) {
$this->migration->addUpCode($this->recordBuilder->createEnum($column->name, $column->enumValues))
->addDownCode($this->recordBuilder->dropEnum($column->name), true);
}
Expand All @@ -41,8 +41,8 @@ protected function buildColumnsDrop(array $columns):void
$tableName = $this->model->getTableAlias();
$this->migration->addDownCode($this->recordBuilder->addDbColumn($tableName, $column))
->addUpCode($this->recordBuilder->dropColumn($tableName, $column->name));
if ($column->dbType === 'enum') {
$this->migration->addDownCode($this->recordBuilder->createEnum($column->name, $column->enumValues), true)
if (static::isEnum($column)) {
$this->migration->addDownCode($this->recordBuilder->createEnum($column->name, $column->enumValues))
->addUpCode($this->recordBuilder->dropEnum($column->name));
}
}
Expand All @@ -54,9 +54,9 @@ protected function buildColumnsDrop(array $columns):void
protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desired, array $changed):void
{
$tableName = $this->model->getTableAlias();
$isChangeToEnum = $current->type !== $desired->type && !empty($desired->enumValues);
$isChangeFromEnum = $current->type !== $desired->type && !empty($current->enumValues);
$isChangedEnum = $current->type === $desired->type && !empty($current->enumValues);
$isChangeToEnum = !static::isEnum($current) && static::isEnum($desired);
$isChangeFromEnum = static::isEnum($current) && !static::isEnum($desired);
$isChangedEnum = static::isEnumValuesChanged($current, $desired);
if ($isChangedEnum) {
// Generation for change enum values not supported. Do it manually
// This action require several steps and can't be applied during single transaction
Expand Down Expand Up @@ -210,11 +210,6 @@ public function modifyCurrent(ColumnSchema $current): void
if ($current->phpType === 'integer' && $current->defaultValue !== null) {
$current->defaultValue = (int)$current->defaultValue;
}
// TODO this is not concretely correct, reason is in BaseMigrationBuilder
if (!empty($current->enumValues)) {
$current->type = 'enum';
$current->dbType = 'enum';
}
}

public function modifyDesired(ColumnSchema $desired): void
Expand All @@ -227,11 +222,6 @@ public function modifyDesired(ColumnSchema $desired): void
$desired->precision = $decimalAttributes['precision'];
$desired->scale = $decimalAttributes['scale'];
}
// TODO this is not concretely correct, reason is in BaseMigrationBuilder
if (!empty($desired->enumValues)) {
$desired->type = 'enum';
$desired->dbType = 'enum';
}
}

public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSchema $desired): void
Expand Down
28 changes: 28 additions & 0 deletions tests/specs/enum/enum.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ components:
TV
nullable: false

Newcolumn:
type: object
description: New Fresh table with new enum column for migration code generation
required:
- id
properties:
id:
type: integer
# device:
# type: string
# maxLength: 8
# enum:
# - MOBILE
# - TV
# - COMPUTER
# default:
# TV
# nullable: false
new_column:
type: string
enum:
- ONE
- TWO
- THREE
default:
ONE
nullable: false

Editcolumn:
type: object
description: Table with edit enum columns for migration code generation
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ public function testEnumFresh()
// $this->compareFiles($actualFiles, $expectedFiles);
}

public function testAddNewEnumColumn()
{
$this->deleteTables();
$this->createTableForNewEnumColumn();
$testFile = Yii::getAlias("@specs/enum/enum.php");
$this->runGenerator($testFile, 'mysql');

$this->changeDbToMariadb();
$this->deleteTables();
$this->createTableForNewEnumColumn();
$testFile = Yii::getAlias("@specs/enum/enum.php");
$this->runGenerator($testFile, 'maria');

$this->changeDbToPgsql();
$this->deleteTables();
$this->createTableForNewEnumColumn();
$testFile = Yii::getAlias("@specs/enum/enum.php");
$this->runGenerator($testFile, 'pgsql');
}

public function testEnumToString()
{
$this->deleteTables();
Expand Down Expand Up @@ -83,6 +103,9 @@ private function deleteTables()
{
if (ApiGenerator::isPostgres()) {
Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_device CASCADE')->execute();
Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_connection CASCADE')->execute();
Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_new_column CASCADE')->execute();
Yii::$app->db->createCommand('DROP TYPE IF EXISTS enum_delete_col CASCADE')->execute();
}
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%newcolumns}}')->execute();
Expand All @@ -107,4 +130,21 @@ private function createTableForEditEnumToString()
'connection' => 'string'
])->execute();
}

private function createTableForNewEnumColumn()
{
if (ApiGenerator::isPostgres()) {
Yii::$app->db->createCommand('CREATE TYPE enum_delete_col AS ENUM(\'FOUR\', \'FIVE\', \'SIX\')')->execute();
Yii::$app->db->createCommand()->createTable('{{%newcolumns}}', [
'id' => 'pk',
'delete_col' => 'enum_delete_col'
])->execute();
return;
}

Yii::$app->db->createCommand()->createTable('{{%newcolumns}}', [
'id' => 'pk',
'delete_col' => 'enum("FOUR", "FIVE", "SIX")'
])->execute();
}
}