Skip to content
Prev Previous commit
Next Next commit
Fix cebe#125
  • Loading branch information
SOHELAHMED7 committed Dec 30, 2022
commit c73c16d99ea688ed09af3500d53d95dec342c2f1
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,3 @@ Professional support, consulting as well as software development services are av
https://www.cebe.cc/en/contact

Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud).

12 changes: 3 additions & 9 deletions src/lib/migrations/BaseMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,18 +400,12 @@ protected function unPrefixTableName(string $tableName):string
return str_replace($this->db->tablePrefix, '', $tableName);
}

protected function isNeedUsingExpression(string $fromType, string $toType):bool
protected function isNeedUsingExpression(string $fromDbType, string $toDbType):bool
{
$strings = ['string', 'text', 'char'];
if (in_array($fromType, $strings) && in_array($toType, $strings)) {
if ($fromDbType === $toDbType) {
return false;
}
$ints = ['smallint', 'integer', 'bigint', 'float', 'decimal'];
if (in_array($fromType, $ints) && in_array($toType, $ints)) {
return false;
}
$dates = ['date', 'timestamp'];
return !(in_array($fromType, $dates) && in_array($toType, $dates));
return true;
}

public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema
Expand Down
2 changes: 1 addition & 1 deletion src/lib/migrations/PostgresMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
, 'dbType', 'phpType'
, 'precision', 'scale', 'unsigned'
], $changed))) {
$addUsing = $this->isNeedUsingExpression($current->type, $desired->type);
$addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType);
$this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired));
$this->migration->addDownCode($this->recordBuilder->alterColumnTypeFromDb($tableName, $current, $addUsing));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function safeUp()
public function safeDown()
{
$this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true);
$this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->integer()->null());
$this->alterColumn('{{%v2_posts}}', 'category_id', $this->integer()->notNull());
$this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int4 NULL USING "created_by_id"::int4');
$this->alterColumn('{{%v2_posts}}', 'category_id', 'int4 NOT NULL USING "category_id"::int4');
$this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger()->notNull());
$this->dropColumn('{{%v2_posts}}', 'lang');
$this->dropColumn('{{%v2_posts}}', 'id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function safeDown()
{
$this->dropIndex('v2_categories_title_index', '{{%v2_categories}}');
$this->createIndex('v2_categories_title_key', '{{%v2_categories}}', 'title', true);
$this->alterColumn('{{%v2_categories}}', 'title', $this->string(255)->notNull());
$this->alterColumn('{{%v2_categories}}', 'title', 'varchar(255) NOT NULL USING "title"::varchar');
$this->dropColumn('{{%v2_categories}}', 'cover');
$this->alterColumn('{{%v2_categories}}', 'active', "SET DEFAULT 'f'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function safeDown()
$this->dropIndex('v2_users_role_flags_hash_index', '{{%v2_users}}');
$this->dropIndex('v2_users_login_key', '{{%v2_users}}');
$this->createIndex('v2_users_username_key', '{{%v2_users}}', 'username', true);
$this->alterColumn('{{%v2_users}}', 'role', $this->string(20)->null());
$this->alterColumn('{{%v2_users}}', 'role', 'varchar(20) NULL USING "role"::varchar');
$this->alterColumn('{{%v2_users}}', 'email', $this->string(200)->notNull());
$this->addColumn('{{%v2_users}}', 'username', $this->string(200)->notNull());
$this->dropColumn('{{%v2_users}}', 'login');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function safeDown()
{
$this->execute('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')');
$this->alterColumn('{{%editcolumns}}', 'device', 'enum_device USING "device"::enum_device');
$this->alterColumn('{{%editcolumns}}', 'connection', $this->string(255)->null());
$this->alterColumn('{{%editcolumns}}', 'connection', 'varchar(255) NULL USING "connection"::varchar');
$this->alterColumn('{{%editcolumns}}', 'connection', "DROP NOT NULL");
$this->alterColumn('{{%editcolumns}}', 'connection', "DROP DEFAULT");
$this->execute('DROP TYPE enum_connection');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.yaml',
'generateUrls' => false,
'generateModels' => false,
'excludeModels' => [
'Error',
],
'generateControllers' => false,
'generateMigrations' => true,
'generateModelFaker' => false,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Fix https://github.com/cebe/yii2-openapi/issues/107 Migrations are generated with syntax error and wrong data type in MySQL
paths:
/:
get:
summary: List
operationId: list
responses:
'200':
description: The information

components:
schemas:
Fruit:
type: object
description: A table to fix \#107
required:
- id
- name
properties:
id:
type: integer
name:
# type: array
# x-db-type: JSON
# nullable: false
# default: '{}'

type: array
x-db-type: text[]
nullable: false
# default: '{}'


test_emails:
type: array
x-db-type: JSON
nullable: false
default: '[]'
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public function safeUp()

public function safeDown()
{
$this->alterColumn('{{%editcolumns}}', 'string_col', $this->string(255)->notNull());
$this->alterColumn('{{%editcolumns}}', 'string_col', 'varchar(255) NOT NULL USING "string_col"::varchar');
$this->alterColumn('{{%editcolumns}}', 'numeric_col', 'int4 NULL USING "numeric_col"::int4');
$this->alterColumn('{{%editcolumns}}', 'name', $this->string(255)->notNull());
$this->alterColumn('{{%editcolumns}}', 'json_col', 'jsonb NULL USING "json_col"::jsonb');
$this->alterColumn('{{%editcolumns}}', 'dec_col', $this->decimal()->null());
$this->alterColumn('{{%editcolumns}}', 'dec_col', 'numeric NULL USING "dec_col"::numeric');
$this->dropColumn('{{%editcolumns}}', 'text_col_array');
$this->dropColumn('{{%editcolumns}}', 'json_col_def_n_2');
$this->dropColumn('{{%editcolumns}}', 'json_col_def_n');
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace tests\unit;

use Yii;
use tests\DbTestCase;
use yii\helpers\FileHelper;

// This class contains tests for various issues present at GitHub
class IssueFixTest extends DbTestCase
{
// fix https://github.com/cebe/yii2-openapi/issues/107
// 107_no_syntax_error
public function testMigrationsAreNotGeneratedWithSyntaxError()
{
// $testFile = Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php");
// $this->runGenerator($testFile, 'mysql');
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
// 'recursive' => true,
// ]);
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/id_not_in_rules/app"), [
// 'recursive' => true,
// ]);
// $this->checkFiles($actualFiles, $expectedFiles);

// $this->changeDbToMariadb();
// $this->deleteTablesForNoSyntaxError107();
// $this->createTableForNoSyntaxError107();
// $testFile = Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php");
// $this->runGenerator($testFile, 'maria');

$this->changeDbToPgsql();
$this->deleteTablesForNoSyntaxError107();
$this->createTableForNoSyntaxError107();
$testFile = Yii::getAlias("@specs/issue_fix/no_syntax_error_107/mysql/no_syntax_error_107.php");
$this->runGenerator($testFile, 'pgsql');
}

private function deleteTablesForNoSyntaxError107()
{
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
}

private function createTableForNoSyntaxError107()
{
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
'id' => 'pk',
'name' => 'varchar(255)',
])->execute();
}
}