Skip to content

Commit 692c4aa

Browse files
committed
Implement for PgSQL
1 parent 20fad45 commit 692c4aa

File tree

6 files changed

+29
-25
lines changed

6 files changed

+29
-25
lines changed

src/lib/generators/MigrationsGenerator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use cebe\yii2openapi\lib\items\DbModel;
1313
use cebe\yii2openapi\lib\items\MigrationModel;
1414
use cebe\yii2openapi\lib\migrations\BaseMigrationBuilder;
15-
use cebe\yii2openapi\lib\migrations\MigrationRecordBuilder;
1615
use cebe\yii2openapi\lib\migrations\MysqlMigrationBuilder;
1716
use cebe\yii2openapi\lib\migrations\PostgresMigrationBuilder;
1817
use Exception;
@@ -140,7 +139,7 @@ public function buildMigrations():array
140139
protected function createBuilder(DbModel $model):BaseMigrationBuilder
141140
{
142141
if ($this->db->getDriverName() === 'pgsql') {
143-
return Yii::createObject(PostgresMigrationBuilder::class, [$this->db, $model]);
142+
return Yii::createObject(PostgresMigrationBuilder::class, [$this->db, $model, $this->config]);
144143
}
145144
return Yii::createObject(MysqlMigrationBuilder::class, [$this->db, $model, $this->config]);
146145
}

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use cebe\yii2openapi\generator\ApiGenerator;
1111
use cebe\yii2openapi\lib\ColumnToCode;
1212
use cebe\yii2openapi\lib\Config;
13+
use cebe\yii2openapi\lib\CustomSpecAttr;
1314
use cebe\yii2openapi\lib\items\DbModel;
1415
use cebe\yii2openapi\lib\items\ManyToManyRelation;
1516
use cebe\yii2openapi\lib\items\MigrationModel;
@@ -54,7 +55,7 @@ abstract class BaseMigrationBuilder
5455
*/
5556
protected $recordBuilder;
5657

57-
public ?Config $config = null;
58+
protected ?Config $config = null;
5859

