Skip to content
Prev Previous commit
Next Next commit
Run actual migrations
  • Loading branch information
SOHELAHMED7 committed Dec 31, 2022
commit a6fc5b1c9fbae58c21f7f02a06790b4de1542c09
5 changes: 4 additions & 1 deletion src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public function getAlterExpression(bool $addUsingExpression = false):string
return "'" . $this->rawParts['type'] . "'";
}
if ($addUsingExpression && ApiGenerator::isPostgres()) {
return "'" . $this->rawParts['type'] . " ".$this->rawParts['nullable']
return "'" . $this->rawParts['type'] .
($this->alterByXDbType ?
'' :
" ".$this->rawParts['nullable'])
.' USING "'.$this->column->name.'"::'.$this->typeWithoutSize($this->rawParts['type'])."'";
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/migrations/BaseMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ protected function isNeedUsingExpression(string $fromDbType, string $toDbType):b
return true;
}

// 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
public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema
{
$tableName = 'tmp_table_';
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 @@ -68,7 +68,7 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
, 'precision', 'scale', 'unsigned'
], $changed))) {
$addUsing = $this->isNeedUsingExpression($current->dbType, $desired->dbType);
$this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired));
$this->migration->addUpCode($this->recordBuilder->alterColumnType($tableName, $desired, $addUsing));
$this->migration->addDownCode($this->recordBuilder->alterColumnTypeFromDb($tableName, $current, $addUsing));
}
if (in_array('allowNull', $changed, true)) {
Expand Down
21 changes: 21 additions & 0 deletions tests/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,25 @@ protected function checkFiles(array $actual, array $expected)
$this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath");
}
}

protected function runActualMigrations(string $db = 'mysql', int $number = 2): void
{
// up
exec('cd tests; ./yii migrate-'.$db.' --interactive=0', $upOutput, $upExitCode);
$last = count($upOutput) - 1;
$lastThird = count($upOutput) - 3;
$this->assertSame($upExitCode, 0);
$this->assertSame($upOutput[$last], 'Migrated up successfully.');
$this->assertSame($upOutput[$lastThird], $number.' migrations were applied.');
// 1 migration was applied.

// down
exec('cd tests; ./yii migrate-'.$db.'/down --interactive=0 '.$number, $downOutput, $downExitCode);
$last = count($downOutput) - 1;
$lastThird = count($downOutput) - 3;
$this->assertSame($downExitCode, 0);
$this->assertSame($downOutput[$last], 'Migrated down successfully.');
$this->assertSame($downOutput[$lastThird], $number.' migrations were reverted.');

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function safeUp()
$this->addColumn('{{%v2_posts}}', 'lang', 'enum_lang NULL DEFAULT \'ru\'');
$this->dropColumn('{{%v2_posts}}', 'uid');
$this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT");
$this->alterColumn('{{%v2_posts}}', 'category_id', $this->bigInteger()->notNull());
$this->alterColumn('{{%v2_posts}}', 'created_by_id', $this->bigInteger()->null());
$this->alterColumn('{{%v2_posts}}', 'category_id', 'bigint NOT NULL USING "category_id"::bigint');
$this->alterColumn('{{%v2_posts}}', 'created_by_id', 'bigint NULL USING "created_by_id"::bigint');
$this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function safeUp()
{
$this->addColumn('{{%v2_categories}}', 'cover', $this->text()->notNull());
$this->alterColumn('{{%v2_categories}}', 'active', "DROP DEFAULT");
$this->alterColumn('{{%v2_categories}}', 'title', $this->string(100)->notNull());
$this->alterColumn('{{%v2_categories}}', 'title', 'string(100) NOT NULL USING "title"::string');
$this->dropIndex('v2_categories_title_key', '{{%v2_categories}}');
$this->createIndex('v2_categories_title_index', '{{%v2_categories}}', 'title', false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public function safeUp()
$this->dropForeignKey('fk_v2_comments_post_id_v2_posts_uid', '{{%v2_comments}}');
$this->addColumn('{{%v2_comments}}', 'user_id', $this->bigInteger()->null()->defaultValue(null));
$this->dropColumn('{{%v2_comments}}', 'author_id');
$this->alterColumn('{{%v2_comments}}', 'created_at', $this->timestamp()->notNull());
$this->alterColumn('{{%v2_comments}}', 'message', $this->text()->notNull());
$this->alterColumn('{{%v2_comments}}', 'created_at', 'datetime NOT NULL USING "created_at"::datetime');
$this->alterColumn('{{%v2_comments}}', 'message', 'text NOT NULL USING "message"::text');
$this->alterColumn('{{%v2_comments}}', 'message', "DROP DEFAULT");
$this->alterColumn('{{%v2_comments}}', 'meta_data', $this->string(300)->null());
$this->alterColumn('{{%v2_comments}}', 'meta_data', 'string(300) NULL USING "meta_data"::string');
$this->alterColumn('{{%v2_comments}}', 'meta_data', "DROP NOT NULL");
$this->alterColumn('{{%v2_comments}}', 'meta_data', "SET DEFAULT ''");
$this->addForeignKey('fk_v2_comments_post_id_v2_posts_id', '{{%v2_comments}}', 'post_id', '{{%v2_posts}}', 'id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function safeUp()
$this->alterColumn('{{%editcolumns}}', 'connection', 'enum_connection USING "connection"::enum_connection');
$this->alterColumn('{{%editcolumns}}', 'connection', "SET NOT NULL");
$this->alterColumn('{{%editcolumns}}', 'connection', "SET DEFAULT 'WIRED'");
$this->alterColumn('{{%editcolumns}}', 'device', $this->text()->null());
$this->alterColumn('{{%editcolumns}}', 'device', 'text NULL USING "device"::text');
$this->alterColumn('{{%editcolumns}}', 'device', "DROP NOT NULL");
$this->alterColumn('{{%editcolumns}}', 'device', "DROP DEFAULT");
$this->execute('DROP TYPE enum_device');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ public function safeUp()
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ADD COLUMN json_col_def_n json NOT NULL DEFAULT \'[]\'')->execute();
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ADD COLUMN json_col_def_n_2 json NOT NULL DEFAULT \'[]\'')->execute();
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ADD COLUMN text_col_array text[] NULL DEFAULT NULL')->execute();
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN dec_col SET DATA TYPE decimal(12,2)')->execute();
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN dec_col SET DATA TYPE decimal(12,2) USING "dec_col"::decimal(12,2)')->execute();
$this->alterColumn('{{%editcolumns}}', 'dec_col', "SET DEFAULT 3.14");
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN json_col SET DATA TYPE text')->execute();
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN json_col SET DATA TYPE text USING "json_col"::text')->execute();
$this->alterColumn('{{%editcolumns}}', 'json_col', "SET NOT NULL");
$this->alterColumn('{{%editcolumns}}', 'json_col', "SET DEFAULT 'fox jumps over dog'");
$this->alterColumn('{{%editcolumns}}', 'json_col_2', "SET NOT NULL");
$this->alterColumn('{{%editcolumns}}', 'json_col_2', "SET DEFAULT '[]'");
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN name SET DATA TYPE varchar(254)')->execute();
$this->alterColumn('{{%editcolumns}}', 'name', "SET DEFAULT 'Horse-2'");
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN numeric_col SET DATA TYPE double precision')->execute();
$this->db->createCommand('ALTER TABLE {{%editcolumns}} ALTER COLUMN numeric_col SET DATA TYPE double precision USING "numeric_col"::double precision')->execute();
$this->alterColumn('{{%editcolumns}}', 'str_col_def', "SET NOT NULL");
$this->alterColumn('{{%editcolumns}}', 'str_col_def', "DROP DEFAULT");
$this->alterColumn('{{%editcolumns}}', 'string_col', $this->text()->null());
$this->alterColumn('{{%editcolumns}}', 'string_col', 'text NULL USING "string_col"::text');
$this->alterColumn('{{%editcolumns}}', 'string_col', "DROP NOT NULL");
}

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function testFresh()
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('mysql', 3);

$this->changeDbToMariadb();
$this->deleteTables();
Expand All @@ -43,6 +44,7 @@ public function testFresh()
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('maria', 3);

$this->changeDbToPgsql();
$this->deleteTables();
Expand All @@ -56,6 +58,7 @@ public function testFresh()
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('pgsql', 3);
}

