Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 1cdb53b

Browse files
authored
Merge pull request #7 from SOHELAHMED7/107-migrations-are-generated-with-syntax-error-and-wrong-data-type-in-mysql
Fix 107-Migrations are generated with syntax error and wrong data type in MySQL
2 parents eaf2464 + 3fff89a commit 1cdb53b

19 files changed

+253
-31
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ up:
3333
docker-compose up -d
3434
echo "Waiting for mariadb to start up..."
3535
docker-compose exec -T mysql timeout 60s sh -c "while ! (mysql -udbuser -pdbpass -h maria --execute 'SELECT 1;' > /dev/null 2>&1); do echo -n '.'; sleep 0.1 ; done; echo 'ok'" || (docker-compose ps; docker-compose logs; exit 1)
36-
echo "Create another test DB for PgSQL ..." # created because of enum in PgSQL are different than MySQL
3736

3837
cli:
3938
docker-compose exec php bash

src/lib/ColumnToCode.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ public function getAlterExpression(bool $addUsingExpression = false):string
166166
return "'" . $this->rawParts['type'] . "'";
167167
}
168168
if ($addUsingExpression && ApiGenerator::isPostgres()) {
169-
return "'" . $this->rawParts['type'] . " ".$this->rawParts['nullable']
169+
return "'" . $this->rawParts['type'] .
170+
($this->alterByXDbType ?
171+
'' :
172+
" ".$this->rawParts['nullable'])
170173
.' USING "'.$this->column->name.'"::'.$this->typeWithoutSize($this->rawParts['type'])."'";
171174
}
172175

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,20 +400,15 @@ protected function unPrefixTableName(string $tableName):string
400400
return str_replace($this->db->tablePrefix, '', $tableName);
401401
}
402402

403-
protected function isNeedUsingExpression(string $fromType, string $toType):bool
403+
protected function isNeedUsingExpression(string $fromDbType, string $toDbType):bool
404404
{
405-
$strings = ['string', 'text', 'char'];
406-
if (in_array($fromType, $strings) && in_array($toType, $strings)) {
405+
if ($fromDbType === $toDbType) {
407406
return false;
408407
}
409-
$ints = ['smallint', 'integer', 'bigint', 'float', 'decimal'];
410-
if (in_array($fromType, $ints) && in_array($toType, $ints)) {
411-
return false;
412-
}
413-
$dates = ['date', 'timestamp'];
414-
return !(in_array($fromType, $dates) && in_array($toType, $dates));
408+
return true;
415409
}
416410

411+
// temporary save new/changed/desired column to temporary table. If saved we can fetch it from DB and then it can be used to compare with current column
417412
public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema
418413
{
419414
$tableName = 'tmp_table_';

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
6767
, 'dbType', 'phpType'
6868
, 'precision', 'scale', 'unsigned'
6969
], $changed))) {
70-
$addUsing = $this->isNeedUsingExpression($current->type, $desired->type);
71-
$this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired));
70+
$addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType);
71+
$this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired, $addUsing));
7272
$this->migration->addDownCode($this->recordBuilder->alterColumnTypeFromDb($tableName, $current, $addUsing));
7373
}
7474
if (in_array('allowNull', $changed, true)) {

tests/DbTestCase.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,26 @@ protected function checkFiles(array $actual, array $expected)
104104
$this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath");
105105
}
106106
}
107+
108+
protected function runActualMigrations(string $db = 'mysql', int $number = 2): void
109+
{
110+
// up
111+
exec('cd tests; ./yii migrate-'.$db.' --interactive=0', $upOutput, $upExitCode);
112+
$last = count($upOutput) - 1;
113+
$lastThird = count($upOutput) - 3;
114+
$this->assertSame($upExitCode, 0);
115+
$this->assertSame($upOutput[$last], 'Migrated up successfully.');
116+
$this->assertSame($upOutput[$lastThird], $number.' '.(($number === 1) ? 'migration was' : 'migrations were').' applied.');
117+
// 1 migration was applied.
118+
// 2 migrations were applied.
119+
120+
// down
121+
exec('cd tests; ./yii migrate-'.$db.'/down --interactive=0 '.$number, $downOutput, $downExitCode);
122+
$last = count($downOutput) - 1;
123+
$lastThird = count($downOutput) - 3;
124+
$this->assertSame($downExitCode, 0);
125+
$this->assertSame($downOutput[$last], 'Migrated down successfully.');
126+
$this->assertSame($downOutput[$lastThird], $number.' '.(($number === 1) ? 'migration was' : 'migrations were').' reverted.');
127+
128+
}
107129
}

tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ public function safeUp()
1212
$this->addColumn('{{%v2_posts}}', 'lang', 'enum_lang NULL DEFAULT \'ru\'');
1313
$this->dropColumn('{{%v2_posts}}', 'uid');
1414
$this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT");
15-
$this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull());
16-
$this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null());
15+
$this->alterColumn('{{%v2_posts}}', 'category_id', 'bigint NOT NULL USING "category_id"::bigint');
16+
$this->alterColumn('{{%v2_posts}}', 'created_by_id', 'bigint NULL USING "created_by_id"::bigint');
1717
$this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}');
1818
}
1919