5960
/**
6061
* MigrationBuilder constructor.
@@ -597,4 +598,20 @@ public function buildTablesDrop(): void
597598
abstract public function findPosition(ColumnSchema $column, bool $forDrop = false, bool $forAlter = false): ?string;
598599

599600
abstract public function setColumnsPositions();
601+
602+
protected function shouldCompareComment(ColumnSchema $desired): bool
603+
{
604+
$comment = false;
605+
if (isset($this->model->attributes[$desired->name]) && $this->model->attributes[$desired->name]->xDescriptionIsComment) {
606+
$comment = true;
607+
}
608+
if ($this->model->descriptionIsComment) {
609+
$comment = true;
610+
}
611+
if ($this->config !== null &&
612+
!empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) {
613+
$comment = true;
614+
}
615+
return $comment;
616+
}
600617
}

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use cebe\yii2openapi\generator\ApiGenerator;
1111
use cebe\yii2openapi\lib\ColumnToCode;
12-
use cebe\yii2openapi\lib\CustomSpecAttr;
1312
use cebe\yii2openapi\lib\items\DbIndex;
1413
use yii\base\NotSupportedException;
1514
use yii\db\ColumnSchema;
@@ -62,19 +61,9 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
6261

6362
$properties = ['type', 'size', 'allowNull', 'defaultValue', 'enumValues'
6463
, 'dbType', 'phpType'
65-
, 'precision', 'scale', 'unsigned'#, 'comment'
64+
, 'precision', 'scale', 'unsigned'
6665
];
67-
$comment = false;
68-
if (isset($this->model->attributes[$desired->name]) && $this->model->attributes[$desired->name]->xDescriptionIsComment) {
69-
$comment = true;
70-
}
71-
if ($this->model->descriptionIsComment) {
72-
$comment = true;
73-
}
74-
if ($this->config && !empty($this->config->getOpenApi()->{CustomSpecAttr::DESC_IS_COMMENT})) {
75-
$comment = true;
76-
}
77-
if ($comment) {
66+
if ($this->shouldCompareComment($desired)) {
7867
$properties[] = 'comment';
7968
}
8069
foreach ($properties as $attr) {

src/lib/migrations/PostgresMigrationBuilder.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,15 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
141141
$this->modifyDesiredInContextOfCurrent($current, $desiredFromDb);
142142
$this->modifyDesiredFromDbInContextOfDesired($desired, $desiredFromDb);
143143

144-
foreach (['type', 'size', 'allowNull', 'defaultValue', 'enumValues'
144+
$properties = ['type', 'size', 'allowNull', 'defaultValue', 'enumValues'
145145
, 'dbType', 'phpType'
146-
, 'precision', 'scale', 'unsigned', 'comment'
147-
] as $attr) {
146+
, 'precision', 'scale', 'unsigned'
147+
];
148+
if ($this->shouldCompareComment($desired)) {
149+
$properties[] = 'comment';
150+
}
151+
152+
foreach ($properties as $attr) {
148153
if ($attr === 'defaultValue') {
149154
if ($this->isDefaultValueChanged($current, $desiredFromDb)) {
150155
$changedAttributes[] = $attr;

tests/specs/blog_v2/migrations_pgsql_db/m200000_000000_change_table_v2_posts.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ public function safeUp()
1313
$this->dropIndex('v2_posts_slug_key', '{{%v2_posts}}');
1414
$this->dropColumn('{{%v2_posts}}', 'uid');
1515
$this->alterColumn('{{%v2_posts}}', 'category_id', 'int8 NOT NULL USING "category_id"::int8');
16-
$this->addCommentOnColumn('{{%v2_posts}}', 'category_id', 'Category of posts');
1716
$this->alterColumn('{{%v2_posts}}', 'active', "DROP DEFAULT");
1817
$this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int8 NULL USING "created_by_id"::int8');
19-
$this->addCommentOnColumn('{{%v2_posts}}', 'created_by_id', 'The User');
2018
}
2119

2220
public function safeDown()
2321
{
24-
$this->dropCommentFromColumn('{{%v2_posts}}', 'created_by_id');
2522
$this->alterColumn('{{%v2_posts}}', 'created_by_id', 'int4 NULL USING "created_by_id"::int4');
26-
$this->dropCommentFromColumn('{{%v2_posts}}', 'category_id');
2723
$this->alterColumn('{{%v2_posts}}', 'category_id', 'int4 NOT NULL USING "category_id"::int4');
2824
$this->addColumn('{{%v2_posts}}', 'uid', $this->bigInteger()->notNull());
2925
$this->createIndex('v2_posts_slug_key', '{{%v2_posts}}', 'slug', true);

tests/specs/blog_v2/migrations_pgsql_db/m200000_000005_change_table_v2_comments.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ 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)->comment('The User'));
1313
$this->dropColumn('{{%v2_comments}}', 'author_id');
14-
$this->addCommentOnColumn('{{%v2_comments}}', 'post_id', 'A blog post (uid used as pk for test purposes)');
1514
$this->alterColumn('{{%v2_comments}}', 'message', 'text NOT NULL USING "message"::text');
1615
$this->alterColumn('{{%v2_comments}}', 'message', "DROP DEFAULT");
1716
$this->alterColumn('{{%v2_comments}}', 'meta_data', 'varchar(300) NULL USING "meta_data"::varchar');
@@ -29,7 +28,6 @@ public function safeDown()
2928
$this->alterColumn('{{%v2_comments}}', 'created_at', 'int4 NOT NULL USING "created_at"::int4');
3029
$this->alterColumn('{{%v2_comments}}', 'meta_data', 'jsonb NOT NULL USING "meta_data"::jsonb');
3130
$this->alterColumn('{{%v2_comments}}', 'message', 'jsonb NOT NULL USING "message"::jsonb');
32-
$this->dropCommentFromColumn('{{%v2_comments}}', 'post_id');
3331
$this->addColumn('{{%v2_comments}}', 'author_id', $this->integer()->notNull());
3432
$this->dropColumn('{{%v2_comments}}', 'user_id');
3533
$this->alterColumn('{{%v2_comments}}', 'message', "SET DEFAULT '[]'");

0 commit comments

Comments
 (0)