Skip to content
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

Update addIndex method to automatically execute an analyze to refresh… #5

Merged
merged 1 commit into from
Feb 8, 2024
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ $this->commentOnColumn(table: 'address', name: 'name', comment: null)

<details><summary>Add index</summary>

> Note: Adding an index on a table will execute an "analyze" on all columns of the table to update statistics

```php
$this->addIndex(name: 'idx_contact_email', table: 'contact', columns: ['email'], unique: false, usingMethod: 'GIN', where: 'country = "France"')
```
Expand Down
7 changes: 4 additions & 3 deletions src/Doctrine/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function down(Schema $schema): void
$this->throwIrreversibleMigrationException();
}

protected function addUnsafeSql(string $sql, array $params = [], array $types = [], int $statementTimeout = null): void
protected function addUnsafeSql(string $sql, array $params = [], array $types = [], ?int $statementTimeout = null): void
{
if (null !== $statementTimeout) {
parent::addSql("SET statement_timeout TO '{$statementTimeout}s'");
Expand Down Expand Up @@ -76,6 +76,7 @@ protected function addIndex(
$where ? " WHERE $where" : '',
));
$this->addUnsafeSql("SET lock_timeout TO '3s'");
$this->addUnsafeSql(sprintf('ANALYZE %s', $table));
}

protected function dropIndex(string $name): void
Expand All @@ -88,7 +89,7 @@ protected function renameIndex(string $from, string $to): void
$this->addUnsafeSql(sprintf('ALTER INDEX %s RENAME TO %s', $from, $to));
}

protected function addColumn(string $table, string $name, string $type, string $defaultValue = null, bool $nullable = true): void
protected function addColumn(string $table, string $name, string $type, ?string $defaultValue = null, bool $nullable = true): void
{
if (null !== $defaultValue && u($defaultValue)->trim()->ignoreCase()->equalsTo('NULL')) {
throw new AbortMigration(__METHOD__.' requires the usage of null PHP value instead of string one as default value.');
Expand Down Expand Up @@ -192,7 +193,7 @@ protected function createTable(string $table, array $columnDefinitions): void
* @param string|null $options Can be used to add things like:
* [ ON DELETE|ON UPDATE referential_action ] [ DEFERRABLE|NOT DEFERRABLE ] [ INITIALLY DEFERRED|INITIALLY IMMEDIATE ]
*/
protected function addForeignKey(string $table, string $name, string $column, string $referenceTable, string $referenceColumn, string $options = null): void
protected function addForeignKey(string $table, string $name, string $column, string $referenceTable, string $referenceColumn, ?string $options = null): void
{
$this->addUnsafeSql(sprintf('ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)%s NOT VALID',
$table,
Expand Down
11 changes: 8 additions & 3 deletions tests/Database/Doctrine/Migrations/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function testAddIndexConcurrentlyWithOneColumn(): void
'SET lock_timeout TO \'0\'',
'CREATE INDEX CONCURRENTLY idx_approver_email ON approver (email)',
'SET lock_timeout TO \'3s\'',
'ANALYZE approver',
]);
}

Expand All @@ -83,6 +84,7 @@ public function testAddIndexConcurrentlyWithMultipleColumns(): void
'SET lock_timeout TO \'0\'',
'CREATE INDEX CONCURRENTLY idx_approver_first_name_email ON approver (first_name,email)',
'SET lock_timeout TO \'3s\'',
'ANALYZE approver',
]);
}

Expand All @@ -95,6 +97,7 @@ public function testAddUniqueIndexConcurrently(): void
'SET lock_timeout TO \'0\'',
'CREATE UNIQUE INDEX CONCURRENTLY uq_signer_email ON signer (email)',
'SET lock_timeout TO \'3s\'',
'ANALYZE signer',
]);
}

Expand All @@ -107,6 +110,7 @@ public function testAddIndexConcurrentlyUsingGinMethod(): void
'SET lock_timeout TO \'0\'',
'CREATE INDEX CONCURRENTLY idx_signer_email ON signer USING GIN(email)',
'SET lock_timeout TO \'3s\'',
'ANALYZE signer',
]);
}

Expand All @@ -119,6 +123,7 @@ public function testAddIndexConcurrentlyWhere(): void
'SET lock_timeout TO \'0\'',
'CREATE INDEX CONCURRENTLY idx_signer_email ON signer (email) WHERE name = "foo"',
'SET lock_timeout TO \'3s\'',
'ANALYZE signer',
]);
}

Expand Down Expand Up @@ -331,7 +336,7 @@ public function up(Schema $schema): void
throw new \Exception('Unused for this test');
}

public function addUnsafeSql(string $sql, array $params = [], array $types = [], int $statementTimeout = null): void
public function addUnsafeSql(string $sql, array $params = [], array $types = [], ?int $statementTimeout = null): void
{
parent::addUnsafeSql($sql, $params, $types, $statementTimeout);
}
Expand All @@ -351,7 +356,7 @@ public function renameIndex(string $from, string $to): void
parent::renameIndex($from, $to);
}

public function addColumn(string $table, string $name, string $type, string $defaultValue = null, bool $nullable = true): void
public function addColumn(string $table, string $name, string $type, ?string $defaultValue = null, bool $nullable = true): void
{
parent::addColumn($table, $name, $type, $defaultValue, $nullable);
}
Expand Down Expand Up @@ -396,7 +401,7 @@ public function createTable(string $table, array $columnDefinitions): void
parent::createTable($table, $columnDefinitions);
}

public function addForeignKey(string $table, string $name, string $column, string $referenceTable, string $referenceColumn, string $options = null): void
public function addForeignKey(string $table, string $name, string $column, string $referenceTable, string $referenceColumn, ?string $options = null): void
{
parent::addForeignKey($table, $name, $column, $referenceTable, $referenceColumn, $options);
}
Expand Down
Loading