Skip to content

Commit c8e3b43

Browse files
authored
Merge pull request #26 from SOHELAHMED7/149-wrong-migration-for-pgsql-is-generated-for-string-varchar-datatype
Resolve Wrong migration for Pgsql is generated for string/varchar datatype cebe#149
2 parents e866b3a + f564b16 commit c8e3b43

10 files changed

+116
-6
lines changed

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,4 +568,12 @@ public function modifyDesiredFromDbInContextOfDesired(ColumnSchema $desired, Col
568568
$desiredFromDb->dbType = 'timestamp';
569569
}
570570
}
571+
572+
public function modifyDesiredInContextOfDesiredFromDb(ColumnSchema $desired, ColumnSchema $desiredFromDb): void
573+
{
574+
if (property_exists($desired, 'xDbType') && is_string($desired->xDbType) && !empty($desired->xDbType)) {
575+
return;
576+
}
577+
$desired->dbType = $desiredFromDb->dbType;
578+
}
571579
}

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
4747

4848
// Why this is needed? Often manually created ColumnSchem instance have dbType 'varchar' with size 255 and ColumnSchema fetched from db have 'varchar(255)'. So varchar !== varchar(255). such normal mistake was leading to errors. So desired column is saved in temporary table and it is fetched from that temp. table and then compared with current ColumnSchema
4949
$desiredFromDb = $this->tmpSaveNewCol($tableAlias, $desired);
50+
51+
$this->modifyDesiredInContextOfDesiredFromDb($desired, $desiredFromDb);
52+
5053
$this->modifyDesired($desiredFromDb);
5154
$this->modifyDesiredInContextOfCurrent($current, $desiredFromDb);
5255
$this->modifyDesiredFromDbInContextOfDesired($desired, $desiredFromDb);

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
118118

119119
// for docs, please see MysqlMigrationBuilder file
120120
$desiredFromDb = $this->tmpSaveNewCol($tableAlias, $desired);
121+
122+
$this->modifyDesiredInContextOfDesiredFromDb($desired, $desiredFromDb);
123+
121124
$this->modifyDesired($desiredFromDb);
122125
$this->modifyDesiredInContextOfCurrent($current, $desiredFromDb);
123126
$this->modifyDesiredFromDbInContextOfDesired($desired, $desiredFromDb);

tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public function safeUp()
1111
$this->execute('CREATE TYPE "enum_itt_v2_posts_lang" AS ENUM(\'ru\', \'eng\')');
1212
$this->addColumn('{{%v2_posts}}', 'lang', '"enum_itt_v2_posts_lang" NULL DEFAULT \'ru\'');
1313
$this->dropColumn('{{%v2_posts}}', 'uid');
14-
$this->alterColumn('{{%v2_posts}}', 'category_id', 'bigint NOT NULL USING "category_id"::bigint');
14+
$this->alterColumn('{{%v2_posts}}', 'category_id', 'int8 NOT NULL USING "category_id"::int8');
1515
$this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT");
16-
$this->alterColumn('{{%v2_posts}}', 'created_by_id', 'bigint NULL USING "created_by_id"::bigint');
16+
$this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int8 NULL USING "created_by_id"::int8');
1717
$this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}');
1818
}
1919

tests/specs/blog_v2/migrations_pgsql_db/m200000_000003_change_table_v2_categories.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class m200000_000003_change_table_v2_categories extends \yii\db\Migration
88
public function safeUp()
99
{
1010
$this->addColumn('{{%v2_categories}}', 'cover', $this->text()->notNull());
11-
$this->alterColumn('{{%v2_categories}}', 'title', 'string(100) NOT NULL USING "title"::string');
11+
$this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull());
1212
$this->alterColumn('{{%v2_categories}}', 'active', "DROP DEFAULT");
1313
$this->dropIndex('v2_categories_title_key', '{{%v2_categories}}');
1414
$this->createIndex('v2_categories_title_index', '{{%v2_categories}}', 'title', false);
@@ -18,7 +18,7 @@ public function safeDown()
1818
{
1919
$this->dropIndex('v2_categories_title_index', '{{%v2_categories}}');
2020
$this->createIndex('v2_categories_title_key', '{{%v2_categories}}', 'title', true);
21-
$this->alterColumn('{{%v2_categories}}', 'title', 'varchar(255) NOT NULL USING "title"::varchar');
21+
$this->alterColumn('{{%v2_categories}}', 'title', $this->string(255)->notNull());
2222
$this->dropColumn('{{%v2_categories}}', 'cover');
2323
$this->alterColumn('{{%v2_categories}}', 'active', "SET DEFAULT 'f'");
2424
}

