Skip to content

Commit 990ae26

Browse files
committed
Fix lot of issues and failing test + proper handling of text[] (array), decimal enum and more
1 parent feba5f1 commit 990ae26

9 files changed

+181
-186
lines changed

src/lib/AttributeResolver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ protected function resolveJunctionTableProperty(PropertySchema $property, bool $
147147
->asReference($junkAttribute['relatedClassName'])
148148
->setPhpType($junkAttribute['phpType'])
149149
->setDbType($junkAttribute['dbType'])
150-
->setForeignKeyColumnName($property->fkColName);
150+
->setForeignKeyColumnName($property->fkColName)
151+
->setTableName($this->schema->resolveTableName($this->schemaName));
151152
$relation = Yii::createObject(AttributeRelation::class, [
152153
$property->getName(),
153154
$junkAttribute['relatedTableName'],
@@ -228,7 +229,8 @@ protected function resolveProperty(
228229
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
229230
->setNullable($nullableValue)
230231
->setIsPrimary($property->isPrimaryKey())
231-
->setForeignKeyColumnName($property->fkColName);
232+
->setForeignKeyColumnName($property->fkColName)
233+
->setTableName($this->schema->resolveTableName($this->schemaName));
232234
if ($property->isReference()) {
233235
if ($property->isVirtual()) {
234236
throw new InvalidDefinitionException('References not supported for virtual attributes');

src/lib/ColumnToCode.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,9 @@ private function getIsBuiltinType($type, $dbType)
398398
private function resolveEnumType():void
399399
{
400400
if (ApiGenerator::isPostgres()) {
401-
$rawTableName = $this->dbSchema->getRawTableName($this->tableAlias);
402-
$this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"';
401+
// $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias);
402+
// $this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"';
403+
$this->rawParts['type'] = '"'.$this->column->dbType.'"';
403404
return;
404405
}
405406
$this->rawParts['type'] = 'enum(' . self::mysqlEnumToString($this->column->enumValues) . ')';

src/lib/SchemaToDatabase.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use cebe\openapi\exceptions\IOException;
1111
use cebe\openapi\exceptions\TypeErrorException;
1212
use cebe\openapi\exceptions\UnresolvableReferenceException;
13+
use cebe\yii2openapi\generator\ApiGenerator;
1314
use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException;
1415
use cebe\yii2openapi\lib\items\Attribute;
1516
use cebe\yii2openapi\lib\items\DbModel;
@@ -278,7 +279,7 @@ public static function f7(array $schemasToDrop): array // TODO rename
278279
'pkName' => $table->primaryKey[0],
279280
'name' => $schemaName,
280281
'tableName' => $tableName,
281-
'attributes' => static::attributesFromColumnSchemas($table->columns),
282+
'attributes' => static::attributesFromColumnSchemas(static::enhanceColumnSchemas($table->columns)),
282283
'drop' => true
283284
]);
284285
$dbModelsToDrop[$key] = $dbModelHere;
@@ -325,14 +326,25 @@ public static function attributesFromColumnSchemas(array $columnSchemas)
325326
'defaultValue' => $columnSchema->defaultValue,
326327
'description' => $columnSchema->comment,
327328
]);
329+
$attributes[] = $attribute;
330+
}
331+
return $attributes;
332+
}
328333

334+
public static function enhanceColumnSchemas(array $columnSchemas)
335+
{
336+
foreach ($columnSchemas as $columnSchema) {
329337
// PgSQL array
330338
if (property_exists($columnSchema, 'dimension') && $columnSchema->dimension !== 0) {
331339
for ($i = 0; $i < $columnSchema->dimension; $i++) {
332-
$attribute->dbType .= '[]';
340+
$columnSchema->dbType .= '[]';
333341
}
334342
}
335343

344+
if (ApiGenerator::isPostgres() && $columnSchema->type === Schema::TYPE_DECIMAL) {
345+
$columnSchema->dbType .= '('.$columnSchema->precision.','.$columnSchema->scale.')';
346+
}
347+
336348
// generate PK using `->primaryKeys()` or similar methods instead of separate SQL statement which sets only PK to a column of table
337349
// https://github.com/cebe/yii2-openapi/issues/132
338350
if (in_array($columnSchema->phpType, [
@@ -344,21 +356,19 @@ public static function attributesFromColumnSchemas(array $columnSchemas)
344356
str_ireplace(['BIGINT', 'int8', 'bigserial', 'serial8'], 'nothing', $columnSchema->dbType, $count); # can be refactored if https://github.com/yiisoft/yii2/issues/20209 is fixed
345357
if ($count) {
346358
if ($columnSchema->unsigned) {
347-
$attribute->dbType = Schema::TYPE_UBIGPK;
359+
$columnSchema->dbType = Schema::TYPE_UBIGPK;
348360
} else {
349-
$attribute->dbType = Schema::TYPE_BIGPK;
361+
$columnSchema->dbType = Schema::TYPE_BIGPK;
350362
}
351363
} else {
352364
if ($columnSchema->unsigned) {
353-
$attribute->dbType = Schema::TYPE_UPK;
365+
$columnSchema->dbType = Schema::TYPE_UPK;
354366
} else {
355-
$attribute->dbType = Schema::TYPE_PK;
367+
$columnSchema->dbType = Schema::TYPE_PK;
356368
}
357369
}
358370
}
359-
360-
$attributes[] = $attribute;
361371
}
362-
return $attributes;
372+
return $columnSchemas;
363373
}
364374
}

src/lib/items/Attribute.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class Attribute extends BaseObject
124124
**/
125125
public $fakerStub;
126126

127+
public $tableName; // required for PgSQL enum
128+
127129
/**
128130
* @var bool
129131
**/
@@ -148,6 +150,12 @@ public function setDbType(string $dbType):Attribute
148150
return $this;
149151
}
150152

153+
public function setTableName(string $tableName):Attribute
154+
{
155+
$this->tableName = $tableName;
156+
return $this;
157+
}
158+
151159
public function setXDbType($xDbType):Attribute
152160
{
153161
$this->xDbType = $xDbType;
@@ -326,6 +334,9 @@ public function toColumnSchema():ColumnSchema
326334
}
327335
if (is_array($this->enumValues)) {
328336
$column->enumValues = $this->enumValues;
337+
if (ApiGenerator::isPostgres() && empty($this->xDbType)) {
338+
$column->dbType = 'enum_'.Yii::$app->db->tablePrefix.$this->tableName.'_' . $column->name;
339+
}
329340
}
330341
$this->handleDecimal($column);
331342

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use cebe\yii2openapi\lib\items\DbModel;
1313
use cebe\yii2openapi\lib\items\ManyToManyRelation;
1414
use cebe\yii2openapi\lib\items\MigrationModel;
15+
use cebe\yii2openapi\lib\SchemaToDatabase;
1516
use Yii;
1617
use yii\db\ColumnSchema;
1718
use yii\db\Connection;
@@ -450,6 +451,9 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch
450451
} else {
451452
$column = [$columnSchema->name => $this->newColStr($tmpTableName, $columnSchema)];
452453
if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {
454+
$clonedSchema = clone $columnSchema;
455+
$clonedSchema->dbType = trim($innerEnumTypeName, '"');
456+
$column = [$columnSchema->name => $this->newColStr($tmpTableName, $clonedSchema)];
453457
$column[$columnSchema->name] = strtr($column[$columnSchema->name], [$innerEnumTypeName => $tmpEnumName($columnSchema->name)]);
454458
}
455459
}
@@ -600,8 +604,8 @@ public function buildTablesDrop(): void
600604
->addDownCode(
601605
$this->recordBuilder->createTable(
602606
$this->model->getTableAlias(),
603-
$this->model->attributesToColumnSchema()
604-
// $this->tableSchema->columns
607+
// $this->model->attributesToColumnSchema()
608+
SchemaToDatabase::enhanceColumnSchemas($this->tableSchema->columns)
605609
)
606610
);
607611
}

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function modifyCurrent(ColumnSchema $current): void
232232

