Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 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
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
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 multiple issue + add more tests for eunm in Pgsql + other DB
  • Loading branch information
SOHELAHMED7 committed Dec 23, 2022
commit fcc53a20e1d8ef53ffd50efad922610b28a5d20b
2 changes: 1 addition & 1 deletion src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function getCode(bool $quoted = false):string
public function getAlterExpression(bool $addUsingExpression = false):string
{
if ($this->isEnum() && ApiGenerator::isPostgres()) {
return "'" . sprintf('enum_%1$s USING %1$s::enum_%1$s', $this->column->name) . "'";
return "'" . sprintf('enum_%1$s USING "%1$s"::enum_%1$s', $this->column->name) . "'";
}
if ($this->column->dbType === 'tsvector') {
return "'" . $this->rawParts['type'] . "'";
Expand Down
20 changes: 20 additions & 0 deletions src/lib/migrations/BaseMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace cebe\yii2openapi\lib\migrations;

use cebe\yii2openapi\generator\ApiGenerator;
use cebe\yii2openapi\lib\ColumnToCode;
use cebe\yii2openapi\lib\items\DbModel;
use cebe\yii2openapi\lib\items\ManyToManyRelation;
Expand Down Expand Up @@ -203,6 +204,11 @@ 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';
Expand All @@ -211,6 +217,7 @@ function (string $unknownColumn) {
$desired->type = 'enum';
$desired->dbType = 'enum';
}
// section end
$changedAttributes = $this->compareColumns($current, $desired);
if (empty($changedAttributes)) {
continue;
Expand Down Expand Up @@ -432,12 +439,25 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema):
$column = [$columnSchema->name => $this->newColStr($columnSchema)];
}

// create enum if relevant
if (ApiGenerator::isPostgres() && !empty($columnSchema->enumValues)) {
$allEnumValues = $columnSchema->enumValues;
$allEnumValues = array_map(function ($aValue) {return "'$aValue'";}, $allEnumValues);
Yii::$app->db->createCommand(
'CREATE TYPE enum_'.$columnSchema->name.' AS ENUM('.implode(', ', $allEnumValues).')'
)->execute();
}

Yii::$app->db->createCommand()->createTable($tableName, $column)->execute();

$table = Yii::$app->db->getTableSchema($tableName);

Yii::$app->db->createCommand()->dropTable($tableName)->execute();

if (ApiGenerator::isPostgres() && !empty($columnSchema->enumValues)) {// drop enum
Yii::$app->db->createCommand('DROP TYPE enum_'.$columnSchema->name)->execute();
}

return $table->columns[$columnSchema->name];
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib/migrations/MysqlMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public function modifyCurrent(ColumnSchema $current): void
if ($current->phpType === 'integer' && $current->defaultValue !== null) {
$current->defaultValue = (int)$current->defaultValue;
}

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

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

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

public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSchema $desired): void
Expand Down
12 changes: 11 additions & 1 deletion src/lib/migrations/PostgresMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
return;
}

if (!empty(array_intersect(['type', 'size'], $changed))) {
if (!empty(array_intersect(['type', 'size'/*, 'dbType'*/], $changed))) {
$addUsing = $this->isNeedUsingExpression($desired->type, $current->type);
$this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired));
$this->migration->addDownCode($this->recordBuilder->alterColumnTypeFromDb($tableName, $current, $addUsing));
Expand Down Expand Up @@ -207,6 +207,11 @@ public function modifyCurrent(ColumnSchema $current): void
if ($current->phpType === 'integer' && $current->defaultValue !== null) {
$current->defaultValue = (int)$current->defaultValue;
}
// TODO this is not concretely correct
if (!empty($current->enumValues)) {
$current->type = 'enum';
$current->dbType = 'enum';
}
}

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

public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSchema $desired): void
Expand Down
16 changes: 8 additions & 8 deletions tests/specs/enum/enum.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ components:
type: integer
device:
type: string
# maxLength: 8
# enum:
# - MOBILE
# - TV
# - COMPUTER
# default:
# TV
# nullable: false
connection:
type: string
enum:
- WIRED
- WIRELESS
default:
WIRED
nullable: false
8 changes: 7 additions & 1 deletion tests/unit/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public function testEnumToString()
}

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

// public function testChangeEnumValues()
Expand Down Expand Up @@ -93,12 +97,14 @@ private function createTableForEditEnumToString()
Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [
'id' => 'pk',
'device' => 'enum_device NOT NULL DEFAULT \'TV\'',
'connection' => 'string'
])->execute();
return;
}
Yii::$app->db->createCommand()->createTable('{{%editcolumns}}', [
'id' => 'pk',
'device' => 'enum("MOBILE", "TV", "COMPUTER") NOT NULL DEFAULT \'TV\'',
'connection' => 'string'
])->execute();
}
}