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 all commits
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
38 changes: 38 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,31 @@ 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)
{
$language = $command->language ?: 'english';

if (count($command->columns) > 1) {
throw new RuntimeException('The PostgreSQL driver does not support fulltext index creation using multiple columns.');
}

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

/**
* Compile a spatial index key command.
*
Expand Down Expand Up @@ -359,6 +385,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
30 changes: 30 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Expand Up @@ -262,6 +262,36 @@ 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');
$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 testAddingFulltextIndexWithLanguage()
{
$blueprint = new Blueprint('users');
$blueprint->fulltext('body')->language('spanish');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

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

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

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

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