tests/specs/blog_v2/migrations_pgsql_db/m200000_000005_change_table_v2_comments.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public function safeUp()
1313
$this->dropColumn('{{%v2_comments}}', 'author_id');
1414
$this->alterColumn('{{%v2_comments}}', 'message', 'text NOT NULL USING "message"::text');
1515
$this->alterColumn('{{%v2_comments}}', 'message', "DROP DEFAULT");
16-
$this->alterColumn('{{%v2_comments}}', 'meta_data', 'string(300) NULL USING "meta_data"::string');
16+
$this->alterColumn('{{%v2_comments}}', 'meta_data', 'varchar(300) NULL USING "meta_data"::varchar');
1717
$this->alterColumn('{{%v2_comments}}', 'meta_data', "DROP NOT NULL");
1818
$this->alterColumn('{{%v2_comments}}', 'meta_data', "SET DEFAULT ''");
19-
$this->alterColumn('{{%v2_comments}}', 'created_at', 'datetime NOT NULL USING "created_at"::datetime');
19+
$this->alterColumn('{{%v2_comments}}', 'created_at', 'timestamp NOT NULL USING "created_at"::timestamp');
2020
$this->addForeignKey('fk_v2_comments_post_id_v2_posts_id', '{{%v2_comments}}', 'post_id', '{{%v2_posts}}', 'id');
2121
$this->addForeignKey('fk_v2_comments_user_id_v2_users_id', '{{%v2_comments}}', 'user_id', '{{%v2_users}}', 'id');
2222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* Table for Fruit
5+
*/
6+
class m200000_000000_change_table_fruits extends \yii\db\Migration
7+
{
8+
public function safeUp()
9+
{
10+
$this->alterColumn('{{%fruits}}', 'name', $this->string(151)->notNull());
11+
$this->alterColumn('{{%fruits}}', 'name', "SET NOT NULL");
12+
}
13+
14+
public function safeDown()
15+
{
16+
$this->alterColumn('{{%fruits}}', 'name', $this->string(150)->null());
17+
$this->alterColumn('{{%fruits}}', 'name', "DROP NOT NULL");
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/wrong_migration_for_pgsql_is_generated_for_string_varchar_datatype_149/wrong_migration_for_pgsql_is_generated_for_string_varchar_datatype_149.yaml',
5+
'generateUrls' => false,
6+
'generateModels' => false,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => true,
12+
'generateModelFaker' => false,
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Fix https://github.com/cebe/yii2-openapi/issues/149 wrong_migration_for_pgsql_is_generated_for_string_varchar_datatype_149
5+
paths:
6+
/:
7+
get:
8+
summary: List
9+
operationId: list
10+
responses:
11+
'200':
12+
description: The information
13+
14+
components:
15+
schemas:
16+
Fruit:
17+
type: object
18+
description: A table to fix \#149
19+
required:
20+
- id
21+
- name
22+
properties:
23+
id:
24+
type: integer
25+
name:
26+
type: string
27+
example: desc
28+
maxLength: 151

tests/unit/IssueFixTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private function deleteTables()
6464
$this->deleteTablesForNoSyntaxError107();
6565
$this->deleteTableForQuoteInAlterColumn();
6666
$this->deleteTableForTimestampIssue143();
67+
$this->deleteTablesForWrongMigrationForPgsqlForStringVarcharDatatype149();
6768
}
6869

6970
private function deleteTablesForFloatIssue()
@@ -221,4 +222,39 @@ public function testModelNameMoreThanOnceInFakerIssue148()
221222
]);
222223
$this->checkFiles($actualFiles, $expectedFiles);
223224
}
225+
226+
// https://github.com/cebe/yii2-openapi/issues/149
227+
// wrongMigrationForPgsqlForStringVarcharDatatype
228+
// wrong_migration_for_pgsql_is_generated_for_string_varchar_datatype
229+
public function testWrongMigrationForPgsqlForStringVarcharDatatype149()
230+
{
231+
$this->changeDbToPgsql();
232+
$this->deleteTablesForWrongMigrationForPgsqlForStringVarcharDatatype149();
233+
$this->createTableForWrongMigrationForPgsqlForStringVarcharDatatype149();
234+
$testFile = Yii::getAlias("@specs/issue_fix/wrong_migration_for_pgsql_is_generated_for_string_varchar_datatype_149/wrong_migration_for_pgsql_is_generated_for_string_varchar_datatype_149.php");
235+
$this->runGenerator($testFile, 'pgsql');
236+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
237+
'recursive' => true,
238+
]);
239+
240+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/wrong_migration_for_pgsql_is_generated_for_string_varchar_datatype_149/app"), [
241+
'recursive' => true,
242+
]);
243+
$this->checkFiles($actualFiles, $expectedFiles);
244+
$this->runActualMigrations('pgsql', 1);
245+
$this->deleteTables();
246+
}
247+
248+
private function createTableForWrongMigrationForPgsqlForStringVarcharDatatype149()
249+
{
250+
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
251+
'id' => 'pk',
252+
'name' => 'string(150)', # not null
253+
])->execute();
254+
}
255+
256+
private function deleteTablesForWrongMigrationForPgsqlForStringVarcharDatatype149()
257+
{
258+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
259+
}
224260
}

0 commit comments

Comments
 (0)