public function testAddNewColumn() // and drop enum column
Expand All @@ -73,6 +76,7 @@ public function testAddNewColumn() // and drop enum column
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('mysql', 3);

// Mariadb
$this->changeDbToMariadb();
Expand All @@ -88,6 +92,7 @@ public function testAddNewColumn() // and drop enum column
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('maria', 3);

// Pgsql
$this->changeDbToPgsql();
Expand All @@ -103,6 +108,7 @@ public function testAddNewColumn() // and drop enum column
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('pgsql', 3);
}

public function testChangeToAndFromEnum() // edit enum to string and vice versa
Expand All @@ -119,6 +125,7 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('mysql', 3);

// Mariadb
$this->changeDbToMariadb();
Expand All @@ -134,6 +141,7 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('maria', 3);


$this->changeDbToPgsql();
Expand All @@ -149,6 +157,7 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('pgsql', 3);
}

// TODO ENH enum change is more work than just changing the eunm values. And for PgSQL it is even more
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/XDbTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testXDbTypeFresh()
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('mysql', 4);

// same yaml file is used for MySQL and MariaDB ----------------------
$this->changeDbToMariadb();
Expand All @@ -43,6 +44,7 @@ public function testXDbTypeFresh()
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('maria', 4);

// PgSQL ------------------------------------------------
$this->changeDbToPgsql();
Expand All @@ -57,6 +59,7 @@ public function testXDbTypeFresh()
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('pgsql', 4);
}

public function testXDbTypeSecondaryWithNewColumn() // v2
Expand All @@ -74,6 +77,7 @@ public function testXDbTypeSecondaryWithNewColumn() // v2
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('mysql', 4);

// same yaml file is used for MySQL and MariaDB ----------------------
$this->changeDbToMariadb();
Expand All @@ -90,6 +94,7 @@ public function testXDbTypeSecondaryWithNewColumn() // v2
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('maria', 4);

// PgSQL ------------------------------------------------
$this->changeDbToPgsql();
Expand All @@ -106,6 +111,7 @@ public function testXDbTypeSecondaryWithNewColumn() // v2
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('pgsql', 4);
}

public function testXDbTypeSecondaryWithEditColumn() // v3
Expand All @@ -123,6 +129,7 @@ public function testXDbTypeSecondaryWithEditColumn() // v3
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('mysql', 4);

// same yaml file is used for MySQL and MariaDB ----------------------
$this->changeDbToMariadb();
Expand All @@ -139,6 +146,7 @@ public function testXDbTypeSecondaryWithEditColumn() // v3
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('maria', 4);

// PgSQL ------------------------------------------------
$this->changeDbToPgsql();
Expand All @@ -155,6 +163,7 @@ public function testXDbTypeSecondaryWithEditColumn() // v3
'recursive' => true,
]);
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations('pgsql', 4);
}

private function deleteTables()
Expand Down