Skip to content

Commit

Permalink
[8.x] Fulltext index for PostgreSQL (#39875)
Browse files Browse the repository at this point in the history
* fulltext index support for PostgreSQL

* use fluent language declarationgs

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
tpetry and taylorotwell committed Dec 7, 2021
1 parent c51ad0f commit 8c1f6ce
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
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

0 comments on commit 8c1f6ce

Please sign in to comment.