Skip to content

Commit e0b96a3

Browse files
committed
Separate tests for PgSQL and more
1 parent 11a7d5e commit e0b96a3

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

src/lib/SchemaToDatabase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,16 @@ public static function attributesFromColumnSchemas(array $columnSchemas)
314314
$attribute = new Attribute($columnSchema->name, [
315315
'phpType' => $columnSchema->phpType, // pk
316316
'dbType' => $columnSchema->dbType, // pk
317+
'fkColName' => $columnSchema->name,
318+
319+
'required' => !$columnSchema->allowNull && ($columnSchema->defaultValue === null),
317320
'nullable' => $columnSchema->allowNull,
318321
'size' => $columnSchema->size,
319-
// 'limits' => ['min' => null, 'max' => null, 'minLength' => null], // TODO
322+
320323
'primary' => $columnSchema->isPrimaryKey,
321324
'enumValues' => $columnSchema->enumValues,
325+
'defaultValue' => $columnSchema->defaultValue,
326+
'description' => $columnSchema->comment,
322327
]);
323328

324329
// generate PK using `->primaryKeys()` or similar methods instead of separate SQL statement which sets only PK to a column of table

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ public function buildSecondary(?ManyToManyRelation $relation = null):MigrationMo
172172
$this->migration = Yii::createObject(MigrationModel::class, [$this->model, false, $relation, []]);
173173
$this->newColumns = $relation->columnSchema ?? $this->model->attributesToColumnSchema();
174174

