From f39c078ba19fa1e2cf0123bc65f9ae576f02e538 Mon Sep 17 00:00:00 2001 From: tpetry Date: Thu, 2 Dec 2021 21:27:35 +0100 Subject: [PATCH 1/2] fulltext index support for PostgreSQL --- src/Illuminate/Database/Schema/Blueprint.php | 10 +++-- .../Schema/Grammars/PostgresGrammar.php | 39 +++++++++++++++++++ .../DatabasePostgresSchemaGrammarTest.php | 10 +++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 954607f3c2b0..9d882fdfe041 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -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); } /** @@ -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; @@ -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') ); } diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index 133da288f19d..5276df5fba36 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,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. * @@ -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. * diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 3a7f80ae3865..adb2e1ec6f4c 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -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'); From c1066ddbd13c87b083f1b8ed247c64068e8a239c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 7 Dec 2021 08:22:35 -0600 Subject: [PATCH 2/2] use fluent language declarationgs --- src/Illuminate/Database/Schema/Blueprint.php | 10 ++++----- .../Schema/Grammars/PostgresGrammar.php | 9 ++++---- .../DatabasePostgresSchemaGrammarTest.php | 22 ++++++++++++++++++- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 9d882fdfe041..954607f3c2b0 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -547,12 +547,11 @@ 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, $language = null) + public function fulltext($columns, $name = null, $algorithm = null) { - return $this->indexCommand('fulltext', $columns, $name, $algorithm, $language); + return $this->indexCommand('fulltext', $columns, $name, $algorithm); } /** @@ -1483,10 +1482,9 @@ 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, $language = null) + protected function indexCommand($type, $columns, $index, $algorithm = null) { $columns = (array) $columns; @@ -1496,7 +1494,7 @@ protected function indexCommand($type, $columns, $index, $algorithm = null, $lan $index = $index ?: $this->createIndexName($type, $columns); return $this->addCommand( - $type, compact('index', 'columns', 'algorithm', 'language') + $type, compact('index', 'columns', 'algorithm') ); } diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index 5276df5fba36..c7023c8ea08e 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -188,17 +188,16 @@ public function compileIndex(Blueprint $blueprint, Fluent $command) */ 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.'); - } + $language = $command->language ?: 'english'; + 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.'); + 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($command->language), + $this->quoteString($language), $this->wrap($command->columns[0]) ); } diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index adb2e1ec6f4c..12601bb8c802 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -265,13 +265,33 @@ public function testAddingIndexWithAlgorithm() public function testAddingFulltextIndex() { $blueprint = new Blueprint('users'); - $blueprint->fulltext('body', null, null, 'english'); + $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');