Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
08e30ab
Test
SOHELAHMED7 Dec 21, 2022
e367a47
Merge branch '100-timestamp-migrations-do-not-work-in-mariadb' of git…
SOHELAHMED7 Dec 21, 2022
ec097d7
Test 2
SOHELAHMED7 Dec 21, 2022
03ca98b
test 3
SOHELAHMED7 Dec 21, 2022
7dee6b9
Merge pull request #1 from SOHELAHMED7/100-timestamp-migrations-do-no…
SOHELAHMED7 Dec 21, 2022
6c90780
Merge branch 'master' of https://github.com/SOHELAHMED7/yii2-openapi …
SOHELAHMED7 Dec 21, 2022
984c7b3
Test
SOHELAHMED7 Dec 21, 2022
96257f6
Cleanup
SOHELAHMED7 Dec 21, 2022
d3ecd74
test
SOHELAHMED7 Dec 21, 2022
c809394
Fix issue in setting default expression
SOHELAHMED7 Dec 21, 2022
3d4d71c
Fix issue: decimal considered as string instead of double/float
SOHELAHMED7 Dec 21, 2022
a9c6ec2
Fix enum issue for MySQL, MariaDB and partially for PgSQL #111
SOHELAHMED7 Dec 22, 2022
89f98c2
WIP
SOHELAHMED7 Dec 23, 2022
b047f7d
undo WIP
SOHELAHMED7 Dec 23, 2022
6cea7f2
Fix issue: CREATE/DROP enum in PGSQL up/down migration not in correct…
SOHELAHMED7 Dec 23, 2022
6a9a680
Fix style
SOHELAHMED7 Dec 23, 2022
c60403f
Fix quote issue in migration down code for MySQL and Maria + add more…
SOHELAHMED7 Dec 23, 2022
fcc53a2
Fix multiple issue + add more tests for eunm in Pgsql + other DB
SOHELAHMED7 Dec 23, 2022
130c69a
Fix bug in column change detection in Pgsql
SOHELAHMED7 Dec 24, 2022
1b0bc2e
Fix failing tests
SOHELAHMED7 Dec 24, 2022
a037827
Fix enum related issues + add more tests for it for all 3 DBs
SOHELAHMED7 Dec 24, 2022
dae8e4c
Implement Enum change value migration - WIP
SOHELAHMED7 Dec 24, 2022
e67e8e1
WIP
SOHELAHMED7 Dec 24, 2022
ded2d2b
Complete enum tests + add stub for enum values change test
SOHELAHMED7 Dec 27, 2022
af614eb
Remove unwanted files
SOHELAHMED7 Dec 27, 2022
39d2ff0
Add docs in README
SOHELAHMED7 Dec 27, 2022
5badea4
Restore tests that were deleted in https://github.com/cebe/yii2-opena…
SOHELAHMED7 Dec 27, 2022
72c2a79
Add enh.
SOHELAHMED7 Dec 27, 2022
a9e0312
Merge pull request #2 from SOHELAHMED7/timestamp-enum-x-db-type-bug-fix
SOHELAHMED7 Dec 28, 2022
c9322ad
test
SOHELAHMED7 Dec 28, 2022
a8d3b61
Undo test
SOHELAHMED7 Dec 28, 2022
c0563e7
Fix issue: defaultValue change detection for expression was not worki…
SOHELAHMED7 Dec 28, 2022
921f6ed
Merge pull request #3 from SOHELAHMED7/json-issue-fix
SOHELAHMED7 Dec 29, 2022
20e960a
test to trigger CI
SOHELAHMED7 Dec 29, 2022
4066e93
test to trigger CI 2
SOHELAHMED7 Dec 29, 2022
ca72322
test to trigger CI 3
SOHELAHMED7 Dec 29, 2022
672ae45
test to trigger CI 4
SOHELAHMED7 Dec 29, 2022
7da7317
Fix style
SOHELAHMED7 Dec 29, 2022
53d4de4
test to trigger CI 5
SOHELAHMED7 Dec 29, 2022
0041f7b
Fix style
SOHELAHMED7 Dec 29, 2022
a2d0c80
Fix https://github.com/cebe/yii2-openapi/issues/104
SOHELAHMED7 Dec 30, 2022
1f1e71f
remove unwanted files
SOHELAHMED7 Dec 30, 2022
86ae33d
Merge pull request #4 from SOHELAHMED7/104-remove-id-from-rules-metho…
SOHELAHMED7 Dec 30, 2022
8c074a9
Fix https://github.com/cebe/yii2-openapi/issues/115
SOHELAHMED7 Dec 30, 2022
512a11e
Merge pull request #5 from SOHELAHMED7/115-default-in-openapi-schema-…
SOHELAHMED7 Dec 30, 2022
91273e8
test
SOHELAHMED7 Dec 30, 2022
0ac648d
Add tests for this issue
SOHELAHMED7 Dec 30, 2022
d7e57a0
remove unwanted space from README
SOHELAHMED7 Dec 30, 2022
0eae0f4
Merge pull request #6 from SOHELAHMED7/114-errors-in-migrations-in-ca…
SOHELAHMED7 Dec 30, 2022
4aad200
Merge branch 'cebe:master' into master
SOHELAHMED7 Dec 30, 2022
220d1ee
Test
SOHELAHMED7 Dec 30, 2022
c73c16d
Fix https://github.com/cebe/yii2-openapi/issues/125
SOHELAHMED7 Dec 30, 2022
a6fc5b1
Run actual migrations
SOHELAHMED7 Dec 31, 2022
09a5fb8
Add a test for float issue in PgSQL
SOHELAHMED7 Dec 31, 2022
67ad00c
test
SOHELAHMED7 Dec 31, 2022
2e382e4
Undo test
SOHELAHMED7 Dec 31, 2022
a8d8788
WIP
SOHELAHMED7 Dec 31, 2022
433e7f0
Fix https://github.com/cebe/yii2-openapi/issues/126
SOHELAHMED7 Jan 2, 2023
ce56996
Merge pull request #8 from SOHELAHMED7/126-dont-use-second-db-pgtestd…
SOHELAHMED7 Jan 2, 2023
60dd357
Merge branches 'master' and '107-migrations-are-generated-with-syntax…
SOHELAHMED7 Jan 2, 2023
d0b83eb
Remove unwanted line
SOHELAHMED7 Jan 2, 2023
9e341e9
Initial commit of this branch
SOHELAHMED7 Jan 2, 2023
4da1db2
Fix https://github.com/cebe/yii2-openapi/issues/127
SOHELAHMED7 Jan 2, 2023
2f58a9e
Fix quote issue in enum in PgSQL
SOHELAHMED7 Jan 2, 2023
e8f1df1
Fix quote issue in enum in PgSQL 2
SOHELAHMED7 Jan 2, 2023
eaf2464
Merge pull request #9 from SOHELAHMED7/127-quote-issue-to-preserve-th…
SOHELAHMED7 Jan 3, 2023
a37e2ca
Merge branches 'master' and '107-migrations-are-generated-with-syntax…
SOHELAHMED7 Jan 3, 2023
a6c20ef
Fix failing tests
SOHELAHMED7 Jan 3, 2023
3fff89a
Fix https://github.com/cebe/yii2-openapi/issues/107
SOHELAHMED7 Jan 3, 2023
1cdb53b
Merge pull request #7 from SOHELAHMED7/107-migrations-are-generated-w…
SOHELAHMED7 Jan 3, 2023
8aa5697
Initial commit of this branch to create PR
SOHELAHMED7 Jan 3, 2023
dcbde27
Add tests
SOHELAHMED7 Jan 3, 2023
123e67a
Implement quote in enum type names
SOHELAHMED7 Jan 3, 2023
d4527f5
Implement enum with table name
SOHELAHMED7 Jan 3, 2023
d4f6f2d
Adjust tests for enum with table name
SOHELAHMED7 Jan 4, 2023
e2fc2e9
Merge pull request #10 from SOHELAHMED7/128-change-enum-name-generati…
SOHELAHMED7 Jan 4, 2023
8250c09
Fix errors in https://github.com/SOHELAHMED7/yii2-openapi/pull/10
SOHELAHMED7 Jan 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/lib/migrations/BaseMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,18 +412,22 @@ protected function isNeedUsingExpression(string $fromDbType, string $toDbType):b
public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema): \yii\db\ColumnSchema
{
$tableName = 'tmp_table_';
$tmpEnumName = function (string $columnName): string {
return '"tmp_enum_'.$columnName.'_"';
};

Yii::$app->db->createCommand('DROP TABLE IF EXISTS '.$tableName)->execute();

if (is_string($columnSchema->xDbType) && !empty($columnSchema->xDbType)) {
$column = [$columnSchema->name.' '.$this->newColStr($columnSchema)];
$name = MigrationRecordBuilder::quote($columnSchema->name);
$column = [$name.' '.$this->newColStr($columnSchema)];
if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {
$column = strtr($column, ['enum_'.$columnSchema->name => 'tmp_enum_'.$columnSchema->name.'_']);
$column = strtr($column, ['enum_'.$columnSchema->name => $tmpEnumName($columnSchema->name)]);
}
} else {
$column = [$columnSchema->name => $this->newColStr($columnSchema)];
if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {
$column[$columnSchema->name] = strtr($column[$columnSchema->name], ['enum_'.$columnSchema->name => 'tmp_enum_'.$columnSchema->name.'_']);
$column[$columnSchema->name] = strtr($column[$columnSchema->name], ['enum_'.$columnSchema->name => $tmpEnumName($columnSchema->name)]);
}
}

Expand All @@ -434,7 +438,7 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema):
return "'$aValue'";
}, $allEnumValues);
Yii::$app->db->createCommand(
'CREATE TYPE tmp_enum_'.$columnSchema->name.'_ AS ENUM('.implode(', ', $allEnumValues).')'
'CREATE TYPE '.$tmpEnumName($columnSchema->name).' AS ENUM('.implode(', ', $allEnumValues).')'
)->execute();
}

