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

Commit ce56996

Browse files
authored
Merge pull request #8 from SOHELAHMED7/126-dont-use-second-db-pgtestdb2-in-pgsql-in-library-source-code
Fix issue 126 - Don't use second db 'pg_test_db_2' in PgSQL in library source code
2 parents 4aad200 + 433e7f0 commit ce56996

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ up:
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)
3636
echo "Create another test DB for PgSQL ..." # created because of enum in PgSQL are different than MySQL
37-
docker-compose exec -T postgres bash -c "psql --username dbuser --dbname testdb -c 'create database pg_test_db_2;'; psql --username dbuser --dbname testdb -c 'grant all privileges on database pg_test_db_2 to dbuser;'" || (docker-compose ps; docker-compose logs; exit 1)
38-
3937

4038
cli:
4139
docker-compose exec php bash

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,19 @@ protected function isNeedUsingExpression(string $fromType, string $toType):bool
417417
public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema
418418
{
419419
$tableName = 'tmp_table_';
420-
$db = 'db';
421-
if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {
422-
$db = 'pg_test_db_2';
423-
}
424420

425-
Yii::$app->{$db}->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute();
421+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute();
426422

427423
if (is_string($columnSchema->xDbType) && !empty($columnSchema->xDbType)) {
428424
$column = [$columnSchema->name.' '.$this->newColStr($columnSchema)];
425+
if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {
426+
$column = strtr($column, ['enum_'.$columnSchema->name => 'tmp_enum_'.$columnSchema->name.'_']);
427+
}
429428
} else {
430429
$column = [$columnSchema->name => $this->newColStr($columnSchema)];
430+
if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {
431+
$column[$columnSchema->name] = strtr($column[$columnSchema->name], ['enum_'.$columnSchema->name => 'tmp_enum_'.$columnSchema->name.'_']);
432+
}
431433
}
432434

433435
// create enum if relevant
@@ -436,19 +438,25 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema):
436438
$allEnumValues = array_map(function ($aValue) {
437439
return "'$aValue'";
438440
}, $allEnumValues);
439-
Yii::$app->{$db}->createCommand(
440-
'CREATE TYPE enum_'.$columnSchema->name.' AS ENUM('.implode(', ', $allEnumValues).')'
441+
Yii::$app->db->createCommand(
442+
'CREATE TYPE tmp_enum_'.$columnSchema->name.'_ AS ENUM('.implode(', ', $allEnumValues).')'
441443
)->execute();
442444
}
443445

444-
Yii::$app->{$db}->createCommand()->createTable($tableName, $column)->execute();
446+
Yii::$app->db->createCommand()->createTable($tableName, $column)->execute();
445447

446-
$table = Yii::$app->{$db}->getTableSchema($tableName);
448+
$table = Yii::$app->db->getTableSchema($tableName);
447449

448-
Yii::$app->{$db}->createCommand()->dropTable($tableName)->execute();
450+
Yii::$app->db->createCommand()->dropTable($tableName)->execute();
449451

450452
if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {// drop enum
451-
Yii::$app->{$db}->createCommand('DROP TYPE enum_'.$columnSchema->name)->execute();
453+
Yii::$app->db->createCommand('DROP TYPE tmp_enum_'.$columnSchema->name.'_')->execute();
454+
if ($table->columns[$columnSchema->name]->dbType !== 'tmp_enum_'.$columnSchema->name.'_') {
455+
throw new \Exception('Unknown error related to PgSQL enum');
456+
}
457+
// reset back column enum name to original as we are comparing with current
458+
// e.g. we get different enum type name such as `enum_status` and `tmp_enum_status_` even there is no change, so below statement fix this issue
459+
$table->columns[$columnSchema->name]->dbType = 'enum_'.$columnSchema->name;
452460
}
453461

454462
return $table->columns[$columnSchema->name];

tests/config/console.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@
5353
'charset' => 'utf8',
5454
'tablePrefix'=>'itt_',
5555
],
56-
'pg_test_db_2' => [
57-
'class' => \yii\db\Connection::class,
58-
'dsn' => 'pgsql:host=postgres;dbname=pg_test_db_2',
59-
'username' => 'dbuser',
60-
'password' => 'dbpass',
61-
'charset' => 'utf8',
62-
],
6356
'mysql' => [
6457
'class' => \yii\db\Connection::class,
6558
'dsn' => 'mysql:host=mysql;dbname=testdb',

0 commit comments

Comments
 (0)