From 8c1f6cecc00f1f488c44328f5d2dcfc61db2c6a8 Mon Sep 17 00:00:00 2001 From: Tobias Petry Date: Tue, 7 Dec 2021 15:25:54 +0100 Subject: [PATCH] [8.x] Fulltext index for PostgreSQL (#39875) * fulltext index support for PostgreSQL * use fluent language declarationgs Co-authored-by: Taylor Otwell --- .../Schema/Grammars/PostgresGrammar.php | 38 +++++++++++++++++++ .../DatabasePostgresSchemaGrammarTest.php | 30 +++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index 133da288f19d..c7023c8ea08e 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -4,6 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Fluent; +use RuntimeException; class PostgresGrammar extends Grammar { @@ -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. * @@ -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. * diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 3a7f80ae3865..12601bb8c802 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -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');