Expand All @@ -445,9 +449,9 @@ public function tmpSaveNewCol(\cebe\yii2openapi\db\ColumnSchema $columnSchema):
Yii::$app->db->createCommand()->dropTable($tableName)->execute();

if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {// drop enum
Yii::$app->db->createCommand('DROP TYPE tmp_enum_'.$columnSchema->name.'_')->execute();
if ($table->columns[$columnSchema->name]->dbType !== 'tmp_enum_'.$columnSchema->name.'_') {
throw new \Exception('Unknown error related to PgSQL enum');
Yii::$app->db->createCommand('DROP TYPE '.$tmpEnumName($columnSchema->name))->execute();
if ('"'.$table->columns[$columnSchema->name]->dbType.'"' !== $tmpEnumName($columnSchema->name)) {
throw new \Exception('Unknown error related to PgSQL enum '.$table->columns[$columnSchema->name]->dbType);
}
// reset back column enum name to original as we are comparing with current
// 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
Expand Down
23 changes: 18 additions & 5 deletions src/lib/migrations/MigrationRecordBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ final class MigrationRecordBuilder
public const ADD_FK = MigrationRecordBuilder::INDENT . "\$this->addForeignKey('%s', '%s', '%s', '%s', '%s');";
public const ADD_PK = MigrationRecordBuilder::INDENT . "\$this->addPrimaryKey('%s', '%s', '%s');";
public const ADD_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addColumn('%s', '%s', %s);";
public const ALTER_COLUMN = MigrationRecordBuilder::INDENT . "\$this->alterColumn('%s', '%s', %s);";

public const ADD_COLUMN_RAW = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ADD COLUMN %s %s')->execute();";

public const ALTER_COLUMN = MigrationRecordBuilder::INDENT . "\$this->alterColumn('%s', '%s', %s);";
public const ADD_COLUMN_RAW = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ADD COLUMN %s %s')->execute();";

public const ALTER_COLUMN_RAW = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s MODIFY %s %s')->execute();";
public const ALTER_COLUMN_RAW_PGSQL = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE %s')->execute();";

public const ALTER_COLUMN_RAW_PGSQL = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ALTER COLUMN \"%s\" SET DATA TYPE %s')->execute();";

/**
* @var \yii\db\Schema
Expand All @@ -62,7 +63,8 @@ public function createTable(string $tableAlias, array $columns):string
$codeColumns = [];
foreach ($columns as $columnName => $cebeDbColumnSchema) {
if (is_string($cebeDbColumnSchema->xDbType) && !empty($cebeDbColumnSchema->xDbType)) {
$codeColumns[] = $columnName.' '.$this->columnToCode($cebeDbColumnSchema, false)->getCode();
$name = static::quote($columnName);
$codeColumns[] = $name.' '.$this->columnToCode($cebeDbColumnSchema, false)->getCode();
} else {
$codeColumns[$columnName] = $this->columnToCode($cebeDbColumnSchema, false)->getCode();
}
Expand All @@ -79,7 +81,8 @@ public function addColumn(string $tableAlias, ColumnSchema $column):string
{
if (is_string($column->xDbType) && !empty($column->xDbType)) {
$converter = $this->columnToCode($column, false);
return sprintf(self::ADD_COLUMN_RAW, $tableAlias, $column->name, $converter->getCode());
$name = static::quote($column->name);
return sprintf(self::ADD_COLUMN_RAW, $tableAlias, $name, $converter->getCode());
}

$converter = $this->columnToCode($column, false);
Expand All @@ -93,6 +96,7 @@ public function addDbColumn(string $tableAlias, ColumnSchema $column):string
{
if (property_exists($column, 'xDbType') && is_string($column->xDbType) && !empty($column->xDbType)) {
$converter = $this->columnToCode($column, true);
$name = static::quote($column->name);
return sprintf(self::ADD_COLUMN_RAW, $tableAlias, $column->name, $converter->getCode());
}
$converter = $this->columnToCode($column, true);
Expand Down Expand Up @@ -269,4 +273,13 @@ private function columnToCode(
$alterByXDbType
]);
}

// https://github.com/cebe/yii2-openapi/issues/127
public static function quote(string $columnName): string
{
if (ApiGenerator::isPostgres()) {
return '"'.$columnName.'"';
}
return $columnName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class m200000_000002_create_table_blog_posts extends \yii\db\Migration
public function safeUp()
{
$this->createTable('{{%blog_posts}}', [
0 => 'uid varchar(128) NOT NULL',
0 => '"uid" varchar(128) NOT NULL',
'title' => $this->string(255)->notNull(),
'slug' => $this->string(200)->null()->defaultValue(null),
'category_id' => $this->integer()->notNull(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public function safeUp()
'id' => $this->bigPrimaryKey(),
'post_id' => $this->string(128)->notNull(),
'author_id' => $this->integer()->notNull(),
0 => 'message json NOT NULL DEFAULT \'[]\'',
1 => 'meta_data json NOT NULL DEFAULT \'[]\'',
0 => '"message" json NOT NULL DEFAULT \'[]\'',
1 => '"meta_data" json NOT NULL DEFAULT \'[]\'',
'created_at' => $this->integer()->notNull(),
]);
$this->addForeignKey('fk_post_comments_post_id_blog_posts_uid', '{{%post_comments}}', 'post_id', '{{%blog_posts}}', 'uid');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function safeUp()
$this->execute('CREATE TYPE enum_lang AS ENUM(\'ru\', \'eng\')');
$this->createTable('{{%v2_tags}}', [
'id' => $this->bigPrimaryKey(),
0 => 'name varchar(100) NOT NULL',
0 => '"name" varchar(100) NOT NULL',
'lang' => 'enum_lang NOT NULL',
]);
$this->createIndex('v2_tags_name_key', '{{%v2_tags}}', 'name', true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function safeUp()
$this->addColumn('{{%v2_users}}', 'login', $this->text()->notNull());
$this->dropColumn('{{%v2_users}}', 'username');
$this->alterColumn('{{%v2_users}}', 'created_at', "DROP DEFAULT");
$this->db->createCommand('ALTER TABLE {{%v2_users}} ALTER COLUMN email SET DATA TYPE varchar(255)')->execute();
$this->db->createCommand('ALTER TABLE {{%v2_users}} ALTER COLUMN "email" SET DATA TYPE varchar(255)')->execute();
$this->alterColumn('{{%v2_users}}', 'role', 'enum_role USING "role"::enum_role');
$this->alterColumn('{{%v2_users}}', 'role', "DROP DEFAULT");
$this->dropIndex('v2_users_username_key', '{{%v2_users}}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class m200000_000000_change_table_column_name_changes extends \yii\db\Migration
{
public function safeUp()
{
$this->db->createCommand('ALTER TABLE {{%column_name_changes}} ADD COLUMN updated_at_2 timestamp NOT NULL')->execute();
$this->db->createCommand('ALTER TABLE {{%column_name_changes}} ADD COLUMN "updated_at_2" timestamp NOT NULL')->execute();
$this->dropColumn('{{%column_name_changes}}', 'updated_at');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public function safeUp()
'id' => $this->bigPrimaryKey(),
'name' => $this->string(100)->notNull(),
'parent_id' => $this->bigInteger()->null()->defaultValue(null),
0 => 'args text[] NULL DEFAULT \'{"foo","bar","baz"}\'',
1 => 'kwargs json NOT NULL DEFAULT \'[{"foo":"bar"},{"buzz":"fizz"}]\'',
0 => '"args" text[] NULL DEFAULT \'{"foo","bar","baz"}\'',
1 => '"kwargs" json NOT NULL DEFAULT \'[{"foo":"bar"},{"buzz":"fizz"}]\'',
]);
$this->addForeignKey('fk_menus_parent_id_menus_id', '{{%menus}}', 'parent_id', '{{%menus}}', 'id');
}
Expand Down
Loading