Skip to content

Resolve: Run generated migration in MultiDbSecondaryMigrationTest and other pertinent test file #6 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ Specify table indexes
default: '{}'
```

If raw DB expression is needed in index, then it must be for only one column. Example:

```yaml
x-indexes:
- "gin(to_tsvector('english', search::text)):search" # valid
- "gin(to_tsvector('english', search::text)):search,column2" # invalid
```

### `x-db-default-expression`

Ability to provide default value by database expression
Expand Down
8 changes: 6 additions & 2 deletions src/lib/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,16 @@ protected function prepareIndexes(array $indexes):array
foreach ($indexes as $index) {
$unique = false;
if (strpos($index, ':') !== false) {
[$indexType, $props] = explode(':', $index);
// [$indexType, $props] = explode(':', $index);
// if `$index` is `gin(to_tsvector('english', search::text)):search,prop2`
$props = strrchr($index, ':'); # `$props` is now `:search,prop2`
$props = substr($props, 1); # search,prop2
$indexType = str_replace(':'.$props, '', $index); # `gin(to_tsvector('english', search::text))`
} else {
$props = $index;
$indexType = null;
}
if ($indexType === 'unique') {
if (strtolower((string) $indexType) === 'unique') {
$indexType = null;
$unique = true;
}
Expand Down
12 changes: 11 additions & 1 deletion src/lib/migrations/MigrationRecordBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public function addFk(string $fkName, string $tableAlias, string $fkCol, string
$onUpdate
);
}
throw new \Exception('Cannot add foreign key');
}

public function addUniqueIndex(string $tableAlias, string $indexName, array $columns):string
Expand All @@ -242,11 +243,20 @@ public function addUniqueIndex(string $tableAlias, string $indexName, array $col
public function addIndex(string $tableAlias, string $indexName, array $columns, ?string $using = null):string
{
$indexType = $using === null ? 'false' : "'".ColumnToCode::escapeQuotes($using)."'";

if ($using && (stripos($using, '(') !== false) && ApiGenerator::isPostgres()) {
// if `$using` is `gin(to_tsvector('english', search::text))`
$r = explode('(', $using, 2);
$indexType = "'".$r[0]."'"; # `gin`
$columnDbIndexExpression = substr($r[1], 0, -1); # to_tsvector('english', search::text)
$columns = [ColumnToCode::escapeQuotes($columnDbIndexExpression)];
}

return sprintf(
self::ADD_INDEX,
$indexName,
$tableAlias,
count($columns) === 1 ? "'{$columns[0]}'" : '["'.implode('", "', $columns).'"]',
count($columns) === 1 ? "'". $columns[0]."'" : '["'.implode('", "', $columns).'"]',
$indexType
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/postgres_custom.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ components:
Custom:
x-table: v3_pgcustom
x-indexes:
- "gin(to_tsvector('english', status)):search"
- "gin(to_tsvector('english', search::text)):search"
required:
- id
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function safeUp()
$this->alterColumn('{{%v3_pgcustom}}', 'json4', "SET DEFAULT '{\"foo\":\"bar\",\"bar\":\"baz\"}'");
$this->alterColumn('{{%v3_pgcustom}}', 'status', "SET DEFAULT 'draft'");
$this->alterColumn('{{%v3_pgcustom}}', 'status_x', "SET DEFAULT 'draft'");
$this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'search', 'gin(to_tsvector(\'english\', status))');
$this->createIndex('v3_pgcustom_search_gin_index', '{{%v3_pgcustom}}', 'to_tsvector(\'english\', search::text)', 'gin');
}

public function safeDown()
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/MultiDbFreshMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function testMaria()
$this->assertInstanceOf(MySqlSchema::class, Yii::$app->db->schema);
$testFile = Yii::getAlias('@specs/blog.php');
$this->runGenerator($testFile, $dbName);
$this->runActualMigrations($dbName, 5);
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
$actualFiles = $this->findActualFiles();
$this->assertEquals($expectedFiles, $actualFiles);
Expand All @@ -39,6 +40,7 @@ public function testPostgres()
$this->assertInstanceOf(PgSqlSchema::class, Yii::$app->db->schema);
$testFile = Yii::getAlias('@specs/blog.php');
$this->runGenerator($testFile, $dbName);
$this->runActualMigrations($dbName, 5);
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
$actualFiles = $this->findActualFiles();
$this->assertEquals($expectedFiles, $actualFiles);
Expand All @@ -52,6 +54,7 @@ public function testMysql()
$this->assertInstanceOf(MySqlSchema::class, Yii::$app->db->schema);
$testFile = Yii::getAlias('@specs/blog.php');
$this->runGenerator($testFile, $dbName);
$this->runActualMigrations($dbName, 5);
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
$actualFiles = $this->findActualFiles();
$this->assertEquals($expectedFiles, $actualFiles);
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/MultiDbSecondaryMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testPostgresCustom()
$this->assertInstanceOf(PgSqlSchema::class, Yii::$app->db->schema);
$testFile = Yii::getAlias('@specs/postgres_custom.php');
$this->runGenerator($testFile, $dbName);
$this->runActualMigrations($dbName, 1);
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
$actualFiles = $this->findActualFiles();
$this->assertEquals($expectedFiles, $actualFiles);
Expand All @@ -35,6 +36,7 @@ public function testMaria()
$this->assertInstanceOf(MySqlSchema::class, Yii::$app->db->schema);
$testFile = Yii::getAlias('@specs/blog_v2.php');
$this->runGenerator($testFile, $dbName);
// $this->runActualMigrations($dbName, 6); since PK is changed, no need to run actual migrations here
$expectedFiles = $this->findExpectedFiles($testFile, $dbName);
$actualFiles = $this->findActualFiles();
$this->assertEquals($expectedFiles, $actualFiles);
Expand Down
Loading