Skip to content

Commit

Permalink
Separate tests for PgSQL and more
Browse files Browse the repository at this point in the history
  • Loading branch information
SOHELAHMED7 committed Jun 22, 2024
1 parent 11a7d5e commit e0b96a3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/lib/SchemaToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,16 @@ public static function attributesFromColumnSchemas(array $columnSchemas)
$attribute = new Attribute($columnSchema->name, [
'phpType' => $columnSchema->phpType, // pk
'dbType' => $columnSchema->dbType, // pk
'fkColName' => $columnSchema->name,

'required' => !$columnSchema->allowNull && ($columnSchema->defaultValue === null),
'nullable' => $columnSchema->allowNull,
'size' => $columnSchema->size,
// 'limits' => ['min' => null, 'max' => null, 'minLength' => null], // TODO

'primary' => $columnSchema->isPrimaryKey,
'enumValues' => $columnSchema->enumValues,
'defaultValue' => $columnSchema->defaultValue,
'description' => $columnSchema->comment,
]);

// generate PK using `->primaryKeys()` or similar methods instead of separate SQL statement which sets only PK to a column of table
Expand Down
13 changes: 8 additions & 5 deletions src/lib/migrations/BaseMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ public function buildSecondary(?ManyToManyRelation $relation = null):MigrationMo
$this->migration = Yii::createObject(MigrationModel::class, [$this->model, false, $relation, []]);
$this->newColumns = $relation->columnSchema ?? $this->model->attributesToColumnSchema();

$this->newColumns = $this->model->drop ? [] : $this->newColumns;

$wantNames = array_keys($this->newColumns);
$haveNames = $this->tableSchema->columnNames;
$columnsForCreate = array_map(
Expand All @@ -192,6 +190,14 @@ function (string $unknownColumn) {

$columnsForChange = array_intersect($wantNames, $haveNames);

if ($this->model->drop) {
$this->newColumns = [];
$wantNames = [];
$columnsForCreate = [];
$columnsForChange = [];
$columnsForDrop = [];
}

$this->buildColumnsCreation($columnsForCreate);
if ($this->model->junctionCols && !isset($this->model->attributes[$this->model->pkName])) {
if (!empty(array_intersect($columnsForDrop, $this->model->junctionCols))) {
Expand Down Expand Up @@ -254,9 +260,6 @@ protected function buildColumnsCreation(array $columns):void
*/
protected function buildColumnsDrop(array $columns):void
{
if ($this->model->drop) {
return;
}
foreach ($columns as $column) {
$tableName = $this->model->getTableAlias();
if ($column->isPrimaryKey && !$column->autoIncrement) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/migrations/PostgresMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class PostgresMigrationBuilder extends BaseMigrationBuilder
* @param array|ColumnSchema[] $columns
* @throws \yii\base\InvalidConfigException
*/
#[\Override]
protected function buildColumnsCreation(array $columns):void
{
foreach ($columns as $column) {
Expand All @@ -35,6 +36,7 @@ protected function buildColumnsCreation(array $columns):void
* @param array|ColumnSchema[] $columns
* @throws \yii\base\InvalidConfigException
*/
#[\Override]
protected function buildColumnsDrop(array $columns):void
{
foreach ($columns as $column) {
Expand Down
81 changes: 81 additions & 0 deletions tests/unit/IssueFixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ private function createTablesForCreateMigrationForDropTable132()
Yii::$app->db->createCommand()->createTable('{{%ubigpks}}', [
'id' => 'ubigpk',
'name' => 'string(150)',
'size' => "ENUM('x-small', 'small', 'medium', 'large', 'x-large') NOT NULL DEFAULT 'x-small'",
'd SMALLINT UNSIGNED ZEROFILL',
'e' => 'SMALLINT UNSIGNED ZEROFILL',
'f' => 'decimal(12,4)',
])->execute();

// ---
Expand Down Expand Up @@ -347,6 +351,83 @@ private function deleteTablesForCreateMigrationForDropTable132()
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%the_animal_table_name}}')->execute();
}

// Create migration for drop table if a entire schema is deleted from OpenAPI spec #132
// https://github.com/cebe/yii2-openapi/issues/132
// For PgSQL
public function testCreateMigrationForDropTable132ForPgsql()
{
$this->changeDbToPgsql();
$testFile = Yii::getAlias("@specs/issue_fix/132_create_migration_for_drop_table/132_create_migration_for_drop_table.php");
$this->createTablesForCreateMigrationForDropTable132ForPgsql();
$this->runGenerator($testFile, 'pgsql');
$this->runActualMigrations('pgsql', 8);
// ... TODO compare files
$this->deleteTablesForCreateMigrationForDropTable132ForPgsql();
$this->deleteTables();
}

private function createTablesForCreateMigrationForDropTable132ForPgsql()
{
Yii::$app->db->createCommand()->createTable('{{%upks}}', [
'id' => 'upk',
'name' => 'string(150)',
])->execute();
Yii::$app->db->createCommand()->createTable('{{%bigpks}}', [
'id' => 'bigpk',
'name' => 'string(150)',
])->execute();
Yii::$app->db->createCommand()->createTable('{{%ubigpks}}', [
'id' => 'ubigpk',
'name' => 'string(150)',
// 'size' => "ENUM('x-small', 'small', 'medium', 'large', 'x-large') NOT NULL DEFAULT 'x-small'",
// 'd SMALLINT UNSIGNED ZEROFILL',
// 'e' => 'SMALLINT UNSIGNED ZEROFILL',
// 'f' => 'decimal(12,4)',
])->execute();

// ---
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
'id' => 'pk',
'name' => 'string(150)',
'food_of' => 'int'
])->execute();
Yii::$app->db->createCommand()->createTable('{{%pristines}}', [
'id' => 'pk',
'name' => 'string(151)',
'fruit_id' => 'int', // FK
])->execute();
Yii::$app->db->createCommand()->addForeignKey('name', '{{%pristines}}', 'fruit_id', '{{%fruits}}', 'id')->execute();

// ---
Yii::$app->db->createCommand()->createTable('{{%the_animal_table_name}}', [
'id' => 'pk',
'name' => 'string(150)',
])->execute();
Yii::$app->db->createCommand()->addForeignKey('name2', '{{%fruits}}', 'food_of', '{{%the_animal_table_name}}', 'id')->execute();
Yii::$app->db->createCommand()->createTable('{{%the_mango_table_name}}', [
'id' => 'pk',
'name' => 'string(150)',
'food_of' => 'int'
])->execute();
Yii::$app->db->createCommand()->addForeignKey('animal_fruit_fk', '{{%the_mango_table_name}}', 'food_of', '{{%the_animal_table_name}}', 'id')->execute();
}

private function deleteTablesForCreateMigrationForDropTable132ForPgsql()
{
Yii::$app->db->createCommand()->dropForeignKey('name', '{{%pristines}}')->execute();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute();
Yii::$app->db->createCommand()->dropForeignKey('name2', '{{%fruits}}')->execute();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();

Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%upks}}')->execute();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%bigpks}}')->execute();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%ubigpks}}')->execute();

Yii::$app->db->createCommand()->dropForeignKey('animal_fruit_fk', '{{%the_mango_table_name}}')->execute();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%the_mango_table_name}}')->execute();
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%the_animal_table_name}}')->execute();
}

public function test162BugDollarrefWithXFaker()
{
$testFile = Yii::getAlias("@specs/issue_fix/162_bug_dollarref_with_x_faker/162_bug_dollarref_with_x_faker.php");
Expand Down

0 comments on commit e0b96a3

Please sign in to comment.