233233
public function modifyDesired(ColumnSchema $desired): void
234234
{
235-
/** @var $desired cebe\yii2openapi\db\ColumnSchema|\yii\db\pgsql\ColumnSchema */
235+
/** @var $desired \cebe\yii2openapi\db\ColumnSchema|\yii\db\pgsql\ColumnSchema */
236236
if (in_array($desired->phpType, ['int', 'integer']) && $desired->defaultValue !== null) {
237237
$desired->defaultValue = (int)$desired->defaultValue;
238238
}
@@ -245,7 +245,7 @@ public function modifyDesired(ColumnSchema $desired): void
245245
public function modifyDesiredInContextOfCurrent(ColumnSchema $current, ColumnSchema $desired): void
246246
{
247247
/** @var $current \yii\db\pgsql\ColumnSchema */
248-
/** @var $desired cebe\yii2openapi\db\ColumnSchema|\yii\db\pgsql\ColumnSchema */
248+
/** @var $desired \cebe\yii2openapi\db\ColumnSchema|\yii\db\pgsql\ColumnSchema */
249249
if ($current->type === $desired->type && !$desired->size && $this->isDbDefaultSize($current)) {
250250
$desired->size = $current->size;
251251
}

tests/DbTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ protected function runDownMigrations(string $db = 'mysql', int $number = 2): voi
180180
exec('cd tests; php -dxdebug.mode=develop ./yii migrate-'.$db.'/down --interactive=0 '.$number, $downOutput, $downExitCode);
181181
$last = count($downOutput) - 1;
182182
$lastThird = count($downOutput) - 3;
183-
$this->assertSame($downExitCode, 0);
184183
$this->assertSame($downOutput[$last], 'Migrated down successfully.');
184+
$this->assertSame($downExitCode, 0);
185185
$this->assertSame($downOutput[$lastThird], $number.' '.(($number === 1) ? 'migration was' : 'migrations were').' reverted.');
186186
}
187187
}

0 commit comments

Comments
 (0)