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

[8.x] Fulltext index for PostgreSQL #39875

Merged
merged 2 commits into from Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Illuminate/Database/Schema/Blueprint.php
Expand Up @@ -547,11 +547,12 @@ public function index($columns, $name = null, $algorithm = null)
* @param string|array $columns
* @param string|null $name
* @param string|null $algorithm
* @param string|null $language
* @return \Illuminate\Support\Fluent
*/
public function fulltext($columns, $name = null, $algorithm = null)
public function fulltext($columns, $name = null, $algorithm = null, $language = null)
{
return $this->indexCommand('fulltext', $columns, $name, $algorithm);
return $this->indexCommand('fulltext', $columns, $name, $algorithm, $language);
}

/**
Expand Down Expand Up @@ -1482,9 +1483,10 @@ public function rememberToken()
* @param string|array $columns
* @param string $index
* @param string|null $algorithm
* @param string|null $language
* @return \Illuminate\Support\Fluent
*/
protected function indexCommand($type, $columns, $index, $algorithm = null)
protected function indexCommand($type, $columns, $index, $algorithm = null, $language = null)
{
$columns = (array) $columns;

Expand All @@ -1494,7 +1496,7 @@ protected function indexCommand($type, $columns, $index, $algorithm = null)
$index = $index ?: $this->createIndexName($type, $columns);

return $this->addCommand(
$type, compact('index', 'columns', 'algorithm')
$type, compact('index', 'columns', 'algorithm', 'language')
);
}

Expand Down
39 changes: 39 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Fluent;
use RuntimeException;

class PostgresGrammar extends Grammar
{
Expand Down Expand Up @@ -176,6 +177,32 @@ public function compileIndex(Blueprint $blueprint, Fluent $command)
);
}

/**
* Compile a fulltext index key command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*
* @throws \RuntimeException
*/
public function compileFulltext(Blueprint $blueprint, Fluent $command)
{
if (is_null($command->language)) {
throw new RuntimeException('The PostgreSQL driver requires a language for fulltext index creation.');
}
if (count($command->columns) > 1) {
throw new RuntimeException('The PostgreSQL driver does not support fulltext index creation with multiple columns. Please create the index by yourself.');
}

return sprintf('create index %s on %s using gin (to_tsvector(%s, %s))',
$this->wrap($command->index),
$this->wrapTable($blueprint),
$this->quoteString($command->language),
$this->wrap($command->columns[0])
);
}

/**
* Compile a spatial index key command.
*
Expand Down Expand Up @@ -359,6 +386,18 @@ public function compileDropIndex(Blueprint $blueprint, Fluent $command)
return "drop index {$this->wrap($command->index)}";
}

/**
* Compile a drop fulltext index command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileDropFulltext(Blueprint $blueprint, Fluent $command)
{
return $this->compileDropIndex($blueprint, $command);
}

/**
* Compile a drop spatial index command.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Expand Up @@ -262,6 +262,16 @@ public function testAddingIndexWithAlgorithm()
$this->assertSame('create index "baz" on "users" using hash ("foo", "bar")', $statements[0]);
}

public function testAddingFulltextIndex()
{
$blueprint = new Blueprint('users');
$blueprint->fulltext('body', null, null, 'english');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('create index "users_body_fulltext" on "users" using gin (to_tsvector(\'english\', "body"))', $statements[0]);
}

public function testAddingSpatialIndex()
{
$blueprint = new Blueprint('geo');
Expand Down