175-
$this->newColumns = $this->model->drop ? [] : $this->newColumns;
176-
177175
$wantNames = array_keys($this->newColumns);
178176
$haveNames = $this->tableSchema->columnNames;
179177
$columnsForCreate = array_map(
@@ -192,6 +190,14 @@ function (string $unknownColumn) {
192190

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

193+
if ($this->model->drop) {
194+
$this->newColumns = [];
195+
$wantNames = [];
196+
$columnsForCreate = [];
197+
$columnsForChange = [];
198+
$columnsForDrop = [];
199+
}
200+
195201
$this->buildColumnsCreation($columnsForCreate);
196202
if ($this->model->junctionCols && !isset($this->model->attributes[$this->model->pkName])) {
197203
if (!empty(array_intersect($columnsForDrop, $this->model->junctionCols))) {
@@ -254,9 +260,6 @@ protected function buildColumnsCreation(array $columns):void
254260
*/
255261
protected function buildColumnsDrop(array $columns):void
256262
{
257-
if ($this->model->drop) {
258-
return;
259-
}
260263
foreach ($columns as $column) {
261264
$tableName = $this->model->getTableAlias();
262265
if ($column->isPrimaryKey && !$column->autoIncrement) {

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ final class PostgresMigrationBuilder extends BaseMigrationBuilder
1818
* @param array|ColumnSchema[] $columns
1919
* @throws \yii\base\InvalidConfigException
2020
*/
21+
#[\Override]
2122
protected function buildColumnsCreation(array $columns):void
2223
{
2324
foreach ($columns as $column) {
@@ -35,6 +36,7 @@ protected function buildColumnsCreation(array $columns):void
3536
* @param array|ColumnSchema[] $columns
3637
* @throws \yii\base\InvalidConfigException
3738
*/
39+
#[\Override]
3840
protected function buildColumnsDrop(array $columns):void
3941
{
4042
foreach ($columns as $column) {

tests/unit/IssueFixTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ private function createTablesForCreateMigrationForDropTable132()
302302
Yii::$app->db->createCommand()->createTable('{{%ubigpks}}', [
303303
'id' => 'ubigpk',
304304
'name' => 'string(150)',
305+
'size' => "ENUM('x-small', 'small', 'medium', 'large', 'x-large') NOT NULL DEFAULT 'x-small'",
306+
'd SMALLINT UNSIGNED ZEROFILL',
307+
'e' => 'SMALLINT UNSIGNED ZEROFILL',
308+
'f' => 'decimal(12,4)',
305309
])->execute();
306310

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

354+
// Create migration for drop table if a entire schema is deleted from OpenAPI spec #132
355+
// https://github.com/cebe/yii2-openapi/issues/132
356+
// For PgSQL
357+
public function testCreateMigrationForDropTable132ForPgsql()
358+
{
359+
$this->changeDbToPgsql();
360+
$testFile = Yii::getAlias("@specs/issue_fix/132_create_migration_for_drop_table/132_create_migration_for_drop_table.php");
361+
$this->createTablesForCreateMigrationForDropTable132ForPgsql();
362+
$this->runGenerator($testFile, 'pgsql');
363+
$this->runActualMigrations('pgsql', 8);
364+
// ... TODO compare files
365+
$this->deleteTablesForCreateMigrationForDropTable132ForPgsql();
366+
$this->deleteTables();
367+
}
368+
369+
private function createTablesForCreateMigrationForDropTable132ForPgsql()
370+
{
371+
Yii::$app->db->createCommand()->createTable('{{%upks}}', [
372+
'id' => 'upk',
373+
'name' => 'string(150)',
374+
])->execute();
375+
Yii::$app->db->createCommand()->createTable('{{%bigpks}}', [
376+
'id' => 'bigpk',
377+
'name' => 'string(150)',
378+
])->execute();
379+
Yii::$app->db->createCommand()->createTable('{{%ubigpks}}', [
380+
'id' => 'ubigpk',
381+
'name' => 'string(150)',
382+
// 'size' => "ENUM('x-small', 'small', 'medium', 'large', 'x-large') NOT NULL DEFAULT 'x-small'",
383+
// 'd SMALLINT UNSIGNED ZEROFILL',
384+
// 'e' => 'SMALLINT UNSIGNED ZEROFILL',
385+
// 'f' => 'decimal(12,4)',
386+
])->execute();
387+
388+
// ---
389+
Yii::$app->db->createCommand()->createTable('{{%fruits}}', [
390+
'id' => 'pk',
391+
'name' => 'string(150)',
392+
'food_of' => 'int'
393+
])->execute();
394+
Yii::$app->db->createCommand()->createTable('{{%pristines}}', [
395+
'id' => 'pk',
396+
'name' => 'string(151)',
397+
'fruit_id' => 'int', // FK
398+
])->execute();
399+
Yii::$app->db->createCommand()->addForeignKey('name', '{{%pristines}}', 'fruit_id', '{{%fruits}}', 'id')->execute();
400+
401+
// ---
402+
Yii::$app->db->createCommand()->createTable('{{%the_animal_table_name}}', [
403+
'id' => 'pk',
404+
'name' => 'string(150)',
405+
])->execute();
406+
Yii::$app->db->createCommand()->addForeignKey('name2', '{{%fruits}}', 'food_of', '{{%the_animal_table_name}}', 'id')->execute();
407+
Yii::$app->db->createCommand()->createTable('{{%the_mango_table_name}}', [
408+
'id' => 'pk',
409+
'name' => 'string(150)',
410+
'food_of' => 'int'
411+
])->execute();
412+
Yii::$app->db->createCommand()->addForeignKey('animal_fruit_fk', '{{%the_mango_table_name}}', 'food_of', '{{%the_animal_table_name}}', 'id')->execute();
413+
}
414+
415+
private function deleteTablesForCreateMigrationForDropTable132ForPgsql()
416+
{
417+
Yii::$app->db->createCommand()->dropForeignKey('name', '{{%pristines}}')->execute();
418+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%pristines}}')->execute();
419+
Yii::$app->db->createCommand()->dropForeignKey('name2', '{{%fruits}}')->execute();
420+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%fruits}}')->execute();
421+
422+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%upks}}')->execute();
423+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%bigpks}}')->execute();
424+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%ubigpks}}')->execute();
425+
426+
Yii::$app->db->createCommand()->dropForeignKey('animal_fruit_fk', '{{%the_mango_table_name}}')->execute();
427+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%the_mango_table_name}}')->execute();
428+
Yii::$app->db->createCommand('DROP TABLE IF EXISTS {{%the_animal_table_name}}')->execute();
429+
}
430+
350431
public function test162BugDollarrefWithXFaker()
351432
{
352433
$testFile = Yii::getAlias("@specs/issue_fix/162_bug_dollarref_with_x_faker/162_bug_dollarref_with_x_faker.php");

0 commit comments

Comments
 (0)