2020
public function safeDown()
2121
{
2222
$this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true);
23-
$this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->integer()->null());
24-
$this->alterColumn('{{%v2_posts}}', 'category_id', $this->integer()->notNull());
23+
$this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int4 NULL USING "created_by_id"::int4');
24+
$this->alterColumn('{{%v2_posts}}', 'category_id', 'int4 NOT NULL USING "category_id"::int4');
2525
$this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger()->notNull());
2626
$this->dropColumn('{{%v2_posts}}', 'lang');
2727
$this->dropColumn('{{%v2_posts}}', 'id');

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
@@ -9,7 +9,7 @@ public function safeUp()
99
{
1010
$this->addColumn('{{%v2_categories}}', 'cover', $this->text()->notNull());
1111
$this->alterColumn('{{%v2_categories}}', 'active', "DROP DEFAULT");
12-
$this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull());
12+
$this->alterColumn('{{%v2_categories}}', 'title', 'string(100) NOT NULL USING "title"::string');
1313
$this->dropIndex('v2_categories_title_key', '{{%v2_categories}}');
1414
$this->createIndex('v2_categories_title_index', '{{%v2_categories}}', 'title', false);
1515
}
@@ -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', $this->string(255)->notNull());
21+
$this->alterColumn('{{%v2_categories}}', 'title', 'varchar(255) NOT NULL USING "title"::varchar');
2222
$this->dropColumn('{{%v2_categories}}', 'cover');
2323
$this->alterColumn('{{%v2_categories}}', 'active', "SET DEFAULT 'f'");
2424
}

tests/specs/blog_v2/migrations_pgsql_db/m200000_000004_change_table_v2_users.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function safeDown()
2424
$this->dropIndex('v2_users_role_flags_hash_index', '{{%v2_users}}');
2525
$this->dropIndex('v2_users_login_key', '{{%v2_users}}');
2626
$this->createIndex('v2_users_username_key', '{{%v2_users}}', 'username', true);
27-
$this->alterColumn('{{%v2_users}}', 'role', $this->string(20)->null());
27+
$this->alterColumn('{{%v2_users}}', 'role', 'varchar(20) NULL USING "role"::varchar');
2828
$this->alterColumn('{{%v2_users}}', 'email', $this->string(200)->notNull());
2929
$this->addColumn('{{%v2_users}}', 'username', $this->string(200)->notNull());
3030
$this->dropColumn('{{%v2_users}}', 'login');

tests/specs/blog_v2/migrations_pgsql_db/m200000_000005_change_table_v2_comments.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public function safeUp()
1111
$this->dropForeignKey('fk_v2_comments_post_id_v2_posts_uid', '{{%v2_comments}}');
1212
$this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null));
1313
$this->dropColumn('{{%v2_comments}}', 'author_id');
14-
$this->alterColumn('{{%v2_comments}}', 'created_at', $this->timestamp()->notNull());
15-
$this->alterColumn('{{%v2_comments}}', 'message', $this->text()->notNull());
14+
$this->alterColumn('{{%v2_comments}}', 'created_at', 'datetime NOT NULL USING "created_at"::datetime');
15+
$this->alterColumn('{{%v2_comments}}', 'message', 'text NOT NULL USING "message"::text');
1616
$this->alterColumn('{{%v2_comments}}', 'message', "DROP DEFAULT");
17-
$this->alterColumn('{{%v2_comments}}', 'meta_data', $this->string(300)->null());
17+
$this->alterColumn('{{%v2_comments}}', 'meta_data', 'string(300) NULL USING "meta_data"::string');
1818
$this->alterColumn('{{%v2_comments}}', 'meta_data', "DROP NOT NULL");
1919
$this->alterColumn('{{%v2_comments}}', 'meta_data', "SET DEFAULT ''");
2020
$this->addForeignKey('fk_v2_comments_post_id_v2_posts_id', '{{%v2_comments}}', 'post_id', '{{%v2_posts}}', 'id');

tests/specs/enum/change/pgsql/app/migrations_pgsql_db/m200000_000000_change_table_editcolumns.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function safeUp()
1111
$this->alterColumn('{{%editcolumns}}', 'connection', 'enum_connection USING "connection"::enum_connection');
1212
$this->alterColumn('{{%editcolumns}}', 'connection', "SET NOT NULL");
1313
$this->alterColumn('{{%editcolumns}}', 'connection', "SET DEFAULT 'WIRED'");
14-
$this->alterColumn('{{%editcolumns}}', 'device', $this->text()->null());
14+
$this->alterColumn('{{%editcolumns}}', 'device', 'text NULL USING "device"::text');
1515
$this->alterColumn('{{%editcolumns}}', 'device', "DROP NOT NULL");
1616
$this->alterColumn('{{%editcolumns}}', 'device', "DROP DEFAULT");
1717
$this->execute('DROP TYPE enum_device');
@@ -21,7 +21,7 @@ public function safeDown()
2121
{
2222
$this->execute('CREATE TYPE enum_device AS ENUM(\'MOBILE\', \'TV\', \'COMPUTER\')');
2323
$this->alterColumn('{{%editcolumns}}', 'device', 'enum_device USING "device"::enum_device');
24-
$this->alterColumn('{{%editcolumns}}', 'connection', $this->string(255)->null());
24+
$this->alterColumn('{{%editcolumns}}', 'connection', 'varchar(255) NULL USING "connection"::varchar');
2525
$this->alterColumn('{{%editcolumns}}', 'connection', "DROP NOT NULL");
2626
$this->alterColumn('{{%editcolumns}}', 'connection', "DROP DEFAULT");
2727
$this->execute('DROP TYPE enum_connection');

0 commit comments

Comments
 (0)