Skip to content

Commit 309f7b2

Browse files
authored
Merge pull request #7 from php-openapi/6-run-generated-migration-in-multidbsecondarymigrationtest-and-other-pertinent-test-file
Resolve: Run generated migration in MultiDbSecondaryMigrationTest and other pertinent test file #6
2 parents 1e5954d + e934a3d commit 309f7b2

File tree

7 files changed

+32
-5
lines changed

7 files changed

+32
-5
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ Specify table indexes
212212
default: '{}'
213213
```
214214

215+
If raw DB expression is needed in index, then it must be for only one column. Example:
216+
217+
```yaml
218+
x-indexes:
219+
- "gin(to_tsvector('english', search::text)):search" # valid
220+
- "gin(to_tsvector('english', search::text)):search,column2" # invalid
221+
```
222+
215223
### `x-db-default-expression`
216224

217225
Ability to provide default value by database expression

src/lib/AttributeResolver.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,16 @@ protected function prepareIndexes(array $indexes):array
427427
foreach ($indexes as $index) {
428428
$unique = false;
429429
if (strpos($index, ':') !== false) {
430-
[$indexType, $props] = explode(':', $index);
430+
// [$indexType, $props] = explode(':', $index);
431+
// if `$index` is `gin(to_tsvector('english', search::text)):search,prop2`
432+
$props = strrchr($index, ':'); # `$props` is now `:search,prop2`
433+
$props = substr($props, 1); # search,prop2
434+
$indexType = str_replace(':'.$props, '', $index); # `gin(to_tsvector('english', search::text))`
431435
} else {
432436
$props = $index;
433437
$indexType = null;
434438
}
435-
if ($indexType === 'unique') {
439+
if (strtolower((string) $indexType) === 'unique') {
436440
$indexType = null;
437441
$unique = true;
438442
}

src/lib/migrations/MigrationRecordBuilder.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public function addFk(string $fkName, string $tableAlias, string $fkCol, string
227227
$onUpdate
228228
);
229229
}
230+
throw new \Exception('Cannot add foreign key');
230231
}
231232

232233
public function addUniqueIndex(string $tableAlias, string $indexName, array $columns):string
@@ -242,11 +243,20 @@ public function addUniqueIndex(string $tableAlias, string $indexName, array $col
242243
public function addIndex(string $tableAlias, string $indexName, array $columns, ?string $using = null):string
243244
{
244245
$indexType = $using === null ? 'false' : "'".ColumnToCode::escapeQuotes($using)."'";
246+
247+
if ($using && (stripos($using, '(') !== false) && ApiGenerator::isPostgres()) {
248+
// if `$using` is `gin(to_tsvector('english', search::text))`
249+
$r = explode('(', $using, 2);
250+
$indexType = "'".$r[0]."'"; # `gin`
251+
$columnDbIndexExpression = substr($r[1], 0, -1); # to_tsvector('english', search::text)
252+
$columns = [ColumnToCode::escapeQuotes($columnDbIndexExpression)];
253+
}
254+
245255
return sprintf(
246256
self::ADD_INDEX,
247257
$indexName,
248258
$tableAlias,
249-
count($columns) === 1 ? "'{$columns[0]}'" : '["'.implode('", "', $columns).'"]',
259+
count($columns) === 1 ? "'". $columns[0]."'" : '["'.implode('", "', $columns).'"]',
250260
$indexType
251261
);
252262
}

tests/specs/postgres_custom.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ components:
3535
Custom:
3636
x-table: v3_pgcustom
3737
x-indexes:
38-
- "gin(to_tsvector('english', status)):search"
38+
- "gin(to_tsvector('english', search::text)):search"
3939
required:
4040
- id
4141
properties:

tests/specs/postgres_custom/migrations_pgsql_db/m200000_000000_change_table_v3_pgcustom.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function safeUp()
1717
$this->alterColumn('{{%v3_pgcustom}}', 'json4', "SET DEFAULT '{\"foo\":\"bar\",\"bar\":\"baz\"}'");
1818
$this->alterColumn('{{%v3_pgcustom}}', 'status', "SET DEFAULT 'draft'");
1919
$this->alterColumn('{{%v3_pgcustom}}', 'status_x', "SET DEFAULT 'draft'");
20-
$this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'search', 'gin(to_tsvector(\'english\', status))');
20+
$this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'to_tsvector(\'english\', search::text)', 'gin');
2121
}
2222

2323
public function safeDown()

tests/unit/MultiDbFreshMigrationTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function testMaria()
2626
$this->assertInstanceOf(MySqlSchema::class, Yii::$app->db->schema);
2727
$testFile = Yii::getAlias('@specs/blog.php');
2828
$this->runGenerator($testFile, $dbName);
29+
$this->runActualMigrations($dbName, 5);
2930
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
3031
$actualFiles = $this->findActualFiles();
3132
$this->assertEquals($expectedFiles, $actualFiles);
@@ -39,6 +40,7 @@ public function testPostgres()
3940
$this->assertInstanceOf(PgSqlSchema::class, Yii::$app->db->schema);
4041
$testFile = Yii::getAlias('@specs/blog.php');
4142
$this->runGenerator($testFile, $dbName);
43+
$this->runActualMigrations($dbName, 5);
4244
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
4345
$actualFiles = $this->findActualFiles();
4446
$this->assertEquals($expectedFiles, $actualFiles);
@@ -52,6 +54,7 @@ public function testMysql()
5254
$this->assertInstanceOf(MySqlSchema::class, Yii::$app->db->schema);
5355
$testFile = Yii::getAlias('@specs/blog.php');
5456
$this->runGenerator($testFile, $dbName);
57+
$this->runActualMigrations($dbName, 5);
5558
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
5659
$actualFiles = $this->findActualFiles();
5760
$this->assertEquals($expectedFiles, $actualFiles);

tests/unit/MultiDbSecondaryMigrationTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function testPostgresCustom()
2222
$this->assertInstanceOf(PgSqlSchema::class, Yii::$app->db->schema);
2323
$testFile = Yii::getAlias('@specs/postgres_custom.php');
2424
$this->runGenerator($testFile, $dbName);
25+
$this->runActualMigrations($dbName, 1);
2526
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
2627
$actualFiles = $this->findActualFiles();
2728
$this->assertEquals($expectedFiles, $actualFiles);
@@ -35,6 +36,7 @@ public function testMaria()
3536
$this->assertInstanceOf(MySqlSchema::class, Yii::$app->db->schema);
3637
$testFile = Yii::getAlias('@specs/blog_v2.php');
3738
$this->runGenerator($testFile, $dbName);
39+
// $this->runActualMigrations($dbName, 6); since PK is changed, no need to run actual migrations here
3840
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
3941
$actualFiles = $this->findActualFiles();
4042
$this->assertEquals($expectedFiles, $actualFiles);

0 commit comments

Comments
 (0)